Last active
August 29, 2015 14:19
-
-
Save rektide/55a8d6afba389edd651b to your computer and use it in GitHub Desktop.
yield-star a by-hand made thing
This file contains hidden or 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
// attempts | |
var si= Symbol.iterator | |
function *realGenerator(){ | |
yield *[1,3,4,7,11] | |
} | |
var g1= realGenerator() | |
console.log(g1.next(),'=== 1') | |
console.log(g1[si]().next(),'=== 3') | |
function fakeGenerator(){ | |
var i= 1 | |
var o= {} | |
o[si]= function(){ | |
console.log('si') | |
return this | |
} | |
o.next= function(){ | |
return {value: i++, done: false} | |
} | |
return o | |
} | |
var f1= fakeGenerator() | |
console.log(f1.next(),' === 1') | |
console.log(f1[si]().next(),'=== 2') | |
console.log('yield* testing:') | |
function *yieldStar(a){ | |
yield *a; | |
yield 1; | |
} | |
var g2= yieldStar(realGenerator()) | |
console.log(g2.next(),'=== 1') | |
var f2= yieldStar(fakeGenerator()) | |
console.log(f2.next(),'=== 1') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment