CycleLoading saved progress…

Cycle

Implement cycle(...values) — given several values, return a function that hands back the next value every time you call it, looping forever once it runs off the end. Think of a playlist set to repeat: track 1, track 2, track 3, then back to track 1. The values you pass are fixed; the position advances on each call and wraps around. Each call to cycle(...) produces its own independent stepper — calling one never affects another.

Signature

// values: any[]                 — zero or more values to rotate through.
// returns: () => any
//   A function. Each call returns the next value in order, wrapping
//   from the last value back to the first. With NO values passed,
//   the returned function returns undefined on every call.
function cycle(...values): () => any;

Examples

const next = cycle('a', 'b', 'c');
next(); // → 'a'
next(); // → 'b'
next(); // → 'c'
next(); // → 'a'  (wrapped back to the start)
// Two independent cyclers — they do not share a position.
const a = cycle(1, 2);
const b = cycle(1, 2);
a(); // → 1
a(); // → 2
b(); // → 1  (b started fresh; a's calls didn't move it)

Notes

  • The returned value is what you passed in. Objects come back by reference — cycle(obj)() returns the same obj, not a copy.
  • Any value type works. Numbers, strings, objects, null, and undefined are all valid rotation members and must come back unchanged.
  • Each cycle(...) call is independent. Two cyclers built from separate calls track separate positions; interleaving calls to them must not interfere.
  • No values is allowed. cycle() returns a function that returns undefined on every call and never throws.
  • The rotation never ends. There's no "done" state — call it forever and it keeps wrapping.
Loading editor…