Created
August 12, 2019 07:58
-
-
Save gpiancastelli/db214f4f1eaac54f901bdb107e6757bf to your computer and use it in GitHub Desktop.
A cyclic generator for values in an iterable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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