Skip to content

Instantly share code, notes, and snippets.

@mattdeboard
Created February 22, 2019 18:20
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 mattdeboard/3bd8294024f0c00bca4ffcc7b997218a to your computer and use it in GitHub Desktop.
Save mattdeboard/3bd8294024f0c00bca4ffcc7b997218a to your computer and use it in GitHub Desktop.
Cycle through data infinitely. Optionally specify a starting index.
function* cycleFrom(elements, startIndex = 0) {
let index = startIndex;
while (true) {
if (index >= elements.length) {
index = index % elements.length;
}
yield elements[index];
index += 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment