Iterator

Iterator

An object representing a stream of data. Repeated calls to the iterator’s next() method return successive items in the stream. When no more data are available returns undefined. At this point, the iterator object is exhausted and any further calls to its next() method just return undefined again. Iterators are required to have an __iter() method that returns the iterator object itself so every iterator is also iterable and may be used in most places where other iterables are accepted. One notable exception is code which attempts multiple iteration passes. A container object (such as a list) produces a fresh new iterator each time you pass it to the iter() function or use it in a for loop. Attempting this with an iterator will just return the same exhausted iterator object used in the previous iteration pass, making it appear like an empty container.

Constructor

new Iterator(data, next, has_next) → {Iterator}

Source:
Parameters:
Name Type Description
data Any
next Method()
has_next Method()
Returns:
Type
Iterator

Extends

Methods

(static) has_next() → {Bool}

Source:

Returns true when Iterator is exhausted.

Returns:
Type
Bool

(static) combinations(ropt) → {Iterator}

Source:

Make an iterator that yields r length subsequences of elements from the iterable.

Example
irange( 4 ).combinations( 3 ) --> [ 0, 1, 2 ], [ 0, 1, 3 ], [ 0, 2, 3 ], [ 1, 2, 3 ]
Parameters:
Name Type Attributes Description
r Number <optional>
Returns:
Type
Iterator

(static) combinations_with_replacements(ropt) → {Iterator}

Source:

Make an iterator that yields r length subsequences of the iterable allowing individual elements to be repeated more than once.

Example
irange( 3 ).combinations_with_replacements( 2 ) --> [ 0, 0 ], [ 0, 1 ], [ 0, 2 ], [ 1, 1 ], [ 1, 2 ], [ 2, 2 ]
Parameters:
Name Type Attributes Description
r Number <optional>
Returns:
Type
Iterator

(static) permutations(ropt) → {Iterator}

Source:

Return an iterator that yields r length permutations.

Example
irange( 3 ).permutations( 2 ) -->  [ 0,1 ], [ 0,2 ], [ 1,0 ], [ 1,2 ], [ 2,0 ], [ 2,1 ]
Parameters:
Name Type Attributes Description
r Number <optional>

If r is not specified or is indefined, then r defaults to the length of the iterable and all possible full-length permutations are generated.

Returns:
Type
Iterator

(static) product(repeats) → {Iterator}

Source:

Return an iterator that yields Cartesian product of items from this Iterator with itself repeats times.

Example
iter( [ 0, 1 ] ).product( 3 ) --> [ 0, 0, 0 ], [ 0, 0, 1 ], [ 0, 1, 0 ], [ 0, 1, 1 ], [ 1, 0, 0 ] ...
Parameters:
Name Type Description
repeats Number
Returns:
Type
Iterator

(static) reduce(function, Anyopt) → {Any}

Source:

Apply function of two arguments cumulatively to the items of Iterator, from left to right, so as to reduce it to a single value.

Example
iter( [ 1, 2, 3, 4 ] ).reduce( function( _a, _x ) { return _a + _x; } ) --> 10
Parameters:
Name Type Attributes Description
function Method(a,x)
Any <optional>

initializer

Returns:
Type
Any

(static) sorted(keyopt, reverseopt)

Source:

Returns a new Iterator that yields items in iterable sorted

Example
iter( [ 5, 2, 3, 1, 4 ] ).sorted() --> 1, 2, 3, 4, 5
Parameters:
Name Type Attributes Default Description
key Method <optional>
reverse Bool <optional>
false

(static) to_array() → {Array}

Source:

Exhausts iterator and combines all of its elements into an array

Example
iter( "1234" ).to_array() --> [ "1", "2", "3", "4" ]
Returns:
Type
Array

(static) to_string(separatoropt) → {String}

Source:

Exhausts iterator and combines all of its elements into a string.

Example
iter( [ 1, 2, 3, 4 ] ).to_string() --> "1234"
Parameters:
Name Type Attributes Default Description
separator String <optional>
""
Returns:
Type
String

(static) unique(keyopt) → {Iterator}

Source:

Returns a new Iterator that yields non-repeating items from sorted iterable

Example
iter( "abcacbacbacbac" ).unique().to_string() --> "abc"
Parameters:
Name Type Attributes Default Description
key Method <optional>
true
Returns:
Type
Iterator