# Namespace goog.iter

code »

## Classes

goog.iter.GroupByIterator_
  Implements the `goog.iter.groupBy` iterator.
goog.iter.Iterator
  Class/interface for iterators.

Show:PublicProtectedPrivate

## Type Definitions

code »goog.iter.Iterable : `(goog.iter.Iterator|{length: number}|{__iterator__: ?})`
No description.

## Global Functions

code » goog.iter.accumulate ( iterable ) ⇒ `!goog.iter.Iterator.<number>`

Creates an iterator that returns running totals from the numbers in `iterable`. For example, the array `[1, 2, 3, 4, 5]` yields `1 -> 3 -> 6 -> 10 -> 15`.

Parameters

iterable: `!goog.iter.Iterable.<number>`
  The iterable of numbers to accumulate.

Returns

A new iterator that returns the numbers in the series.

code » `<VALUE>` goog.iter.chain ( var_args ) ⇒ `!goog.iter.Iterator.<VALUE>`

Takes zero or more iterables and returns one iterator that will iterate over them in the order chained.

Parameters

var_args: `...(!goog.iter.Iterator.<VALUE>|!goog.iter.Iterable)`
  Any number of iterable objects.

Returns

Returns a new iterator that will iterate over all the given iterables' contents.

code » `<VALUE>` goog.iter.chainFromIterable ( iterable ) ⇒ `!goog.iter.Iterator.<VALUE>`

Takes a single iterable containing zero or more iterables and returns one iterator that will iterate over each one in the order given.

Parameters

iterable: `goog.iter.Iterable`
  The iterable of iterables to chain.

Returns

Returns a new iterator that will iterate over all the contents of the iterables contained within `iterable`.

code » `<VALUE>` goog.iter.combinations ( iterable, length ) ⇒ `!goog.iter.Iterator`

Creates an iterator that returns combinations of elements from `iterable`. Combinations are obtained by taking the goog.iter#permutations of `iterable` and filtering those whose elements appear in the order they are encountered in `iterable`. For example, the 3-length combinations of `[0,1,2,3]` are `[[0,1,2], [0,1,3], [0,2,3], [1,2,3]]`.

Parameters

iterable: `(!goog.iter.Iterator.<VALUE>|!goog.iter.Iterable)`
  The iterable from which to generate combinations.
length: `number`
  The length of each combination.

Returns

A new iterator containing combinations from the `iterable`.

code » `<VALUE>` goog.iter.combinationsWithReplacement ( iterable, length ) ⇒ `!goog.iter.Iterator`

Creates an iterator that returns combinations of elements from `iterable`, with repeated elements possible. Combinations are obtained by taking the Cartesian product of `length` iterables and filtering those whose elements appear in the order they are encountered in `iterable`. For example, the 2-length combinations of `[1,2,3]` are `[[1,1], [1,2], [1,3], [2,2], [2,3], [3,3]]`.

Parameters

iterable: `(!goog.iter.Iterator.<VALUE>|!goog.iter.Iterable)`
  The iterable to combine.
length: `number`
  The length of each combination.

Returns

A new iterator containing combinations from the `iterable`.

code » `<VALUE>` goog.iter.compress ( iterable, selectors ) ⇒ `!goog.iter.Iterator.<VALUE>`

Creates an iterator that filters `iterable` based on a series of `selectors`. On each call to `next()`, one item is taken from both the `iterable` and `selectors` iterators. If the item from `selectors` evaluates to true, the item from `iterable` is given. Otherwise, it is skipped. Once either `iterable` or `selectors` is exhausted, subsequent calls to `next()` will throw `goog.iter.StopIteration`.

Parameters

iterable: `(!goog.iter.Iterator.<VALUE>|!goog.iter.Iterable)`
  The iterable to filter.
selectors: `(!goog.iter.Iterator.<VALUE>|!goog.iter.Iterable)`
  An iterable of items to be evaluated in a boolean context to determine if the corresponding element in `iterable` should be included in the result.

Returns

A new iterator that returns the filtered values.

code » `<VALUE>` goog.iter.consume ( iterable, count ) ⇒ `!goog.iter.Iterator.<VALUE>`

Creates an iterator that is advanced `count` steps ahead. Consumed values are silently discarded. If `count` is greater than the number of elements in `iterable`, an empty iterator is returned. Subsequent calls to `next()` will throw `goog.iter.StopIteration`.

Parameters

iterable: `(!goog.iter.Iterator.<VALUE>|!goog.iter.Iterable)`
  The iterable to consume.
count: `number`
  The number of elements to consume from the iterator.

Returns

An iterator advanced zero or more steps ahead.

code » goog.iter.count ( opt_start, opt_step ) ⇒ `!goog.iter.Iterator.<number>`

Creates an iterator that counts indefinitely from a starting value.

Parameters

opt_start: `number=`
  The starting value. Default is 0.
opt_step: `number=`
  The number to increment with between each call to next. Negative and floating point numbers are allowed. Default is 1.

Returns

A new iterator that returns the values in the series.

code » `<VALUE>` goog.iter.cycle ( iterable ) ⇒ `!goog.iter.Iterator.<VALUE>`

Create an iterator to cycle over the iterable's elements indefinitely. For example, ([1, 2, 3]) would return : 1, 2, 3, 1, 2, 3, ...

Parameters

iterable: `(!goog.iter.Iterator.<VALUE>|!goog.iter.Iterable)`
  The iterable object.

Returns

An iterator that iterates indefinitely over the values in `iterable`.

code » `<THIS, VALUE>` goog.iter.dropWhile ( iterable, f, opt_obj ) ⇒ `!goog.iter.Iterator.<VALUE>`

Builds a new iterator that iterates over the original, but skips elements as long as a supplied function returns true.

Parameters

iterable: `(goog.iter.Iterator.<VALUE>|goog.iter.Iterable)`
  The iterator object.
f: `function(this: THIS, VALUE, undefined, goog.iter.Iterator.<VALUE>): boolean`
  The function to call for every value. This function takes 3 arguments (the value, undefined, and the iterator) and should return a boolean.
opt_obj: `THIS=`
  The object to be used as the value of 'this' within `f`.

Returns

A new iterator that drops elements from the original iterator as long as `f` is true.

code » `<VALUE>` goog.iter.enumerate ( iterable, opt_start ) ⇒ `!goog.iter.Iterator`

Creates an iterator that returns arrays containing a count and an element obtained from the given `iterable`.

Parameters

iterable: `(!goog.iter.Iterator.<VALUE>|!goog.iter.Iterable)`
  The iterable to enumerate.
opt_start: `number=`
  Optional starting value. Default is 0.

Returns

A new iterator containing count/item pairs.

code » `<VALUE>` goog.iter.equals ( iterable1, iterable2 ) ⇒ `boolean`

Iterates over two iterables and returns true if they contain the same sequence of elements and have the same length.

Parameters

iterable1: `(!goog.iter.Iterator.<VALUE>|!goog.iter.Iterable)`
  The first iterable object.
iterable2: `(!goog.iter.Iterator.<VALUE>|!goog.iter.Iterable)`
  The second iterable object.

Returns

true if the iterables contain the same sequence of elements and have the same length.

code » `<THIS, VALUE>` goog.iter.every ( iterable, f, opt_obj ) ⇒ `boolean`

Goes through the values in the iterator. Calls f for each of these and if any of them returns false this returns false (without checking the rest). If all return true this will return true.

Parameters

iterable: `(goog.iter.Iterator.<VALUE>|goog.iter.Iterable)`
  The iterator object.
f: `function(this: THIS, VALUE, undefined, goog.iter.Iterator.<VALUE>): boolean`
  The function to call for every value. This function takes 3 arguments (the value, undefined, and the iterator) and should return a boolean.
opt_obj: `THIS=`
  The object to be used as the value of 'this' within `f`.

Returns

true if every value passes the test.

code » `<THIS, VALUE>` goog.iter.filter ( iterable, f, opt_obj ) ⇒ `!goog.iter.Iterator.<VALUE>`

Calls a function for every element in the iterator, and if the function returns true adds the element to a new iterator.

Parameters

iterable: `(goog.iter.Iterator.<VALUE>|goog.iter.Iterable)`
  The iterator to iterate over.
f: `function(this: THIS, VALUE, undefined, goog.iter.Iterator.<VALUE>): boolean`
  The function to call for every element. This function takes 3 arguments (the element, undefined, and the iterator) and should return a boolean. If the return value is true the element will be included in the returned iterator. If it is false the element is not included.
opt_obj: `THIS=`
  The object to be used as the value of 'this' within `f`.

Returns

A new iterator in which only elements that passed the test are present.

code » `<THIS, VALUE>` goog.iter.filterFalse ( iterable, f, opt_obj ) ⇒ `!goog.iter.Iterator.<VALUE>`

Calls a function for every element in the iterator, and if the function returns false adds the element to a new iterator.

Parameters

iterable: `(goog.iter.Iterator.<VALUE>|goog.iter.Iterable)`
  The iterator to iterate over.
f: `function(this: THIS, VALUE, undefined, goog.iter.Iterator.<VALUE>): boolean`
  The function to call for every element. This function takes 3 arguments (the element, undefined, and the iterator) and should return a boolean. If the return value is false the element will be included in the returned iterator. If it is true the element is not included.
opt_obj: `THIS=`
  The object to be used as the value of 'this' within `f`.

Returns

A new iterator in which only elements that did not pass the test are present.

code » `<THIS, VALUE>` goog.iter.forEach ( iterable, f, opt_obj )

Calls a function for each element in the iterator with the element of the iterator passed as argument.

Parameters

iterable: `(goog.iter.Iterator.<VALUE>|goog.iter.Iterable)`
  The iterator to iterate over. If the iterable is an object `toIterator` will be called on it.
f: `(function(this: THIS, VALUE, undefined, goog.iter.Iterator.<VALUE>)|function(this: THIS, number, undefined, goog.iter.Iterator.<VALUE>))`
  The function to call for every element. This function takes 3 arguments (the element, undefined, and the iterator) and the return value is irrelevant. The reason for passing undefined as the second argument is so that the same function can be used in goog.array#forEach as well as others.
opt_obj: `THIS=`
  The object to be used as the value of 'this' within `f`.

code » `<KEY, VALUE>` goog.iter.groupBy ( iterable, opt_keyFunc ) ⇒ `!goog.iter.Iterator`

Creates an iterator that returns arrays containing elements from the `iterable` grouped by a key value. For iterables with repeated elements (i.e. sorted according to a particular key function), this function has a `uniq`-like effect. For example, grouping the array: `[A, B, B, C, C, A]` produces `[A, [A]], [B, [B, B]], [C, [C, C]], [A, [A]]`.

Parameters

iterable: `(!goog.iter.Iterator.<VALUE>|!goog.iter.Iterable)`
  The iterable to group.
opt_keyFunc: `function(VALUE): KEY=`
  Optional function for determining the key value for each group in the `iterable`. Default is the identity function.

Returns

A new iterator that returns arrays of consecutive key and groups.

code » `<VALUE>` goog.iter.hasDuplicates_ ( arr ) ⇒ `boolean`

Checks an array for duplicate elements.

Parameters

arr: `(Array.<VALUE>|goog.array.ArrayLike)`
  The array to check for duplicates.

Returns

True, if the array contains duplicates, false otherwise.

code » `<VALUE>` goog.iter.join ( iterable, deliminator ) ⇒ `string`

Joins the values in a iterator with a delimiter.

Parameters

iterable: `(goog.iter.Iterator.<VALUE>|goog.iter.Iterable)`
  The iterator to get the values from.
deliminator: `string`
  The text to put between the values.

Returns

The joined value string.

code » `<VALUE>` goog.iter.limit ( iterable, limitSize ) ⇒ `!goog.iter.Iterator.<VALUE>`

Creates an iterator that returns the first `limitSize` elements from an iterable. If this number is greater than the number of elements in the iterable, all the elements are returned.

Parameters

iterable: `(!goog.iter.Iterator.<VALUE>|!goog.iter.Iterable)`
  The iterable to limit.
limitSize: `number`
  The maximum number of elements to return.

Returns

A new iterator containing `limitSize` elements.

code » `<THIS, VALUE, RESULT>` goog.iter.map ( iterable, f, opt_obj ) ⇒ `!goog.iter.Iterator.<RESULT>`

For every element in the iterator call a function and return a new iterator with that value.

Parameters

iterable: `(!goog.iter.Iterator.<VALUE>|!goog.iter.Iterable)`
  The iterator to iterate over.
f: `function(this: THIS, VALUE, undefined, !goog.iter.Iterator.<VALUE>): RESULT`
  The function to call for every element. This function takes 3 arguments (the element, undefined, and the iterator) and should return a new value.
opt_obj: `THIS=`
  The object to be used as the value of 'this' within `f`.

Returns

A new iterator that returns the results of applying the function to each element in the original iterator.

code » `<VALUE>` goog.iter.nextOrValue ( iterable, defaultValue ) ⇒ `VALUE`

Advances the iterator to the next position, returning the given default value instead of throwing an exception if the iterator has no more entries.

Parameters

iterable: `(goog.iter.Iterator.<VALUE>|goog.iter.Iterable)`
  The iterable object.
defaultValue: `VALUE`
  The value to return if the iterator is empty.

Returns

The next item in the iteration, or defaultValue if the iterator was empty.

code » `<VALUE>` goog.iter.permutations ( iterable, opt_length ) ⇒ `!goog.iter.Iterator`

Creates an iterator that returns permutations of elements in `iterable`. Permutations are obtained by taking the Cartesian product of `opt_length` iterables and filtering out those with repeated elements. For example, the permutations of `[1,2,3]` are `[[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]`.

Parameters

iterable: `(!goog.iter.Iterator.<VALUE>|!goog.iter.Iterable)`
  The iterable from which to generate permutations.
opt_length: `number=`
  Length of each permutation. If omitted, defaults to the length of `iterable`.

Returns

A new iterator containing the permutations of `iterable`.

code » `<VALUE>` goog.iter.product ( var_args ) ⇒ `!goog.iter.Iterator`

Cartesian product of zero or more sets. Gives an iterator that gives every combination of one element chosen from each set. For example, ([1, 2], [3, 4]) gives ([1, 3], [1, 4], [2, 3], [2, 4]).

Parameters

var_args: `...!goog.array.ArrayLike.<VALUE>`
  Zero or more sets, as arrays.

Returns

An iterator that gives each n-tuple (as an array).

code » goog.iter.range ( startOrStop, opt_stop, opt_step ) ⇒ `!goog.iter.Iterator.<number>`

Creates a new iterator that returns the values in a range. This function can take 1, 2 or 3 arguments:

     range(5) same as range(0, 5, 1)
     range(2, 5) same as range(2, 5, 1)

Parameters

startOrStop: `number`
  The stop value if only one argument is provided. The start value if 2 or more arguments are provided. If only one argument is used the start value is 0.
opt_stop: `number=`
  The stop value. If left out then the first argument is used as the stop value.
opt_step: `number=`
  The number to increment with between each call to next. This can be negative.

Returns

A new iterator that returns the values in the range.

code » `<THIS, VALUE>` goog.iter.reduce ( iterable, f, val, opt_obj ) ⇒ `VALUE`

Passes every element of an iterator into a function and accumulates the result.

Parameters

iterable: `(goog.iter.Iterator.<VALUE>|goog.iter.Iterable)`
  The iterator to iterate over.
f: `function(this: THIS, VALUE, VALUE): VALUE`
  The function to call for every element. This function takes 2 arguments (the function's previous result or the initial value, and the value of the current element). function(previousValue, currentElement) : newValue.
val: `VALUE`
  The initial value to pass into the function on the first call.
opt_obj: `THIS=`
  The object to be used as the value of 'this' within f.

Returns

Result of evaluating f repeatedly across the values of the iterator.

code » `<VALUE>` goog.iter.repeat ( value ) ⇒ `!goog.iter.Iterator.<VALUE>`

Creates an iterator that returns the same object or value repeatedly.

Parameters

value: `VALUE`
  Any object or value to repeat.

Returns

A new iterator that returns the repeated value.

code » `<VALUE>` goog.iter.slice ( iterable, start, opt_end ) ⇒ `!goog.iter.Iterator.<VALUE>`

Creates an iterator that returns a range of elements from an iterable. Similar to goog.array#slice but does not support negative indexes.

Parameters

iterable: `(!goog.iter.Iterator.<VALUE>|!goog.iter.Iterable)`
  The iterable to slice.
start: `number`
  The index of the first element to return.
opt_end: `number=`
  The index after the last element to return. If defined, must be greater than or equal to `start`.

Returns

A new iterator containing a slice of the original.

code » `<THIS, VALUE>` goog.iter.some ( iterable, f, opt_obj ) ⇒ `boolean`

Goes through the values in the iterator. Calls f for each of these, and if any of them returns true, this returns true (without checking the rest). If all return false this will return false.

Parameters

iterable: `(goog.iter.Iterator.<VALUE>|goog.iter.Iterable)`
  The iterator object.
f: `function(this: THIS, VALUE, undefined, goog.iter.Iterator.<VALUE>): boolean`
  The function to call for every value. This function takes 3 arguments (the value, undefined, and the iterator) and should return a boolean.
opt_obj: `THIS=`
  The object to be used as the value of 'this' within `f`.

Returns

true if any value passes the test.

code » `<THIS, RESULT>` goog.iter.starMap ( iterable, f, opt_obj ) ⇒ `!goog.iter.Iterator.<RESULT>`

Gives an iterator that gives the result of calling the given function `f` with the arguments taken from the next element from `iterable` (the elements are expected to also be iterables). Similar to goog.iter#map but allows the function to accept multiple arguments from the iterable.

Parameters

iterable: `!goog.iter.Iterable`
  The iterable of iterables to iterate over.
f: `function(this: THIS, *): RESULT`
  The function to call for every element. This function takes N+2 arguments, where N represents the number of items from the next element of the iterable. The two additional arguments passed to the function are undefined and the iterator itself. The function should return a new value.
opt_obj: `THIS=`
  The object to be used as the value of 'this' within `f`.

Returns

A new iterator that returns the results of applying the function to each element in the original iterator.

code » `<THIS, VALUE>` goog.iter.takeWhile ( iterable, f, opt_obj ) ⇒ `!goog.iter.Iterator.<VALUE>`

Builds a new iterator that iterates over the original, but only as long as a supplied function returns true.

Parameters

iterable: `(goog.iter.Iterator.<VALUE>|goog.iter.Iterable)`
  The iterator object.
f: `function(this: THIS, VALUE, undefined, goog.iter.Iterator.<VALUE>): boolean`
  The function to call for every value. This function takes 3 arguments (the value, undefined, and the iterator) and should return a boolean.
opt_obj: `THIS=`
  This is used as the 'this' object in f when called.

Returns

A new iterator that keeps elements in the original iterator as long as the function is true.

code » `<VALUE>` goog.iter.tee ( iterable, opt_num ) ⇒ `!Array.<goog.iter.Iterator>`

Returns an array of iterators each of which can iterate over the values in `iterable` without advancing the others.

Parameters

iterable: `(!goog.iter.Iterator.<VALUE>|!goog.iter.Iterable)`
  The iterable to tee.
opt_num: `number=`
  The number of iterators to create. Default is 2.

Returns

An array of iterators.

code » `<VALUE>` goog.iter.toArray ( iterable ) ⇒ `!Array.<VALUE>`

Converts the iterator to an array

Parameters

iterable: `(goog.iter.Iterator.<VALUE>|goog.iter.Iterable)`
  The iterator to convert to an array.

Returns

An array of the elements the iterator iterates over.

code » `<VALUE>` goog.iter.toIterator ( iterable ) ⇒ `!goog.iter.Iterator.<VALUE>`

Returns an iterator that knows how to iterate over the values in the object.

Parameters

iterable: `(goog.iter.Iterator.<VALUE>|goog.iter.Iterable)`
  If the object is an iterator it will be returned as is. If the object has an `__iterator__` method that will be called to get the value iterator. If the object is an array-like object we create an iterator for that.

Returns

An iterator that knows how to iterate over the values in `iterable`.

code » `<VALUE>` goog.iter.zip ( var_args ) ⇒ `!goog.iter.Iterator`

Creates an iterator that returns arrays containing the ith elements from the provided iterables. The returned arrays will be the same size as the number of iterables given in `var_args`. Once the shortest iterable is exhausted, subsequent calls to `next()` will throw `goog.iter.StopIteration`.

Parameters

var_args: `...(!goog.iter.Iterator.<VALUE>|!goog.iter.Iterable)`
  Any number of iterable objects.

Returns

A new iterator that returns arrays of elements from the provided iterables.

code » `<VALUE>` goog.iter.zipLongest ( fillValue, var_args ) ⇒ `!goog.iter.Iterator`

Creates an iterator that returns arrays containing the ith elements from the provided iterables. The returned arrays will be the same size as the number of iterables given in `var_args`. Shorter iterables will be extended with `fillValue`. Once the longest iterable is exhausted, subsequent calls to `next()` will throw `goog.iter.StopIteration`.

Parameters

fillValue: `VALUE`
  The object or value used to fill shorter iterables.
var_args: `...(!goog.iter.Iterator.<VALUE>|!goog.iter.Iterable)`
  Any number of iterable objects.

Returns

A new iterator that returns arrays of elements from the provided iterables.

## Global Properties

code » goog.iter.StopIteration : `Error`

Singleton Error object that is used to terminate iterations.