Re: [JSMentors] SICP-style streams, and the Sieve of Eratosthenes

2011-06-30 Thread Poetro
{{{ function fib(n) { fib = function (n) { if (fib.cache[n] === undefined) { fib.cache[n] = fib(n - 1) + fib(n - 2); } return fib.cache[n]; } fib.cache = [0, 1]; fib.next = function () { return fib(fib.cache.length); } return fib(n); } (function(n) { fib(0); w

Re: [JSMentors] SICP-style streams, and the Sieve of Eratosthenes

2011-06-30 Thread Poetro
I have a different approach to a similar problem: {{{ function fib(n) { fib = function (n) { fib.calls++; if (fib.cache[n] === undefined) { fib.cache[n] = fib(n - 1) + fib(n - 2); } return fib.cache[n]; } fib.calls = 0; fib.cache = [0, 1]; return fib(n); } function

Re: [JSMentors] SICP-style streams, and the Sieve of Eratosthenes

2011-06-22 Thread Nick Morgan
On 21 June 2011 18:48, Dmitry A. Soshnikov wrote: > On 21.06.2011 2:22, Nick Morgan wrote: >> >> Hi all >> >> I've been messing about with streams in JavaScript, trying to >> implement some of the code from section 3.5 >> (http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-24.html#%_sec_3.5) >>

Re: [JSMentors] SICP-style streams, and the Sieve of Eratosthenes

2011-06-21 Thread Dmitry A. Soshnikov
On 21.06.2011 2:22, Nick Morgan wrote: Hi all I've been messing about with streams in JavaScript, trying to implement some of the code from section 3.5 (http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-24.html#%_sec_3.5) of SICP. The book which I recommend to read for _every_ programmer.

[JSMentors] SICP-style streams, and the Sieve of Eratosthenes

2011-06-20 Thread Nick Morgan
Hi all I've been messing about with streams in JavaScript, trying to implement some of the code from section 3.5 (http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-24.html#%_sec_3.5) of SICP. Here's a fiddle showing the Sieve of Eratosthenes, displaying each prime as it is found. http://jsfidd