The context used to manage the iteration over the items.
A new Iterable instance
let current = 0;
let next = 1;
let done = false;
let fibCtx: CreateIteratorContext<number> = {
n: function() {
fibCtx.v = current;
current = next;
next = fibCtx.v + next;
// Return not done
return false;
},
r: function(value) {
done = true;
return value;
}
};
let values: number[] = [];
iterForOf(createIterable(fibCtx), (value) => {
values.push(value);
if (values.length === 10) {
return -1;
}
});
// Done is true
// done === true
// Values: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Create an iterable which conforms to the
Iterable
protocol, it uses the providedctx
to create anIterator
via createIterator.