Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Some generator fun!
- // WORK IN PROGRESS
- // Only accepts two arguments in the `cb` call for the moment
- "use strict";
- const cbOnPrev = ((reset) => Object.defineProperty(
- function* cbOnPrev (cb) {
- let a = yield; // Replace `yield` with `function.sent` once it's a thing
- let b = yield a;
- while (true) {
- [a, b] = [b, yield cb(a, b)];
- if (b === reset) {
- a = yield;
- b = yield a;
- }
- }
- },
- "reset",
- { value: reset }
- ))(Symbol("reset"));
- // Usage
- // Kinda Fibonacci
- const fibIt = cbOnPrev((n1, n2) => n1 + n2);
- fibIt.next(); // Remove this step once function.sent is a thing
- let fibNum = 1; // Starting at 0 would infini-loop!
- while (fibNum < 100) {
- fibNum = fibIt.next(fibNum).value;
- console.log(fibNum);
- }
- // Close if you want to
- fibIt.return(fibNum);
- // Reset with
- /*
- fibIt.next(cbOnPrev.reset);
- fibIt.next();
- */
Add Comment
Please, Sign In to add comment