Created
April 11, 2014 14:35
-
-
Save yosriady/10473905 to your computer and use it in GitHub Desktop.
Javascript Yield example
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
function yfunc(prev, index, array, callback){ | |
var newValue = callback(prev, index, array); | |
console.log(newValue); | |
return function(){ | |
return yfunc(newValue, index++, array.push(newValue), callback) | |
}; | |
} | |
function yield(callback){ | |
return yfunc(null, 0, [], callback); | |
} | |
var oddEnum = yield(function(prev, index, array){ | |
return 2*index + 1; | |
}); | |
oddEnum() // 1,3,4,7,9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment