Created
September 5, 2022 14:31
-
-
Save 3dvkr/39f18f3fc7a2623c4db2588c1cbfb072 to your computer and use it in GitHub Desktop.
A function fromTo that produces a generator, that will produce values in a range.
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
/* --- | |
Write a function fromTo that produces a generator, that will produce values in a range. | |
Source: https://buttondown.email/cassidoo/archive/if-everything-was-perfect-you-would-never-learn/ | |
*/ | |
function fromTo(start, end) { | |
let value = start | |
return function () { | |
if (value <= end) { | |
return value++ | |
} | |
} | |
} | |
/* | |
let gen = fromTo(5, 7) | |
console.log(gen()) // 5 | |
console.log(gen()) // 6 | |
console.log(gen()) // 7 | |
console.log(gen()) // undefined | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment