Last active
December 26, 2015 23:19
-
-
Save bjouhier/7229244 to your computer and use it in GitHub Desktop.
paris.js 30-10-2013
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('colors'); | |
function* genFibo() { | |
var f1 = 0, f2 = 1; | |
while (true) { | |
console.log(("genFibo loop: " + f1).green) | |
yield f1; | |
var f = f1 + f2; | |
f1 = f2; | |
f2 = f; | |
} | |
} | |
var g = genFibo(); | |
for (var i = 0; i < 10; i++) { | |
var v = g.next(); | |
console.log(("fibo(" + i + ")=" + v.value).blue); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('colors'); | |
function* myFirstGenerator() { | |
console.log("generator before yield hello".green); | |
yield "hello"; | |
console.log("generator before yield world".green); | |
yield "world"; | |
console.log("generator at end".green); | |
} | |
console.log("starting program".blue); | |
var g = myFirstGenerator(); | |
console.log("generator created".blue); | |
var v1 = g.next(); | |
console.log(("next #1 returned " + JSON.stringify(v1) + "").blue); | |
var v2 = g.next(); | |
console.log(("next #2 returned " + JSON.stringify(v2) + "").blue); | |
var v3 = g.next(); | |
console.log(("next #3 returned " + JSON.stringify(v3) + "").blue); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tree = { | |
left: { | |
left: { left: { value: 1 }, value: 3, right: { value: 8 } }, | |
value: 10, | |
right: { value: 13 } | |
}, | |
value: 20, | |
right: { value: 22 } | |
}; | |
function* walk(t) { | |
if (t.left) yield* walk(t.left); | |
yield t.value; | |
if (t.right) yield* walk(t.right); | |
} | |
var walker = walk(tree); | |
for (var v of walker) console.log(v); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment