itertools

This is the base definition of main Iterator types

Source:

Methods

(inner) new_iter_dict(object, next_key, get, has_next) → {IteratorDict}

Source:

Helper function for creating IteratorDict

Parameters:
Name Type Description
object Any
next_key Method
get Method
has_next Method
Returns:
Type
IteratorDict

(inner) new_iter_collection(object, get, len) → {IteratorCollection}

Source:

Helper function for building IteratorCollection

Parameters:
Name Type Description
object Any
get Method(key)
len Method()
Returns:
Type
IteratorCollection

(inner) iter_tee(iterable, nopt) → {Array}

Source:

returns n independent iterators from single iterable. Once _tee() has made a split, the original iterable should not be used anywhere else; otherwise, the iterable could get advanced without the tee objects being informed.

Example
var t = iter_tee( range( 7 ) );
iter_zip( _t[0], iter_drop( 1, _t[ 1 ] ) ) --> [ 0, 1 ], [ 1, 2 ], [ 2, 3 ], [ 3, 4 ], [ 4, 5 ], [ 5, 6 ]
Parameters:
Name Type Attributes Default Description
iterable Iterable
n Number <optional>
2
Returns:

Array containing n Iterators.

Type
Array

(inner) ds_list_iter(list) → {IteratorCollection}

Source:

Returns iterator object for ds_list data structure.

Parameters:
Name Type Description
list ds_list
Returns:
Type
IteratorCollection

(inner) ds_stack_iter(stack) → {Iterator}

Source:

Returns iterator object for ds_stack data structure.

Parameters:
Name Type Description
stack ds_stack
Returns:
Type
Iterator

(inner) ds_queue_iter(queue) → {Iterator}

Source:

Returns iterator object for ds_queue data structure.

Parameters:
Name Type Description
queue ds_queue
Returns:
Type
Iterator

(inner) ds_map_iter(map) → {IteratorDict}

Source:

Returns iterator object for ds_queue data structure.

Parameters:
Name Type Description
map ds_map
Returns:
Type
IteratorDict

(inner) ds_priority_max_iter(priority) → {Iterator}

Source:

Returns iterator object for ds_priority data structure.

Parameters:
Name Type Description
priority ds_priority
Returns:

Yields max priority elements

Type
Iterator

(inner) ds_priority_min_iter(priority) → {Iterator}

Source:

Returns iterator object for ds_priority data structure.

Parameters:
Name Type Description
priority ds_priority
Returns:

Yields min priority elements

Type
Iterator

(inner) is_iterable(object) → {Bool}

Source:

Returns if object is iterable.

Parameters:
Name Type Description
object Iterable
Returns:
Type
Bool

(inner) iter(object, sentinelopt) → {Iterator}

Source:

Creates Iterator from provided Iterable.

Parameters:
Name Type Attributes Description
object Iterable
sentinel Any <optional>

used if object is method

Returns:
Type
Iterator

(inner) imap(function) → {Iterator}

Source:
See:
  • imap_from_iterable

Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted.

Example
_imap( function( x, n ) { return power( x, n ) }, [ 2, 3, 10 ], [ 5, 2, 3 ] ) --> 32, 9, 1000
Parameters:
Name Type Attributes Description
function Method
... Iterable <optional>
Returns:

Yields result of passing an emement of every argument into a function.

Type
Iterator

(inner) imap_from_iterable(function, iterable) → {Iterator}

Source:

Make an iterator that computes the function using arguments obtained from the iterable. Used instead of map() when argument parameters are already grouped in arrays from a single iterable ( the data has been “pre-zipped” ).

Example
imap_from_iterable( function( x, n ) { return power( x, n ) }, [ [ 2, 5 ], [ 3, 2 ], [ 10, 5 ] ] ) --> 32, 9, 1000
Parameters:
Name Type Description
function Method
iterable Iterable
Returns:

Yields result of passing every emement from iterable into the function.

Type
Iterator

(inner) islice(iterable, startopt, stop, stepopt) → {Iterator}

Source:

Make an iterator that returns selected elements from the iterable. If start is non-zero, then elements from the iterable are skipped until start is reached. Afterward, elements are returned consecutively unless step is set higher than one which results in items being skipped. If stop is undefined, then iteration continues until the iterator is exhausted, if at all; otherwise, it stops at the specified position.

Example
islice( "ABCDEFG", 2 ) --> "A", "B"
islice( "ABCDEFG", 2, 4 ) --> "C", "D"
islice( "ABCDEFG", 2, undefined ) --> "C", "D", "E", "F", "G"
islice( "ABCDEFG", 0, undefined, 2 ) --> "A", "C", "E", "G"
Parameters:
Name Type Attributes Default Description
iterable Iterable
start Number <optional>
0
stop Number
step Number <optional>
1
Returns:

Yields only elements from range.

Type
Iterator

(inner) iter_accumulate(iterable, funcopt, initialopt) → {Iterator}

Source:
See:
  • iter_reduce

Make an iterator that returns accumulated sums, or accumulated results of other binary functions (specified via the optional func argument).

If func is supplied, it should be a function of two arguments. Elements of the input iterable may be any type that can be accepted as arguments to func.

Usually, the number of elements output matches the input iterable. However, if the optional argument initial is provided, the accumulation leads off with the initial value so that the output has one more element than the input iterable.

Examples
iter_accumulate( [ 1, 2, 3, 4, 5 ] ) --> 1, 3, 6, 10, 15
iter_accumulate( [ 1, 2, 3, 4, 5 ], undefined, 100 ) --> 101, 103, 106, 110, 115

 
data = [ 3, 4, 6, 2, 1, 9, 0, 7, 5, 8 ];
iter_accumulate( data, _max ).to_array()
--> [ 3, 4, 6, 6, 6, 9, 9, 9, 9, 9 ]
Parameters:
Name Type Attributes Description
iterable Iterable
func Method( a, b ) <optional>
initial Any <optional>
Returns:

Yields accumulated sums.

Type
Iterator

(inner) iter_chain() → {Iterator}

Source:
See:
  • iter_chain_from_iterable

Make an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted. Used for treating consecutive sequences as a single sequence.

Example
iter_chain( "ABC", "DEF" ) --> "A", "B", "C", "D", "E", "F"
Parameters:
Name Type Attributes Description
... Iterable <optional>
Returns:

Yields chained elements of input iterables.

Type
Iterator

(inner) iter_chain_from_iterable(iterable)

Source:
See:
  • iter_chain

Make an iterator that returns chained elements from iterables returned by argument iterable

Example
iter_chain_from_iterable( [ "ABC", "DEF" ] ) --> "ABC", "DEF"
iter_chain_from_iterable( [ "ABC", "DEF" ] ) --> "A", "B", "C", "D", "E", "F"
Parameters:
Name Type Description
iterable Iterable

(inner) iter_compress(iterable, selectorsopt) → {Iterator}

Source:

Make an iterator that filters elements from iterable returning only those that have a corresponding element in selectors that evaluates to True. Stops when either the data or selectors iterables has been exhausted.

Example
_compress( "ABCDEF", [ 1, 0, 1, 0, 1, 1 ] ) --> "A", "C", "E", "F"
Parameters:
Name Type Attributes Description
iterable Iterable
selectors Iterable <optional>
Returns:

Yields matching elements.

Type
Iterator

(inner) iter_count(startopt, stepopt) → {Generator}

Source:

Make an iterator that returns evenly spaced values starting with number start.

Example
iter_count( 10 ) --> 10, 11, 12, 13, 14, ...
iter_count( 2.5, 0.5 ) --> 2.50, 3, 3.50 ...
Parameters:
Name Type Attributes Default Description
start Number <optional>
0
step Number <optional>
1
Returns:

Infinitely yields numbers start, start + step, start + 2 * step, ...

Type
Generator

(inner) iter_cycle(iterable) → {Iterator}

Source:

Make an iterator returning elements from the iterable and saving a copy of each. When the iterable is exhausted, return elements from the saved copy. Repeats indefinitely.

Example
iter_cycle( [ 1, 2, 3, 4 ] ) --> 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, ...
Parameters:
Name Type Description
iterable Iterable
Returns:

Yields elements of the input iterable cycled.

Type
Iterator

(inner) iter_drop(n, iterable) → {Iterator}

Source:
See:
  • iter_take

Helper function for partially exhausting a long or infinite iterable

Example
iter_drop( 2, "abcdef" ) --> "b", "c", "d", "e", "f"
Parameters:
Name Type Description
n Number
iterable Iterable
Returns:

Yield elements from input iterable starting from n.

Type
Iterator

(inner) iter_dropwhile(iterable, predicateopt) → {Iterator}

Source:
See:
  • _takewhile

Returns elements from iterable starting from the element for which predicate is false

Example
iter_dropwhile( [ 1, 4, 6, 4, 1 ], function( x ) { return x < 5 } ) --> 6, 4, 1
Parameters:
Name Type Attributes Description
iterable Iterable
predicate Method <optional>
Returns:

Yields elements input iterable starting from the element for which predicate is false.

Type
Iterator

(inner) iter_enumerate(iterable, startopt) → {Iterator}

Source:

Returns [ count, element ] for each element from iterable

Example
seasons = [ "Spring", "Summer", "Fall", "Winter" ];
iter_enumerate( seasons ) --> [ 0, "Spring" ], [ 1, "Summer" ], [ 2, "Fall" ], [ 3, "Winter" ]
iter_enumerate( seasons, 1 ) --> [ 1, "Spring" ], [ 2, "Summer" ], [ 3, "Fall" ], [ 4, "Winter" ]
Parameters:
Name Type Attributes Default Description
iterable Iterable
start Number <optional>
0
Returns:

Yields array with count and the next value from input iterable.

Type
Iterator

(inner) iter_ndenumerate(array) → {Iterator}

Source:

Return an iterator yielding pairs of array coordinates and values.

Example
iter_ndenumerate( [ [ 0, 1 ], [ 2, 3 ] ] ) --> [ [ 0,0 ], 0 ], [ [ 0,1 ], 1 ], [ [ 1,0 ], 2 ], [ [ 1,1 ], 3 ]
Parameters:
Name Type Description
array Array
Returns:

Yields pairs of array coordinates and values.

Type
Iterator

(inner) iter_filter(iterable, functionopt) → {Iterator}

Source:
See:
  • iter_filter_false

Construct an iterator from those elements of iterable for which function returns true.

Example
iter_filter( _range( 10 ), function( x ) { return x % 2 } ) --> 1, 3, 5, 7, 9
Parameters:
Name Type Attributes Description
iterable Iterable
function Method(e) <optional>
Returns:

Yields elements from iterable for which function returns true.

Type
Iterator

(inner) iter_filter_false(iterable, functionopt)

Source:
See:
  • iter_filter

Construct an iterator from those elements of iterable for which function returns false.

Example
iter_filter_false( _range( 10 ), function( x ) { return x % 2 } ) --> 0, 2, 4, 6, 8
Parameters:
Name Type Attributes Description
iterable Iterable
function Method <optional>

(inner) iter_group_by(iterable, keyopt) → {Iterator}

Source:

Make an iterator that returns consecutive keys and groups from the iterable.

Example
iter_take( 2, iter_group_by( "AAAABBBCCDAABBB" ) ) --> { key: "A", group: [ "A", "A", "A", "A" ] }, { key: "B", group: [ "B", "B", "B" ] }
Parameters:
Name Type Attributes Description
iterable Iterable
key Method <optional>

Function computing a key value for each element. If not specified or is undefined, key defaults to an identity function and returns the element unchanged. Generally, the iterable needs to already be sorted on the same key function.

Returns:

Yields struct with key and array group for each group.

Type
Iterator

(inner) iter_repeat(object, nopt) → {Iterator}

Source:

Iterator that returns object over and over again.

Examples
iter_repeat( 10, 3 ) --> 10, 10, 10
iter_repeat( 10 ) --> 10, 10, 10, 10, 10, ...

 
imap( function( x, n ) { return power(n) }, range( 10 ), iter_repeat( 2 ) ).to_array()
--> [ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 ]
Parameters:
Name Type Attributes Description
object Any
n Number <optional>

If specified, iterator executes this amount of times.

Returns:

Yields object n times.

Type
Iterator

(inner) iter_sorted(iterable, keyopt, reverseopt) → {Iterator}

Source:

Returns Iterator that yields items from iterable in sorted order

Example
iter_sorted( [ 5, 2, 3, 1, 4 ] ) --> 1, 2, 3, 4, 5
 iter_sorted( "ebcad" ).to_string() --> "abcde"
Parameters:
Name Type Attributes Default Description
iterable Iterable
key Method <optional>
reverse Bool <optional>
false
Returns:
Type
Iterator

(inner) iter_take(n, iterable) → {Iterator}

Source:
See:
  • iter_drop

Helper function for partially consuming a long or infinite iterable

Example
iter_take( 5, _count() ) --> 0, 1, 2, 3, 4
iter_take( 7, _repeat( [ 1, 2, 3 ] ) --> 1, 2, 3, 1, 2, 3, 1
Parameters:
Name Type Description
n Number
iterable Iterable
Returns:

Yields next n elements from iterable.

Type
Iterator

(inner) iter_takewhile(iterable, predicate) → {Iterator}

Source:
See:
  • iter_dropwhile

Make an iterator that returns elements from the iterable as long as the predicate is true.

Example
iter_takewhile( [ 1, 4, 6, 4, 1 ], function( x ) { return x < 5 } ) --> 1, 4
Parameters:
Name Type Description
iterable Iterable
predicate Method
Returns:

Yields elements from iterable

Type
Iterator

(inner) iter_zip() → {Iterator}

Source:
See:
  • iter_zip_longest

Iterator that aggregates elements from each of the iterables until one of them is exhausted.

Example
iter_zip( "ABCD", "xy" ) --> [ "A", "x" ], [ "B", "y" ]
Parameters:
Name Type Attributes Description
... Iterable <optional>
Returns:

Yields an array with elements of every iterable.

Type
Iterator

(inner) iter_zip_longest(fill_value) → {Iterator}

Source:
See:
  • iter_zip

Iterator that aggregates elements from each of the iterables until all of them are exhausted.

Example
iter_zip_longest( "ABCD", "xy", "-" ) --> [ "A", "x" ], [ "B", "y" ], [ "C", "-" ], [ "D", "-" ]
Parameters:
Name Type Attributes Description
... Iterable <optional>
fill_value Any
Returns:

Yields an array with elements of every iterable.

Type
Iterator

(inner) iter_combinations(iterable, ropt) → {Iterator}

Source:

Iterator that yields r length subsequences of elements from the input iterable. Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each combination.

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

(inner) iter_combinations_with_replacements(iterable, 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
iterable Iterable
r Number <optional>
Returns:
Type
Iterator

(inner) iter_permutations(iterable, ropt) → {Iterator}

Source:

Iterator that yields subsequent r-length permutations of input iterable items.

Example
iter_permutations( [ 0, 1, 2 ], 2 ) -->  [ 0,1 ],[ 0,2 ],[ 1,0 ],[ 1,2 ],[ 2,0 ],[ 2,1 ]
Parameters:
Name Type Attributes Description
iterable Iterable
r Number <optional>
Returns:
Type
Iterator

(inner) iter_product(repeatopt) → {Iterator}

Source:

Cartesian product of input iterables

Example
iter_product( [ 0, 1 ], "ab" ) --> [ 0, "a" ], [ 0, "b" ], [ 1, "a" ], [ 1, "b" ]
iter_product( [ 0, 1 ], 2 ) --> [ 0, 0 ], [ 0, 1 ], [ 1, 0 ], [ 1, 1 ]
Parameters:
Name Type Attributes Description
... Iterable
repeat Number <optional>

if supplied, computes product of single iterable with itself

Returns:

Yields array with elements of each iterable

Type
Iterator

(inner) iter_all(iterable) → {Bool}

Source:
See:
  • iter_any

Return True if all elements of the iterable are true (or if the iterable is empty).

Parameters:
Name Type Description
iterable Iterable
Returns:
Type
Bool

(inner) iter_any(iterable) → {Bool}

Source:
See:
  • iter_all

Return true if any element of the iterable is true. If the iterable is empty, return false.

Parameters:
Name Type Description
iterable Iterable
Returns:
Type
Bool

(inner) iter_reduce(iterable, function, Anyopt) → {Any}

Source:
See:
  • iter_accumulate

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

Example
iter_reduce( [1, 2, 3, 4], max ) --> 4
Parameters:
Name Type Attributes Description
iterable Iterable
function Method(a,x)
Any <optional>

initializer

Returns:
Type
Any

(inner) iter_unique(iterable, keyopt) → {Iterator}

Source:

returns Iterator that yields the non-repeating items from iterable sorted by key

Parameters:
Name Type Attributes Description
iterable Iterable
key Method <optional>
Returns:
Type
Iterator