Skip to content

Instantly share code, notes, and snippets.

@alexnm
Created January 10, 2016 17:45
Show Gist options
  • Save alexnm/58e73bbc48fbd4321f13 to your computer and use it in GitHub Desktop.
Save alexnm/58e73bbc48fbd4321f13 to your computer and use it in GitHub Desktop.
function* fibonacci( ){
let n1 = 0;
let n2 = 1;
while ( true ) {
const current = n1;
n1 = n2;
n2 = current + n1;
yield current;
}
}
let iterator = fibonacci( );
iterator.next( ); // 0
iterator.next( ); // 1
iterator.next( ); // 1
iterator.next( ); // 2
iterator.next( ); // 3
iterator.next( ); // 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment