Iterator
An Iterator
value is something that can be used to get a sequence of other values by abstracting the actual way used to calculate them.
Getting iterators
An Iterator
value can't be created directly, but it can be create from another value by using the iterator operator >
:
If the value we are using the iterator operator on doesn't provide an iterator, the operator will return null
:
Advancing iterators
The next operator *
can be used on Iterator
values to get the current value and advance the Iterator
to the next value.
The next operator returns an object with two keys: value
and done
. The value
property will contain the current value and the done
property will be true
if there are no more values left for the iterator to generate:
Outputs:
note
The use of a separate done
value allows for Iterators
to also return null
values without any ambiguity.
Cycling
Iterator
values can be used in conjunction with the for in
loop to iterate over the values of a finite Iterator
:
Outputs: