Skip to content

Instantly share code, notes, and snippets.

@hyrious
Created July 20, 2020 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyrious/9f0bd1c6678fa5f194b1229b7afaea14 to your computer and use it in GitHub Desktop.
Save hyrious/9f0bd1c6678fa5f194b1229b7afaea14 to your computer and use it in GitHub Desktop.
Generator = Object.getPrototypeOf(function* () {})
const Generator = Object.getPrototypeOf(function* () {});
Generator.prototype.takeWhile = function (pred) {
const result = [];
while (true) {
const a = this.next();
if (a.done || !pred(a.value)) break;
result.push(a.value);
}
return result;
};
function* fib() {
let [a, b] = [0, 1];
while (true) {
[a, b] = [a + b, a];
yield a;
}
}
console.log(fib().takeWhile(e => e < 100));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment