Skip to content

Instantly share code, notes, and snippets.

@tobiamos
Created February 12, 2024 10:06
Show Gist options
  • Save tobiamos/be70d8c7a1fe887a036656e307caca71 to your computer and use it in GitHub Desktop.
Save tobiamos/be70d8c7a1fe887a036656e307caca71 to your computer and use it in GitHub Desktop.
a function that produces a generator that produces values in a range.
/**
* 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