Skip to content

Instantly share code, notes, and snippets.

@gpiancastelli
Created August 12, 2019 07:58
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 gpiancastelli/db214f4f1eaac54f901bdb107e6757bf to your computer and use it in GitHub Desktop.
Save gpiancastelli/db214f4f1eaac54f901bdb107e6757bf to your computer and use it in GitHub Desktop.
A cyclic generator for values in an iterable
// implementation ported from
// https://docs.python.org/3/library/itertools.html#itertools.cycle
function* cycle<T>(iterable: Iterable<T>) {
const saved: T[] = []
for (let element of iterable) {
yield element
saved.push(element)
}
while (saved.length > 0) {
for (let element of saved) {
yield element
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment