Skip to content

Instantly share code, notes, and snippets.

@craftybones
Last active November 10, 2019 15:15
Show Gist options
  • Save craftybones/1cc91ef4616b79c86d312da2a76b7832 to your computer and use it in GitHub Desktop.
Save craftybones/1cc91ef4616b79c86d312da2a76b7832 to your computer and use it in GitHub Desktop.
const repeatedly = function* (x) {
while(true) {
yield x;
}
}
const cycle = function* (list) {
while(true) {
for (x of list) {
yield x;
}
}
}
const take = function(times,generator) {
let x=generator.next();
let result=[];
while(!x.done && times>0) {
result.push(x.value);
x = generator.next();
times--;
}
return result;
}
take(10,repeatedly(100));
take(20,cycle(['a','b','c']));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment