Skip to content

Instantly share code, notes, and snippets.

@laphilosophia
Created July 7, 2020 15:39
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 laphilosophia/171cf2ff2792c896f02921640666b175 to your computer and use it in GitHub Desktop.
Save laphilosophia/171cf2ff2792c896f02921640666b175 to your computer and use it in GitHub Desktop.
/**
* @param {number} range
*/
function* generator (range) {
let i = 0
while (i < range) {
i += 1
yield i
}
}
/**
* @param {string | any[]} array
*/
function* each (array) {
if (!Array.isArray(array)) return
if (!array.length || array.length === 1) return
let index = 0
while (index < array.length) {
yield array[index++]
}
}
const gen1 = generator(10)
const gen2 = each([10, 1, 4, 6, 124, 56, 3, 6])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment