Created
July 20, 2020 13:36
-
-
Save hyrious/9f0bd1c6678fa5f194b1229b7afaea14 to your computer and use it in GitHub Desktop.
Generator = Object.getPrototypeOf(function* () {})
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
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