Created
July 19, 2019 05:22
-
-
Save matthewharwood/b722988116afc8eb79a8321a3e940eee to your computer and use it in GitHub Desktop.
Composing Generators
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 * type (iterable) { | |
yield * iterable; | |
} | |
const bool = () => type([true, false]); | |
const str = () => type([ | |
'', | |
undefined, | |
'Lorem', | |
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', | |
'😋😋😋😋' | |
]); | |
function runGenerator(gen) { | |
for(const g of gen()) { | |
console.log(g); | |
} | |
} | |
function * composeGenerator (...iterables) { | |
for (const iterable of iterables) yield * iterable; | |
} | |
const input = () => composeGenerator(str(), bool()); | |
console.log('***********'); | |
runGenerator(input) | |
console.log('***********'); | |
runGenerator(str) | |
runGenerator(str) | |
console.log('***********'); | |
runGenerator(bool) | |
runGenerator(bool) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment