Created
February 12, 2024 10:06
-
-
Save tobiamos/be70d8c7a1fe887a036656e307caca71 to your computer and use it in GitHub Desktop.
a function that produces a generator that produces 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
/** | |
* Generates a range of values from `from` to `to`. | |
* @param {number} from - The starting value of the range. | |
* @param {number} to - The ending value of the range. | |
* @returns {function} - A generator function that produces values in the range. | |
*/ | |
const fromTo = (from, to) => { | |
let array = []; | |
for (let i = from; i < to; i++) { | |
array.push(i); | |
} | |
let currentIndex = 0; | |
return () => { | |
const item = array[currentIndex]; | |
if (item === undefined) return null; | |
currentIndex++; | |
return item; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment