Skip to content

Instantly share code, notes, and snippets.

@3dvkr
Created September 5, 2022 14:31
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 3dvkr/39f18f3fc7a2623c4db2588c1cbfb072 to your computer and use it in GitHub Desktop.
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.
/* ---
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