Skip to content

Instantly share code, notes, and snippets.

@matthewharwood
Created July 19, 2019 05:22
Show Gist options
  • Save matthewharwood/b722988116afc8eb79a8321a3e940eee to your computer and use it in GitHub Desktop.
Save matthewharwood/b722988116afc8eb79a8321a3e940eee to your computer and use it in GitHub Desktop.
Composing Generators
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