In this implementation, we use a closure to keep track of the current index in the values array. Each time the returned function is called, it returns the current value and increments the index, wrapping around to the beginning of the array when it reaches the end with the help of the modulo operator.
/**
* @template T
* @param {...T} values
*
* @returns () => T
*/
exportdefaultfunctioncycle(...values){
let index =0;
return()=>{
const currentValue = values[index];
index =(index +1)% values.length;
return currentValue;
};
}
An alternative shorter solution that increments before returning the value:
exportdefaultfunction cycle<T>(...values: Array<T>): () => T {
let index =-1;
return()=>{
index =(index +1)% values.length;
return values[index];
};
}
Edge Cases
The function should not be called without any arguments.