Skip to content

Instantly share code, notes, and snippets.

@git2samus
Created May 22, 2012 17:56
Show Gist options
  • Select an option

  • Save git2samus/2770593 to your computer and use it in GitHub Desktop.

Select an option

Save git2samus/2770593 to your computer and use it in GitHub Desktop.
> function fibonacciFactory() {
var a=0, b=1;
return function() {
var next=a+b;
a=b, b=next;
return a;
};
};
undefined
> next_fibonacci = fibonacciFactory();
function () { var next=a+b; a=b, b=next; return a; }
> next_fibonacci();
1
> next_fibonacci();
1
> next_fibonacci();
2
> next_fibonacci();
3
> next_fibonacci();
5
> next_fibonacci();
8
> next_fibonacci();
13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment