Skip to content

Instantly share code, notes, and snippets.

View luishendrix92's full-sized avatar

Luis Felipe López G. luishendrix92

View GitHub Profile
@luishendrix92
luishendrix92 / cyclic.js
Last active January 31, 2020 19:44
Cyclic Iterator (with ES6 Generators) and .take()
/*
Say I need to iterate over [1, 2, 3], yielding a value
at a time but once I reach the final value, I want to
keep receiving the values over and over again, making
it a repeated sequence: 1, 2, 3, 1, 2, 3, 1 and so on...
*/
const cyclic = list => (function* () {
while (true) for (let item of list) yield item
}())