Skip to content

Instantly share code, notes, and snippets.

@slikts
Last active May 22, 2016 10:44
Show Gist options
  • Save slikts/486ec32f43ed3da56179d9e2c36a2d43 to your computer and use it in GitHub Desktop.
Save slikts/486ec32f43ed3da56179d9e2c36a2d43 to your computer and use it in GitHub Desktop.
function* cycle(list) {
const arr = [...list]
let i = 0
for (;;) {
i = (i + ((yield arr[i]) || 1)) % arr.length
}
}
const it = cycle(['a', 'b', 'c'])
console.log(...[it.next(), it.next(), it.next(), it.next(), it.next(2), it.next(-1), it.next(-1)].map(x => x.value)) // a b c a c b a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment