Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ethagnawl/es6-generators-fibonacci

Proof of concept for Wikipedia article on generators.
https://github.com/ethagnawl/es6-generators-fibonacci

Last synced: about 2 months ago
JSON representation

Proof of concept for Wikipedia article on generators.

Awesome Lists containing this project

README

        

Example used in [Wikipedia article on generators](https://en.wikipedia.org/w/index.php?title=Generator_(computer_programming)#ECMAScript).

function* fibonacci() {

let init = 1;

let [prev, curr] = [0, init];

yield init;

while (true) {

[prev, curr] = [curr, prev + curr];

yield curr;

}

}

var gen = fibonacci();

console.log(gen.next().value); // 1
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // 3
console.log(gen.next().value); // 5
console.log(gen.next().value); // 8

The example can be run with: `npm install . && node index.js`