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, so it will just continue
return false;
}
};
let values: number[] = [];
let theIterable: Iterable<T> = makeIterable({}, fibCtx);
iterForOf(theIterable, (value) => {
values.push(value);
if (values.length === 10) {
return -1;
}
});
// Values: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Adds or replaces an iterable implementation that conforms to the
Iterable
protocol to the target instance, it uses the providedctx
to create anIterator
via createIterator.