Today I was wondering what would be the swiftest method to provide a cycle-through array in TypeScript, as in:
['one', 'two', 'three']
where the next value after three
would be one
, and I thought that it's a good candidate for a generator function. However it does not seem to work for me. What's wrong with the following code?
function* stepGen(){
const steps = ['one', 'two', 'three'];
let index = 0;
if(index < steps.length - 1){
index++;
} else {
index = 0;
}
yield steps[index];
}
let gen = stepGen();
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value); // should be 'three'
console.log(gen.next().value); // should be 'one'
console.log(gen.next().value);
You need a loop in your generator code, otherwise there is only one yield
happening:
function* stepGen(steps){
let index = 0;
while (true) {
yield steps[index];
index = (index+1)%steps.length;
}
}
let gen = stepGen(['one', 'two', 'three']); // pass array to make it more reusable
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
Alternatively you can also use yield*
which yields values from an iterable, one by one:
function* stepGen(steps){
while (true) yield* steps;
}
let gen = stepGen(['one', 'two', 'three']);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With