Iterator

Produces values in order on demand.

:warning: Version 2 iterators differ substantially from version 1. This is a description of iterators from version 1, which tracked an earlier version of the ECMAScript iterator proposal.

An iterator is an object with a next method that returns the next value for the iterator, or throws StopIteration, a global sentinel object for all iterators. ReturnValue is a global constructor for instances that inherit from StopIteration used to stop an iterator with a return value, particularly useful for generators. The iterator module shims these globals if they do not already exist.

An iterable is an object that implements iterator. Collections that implement iterator may return either an iterator or an Iterator. Iterator supports additional methods beyond next.

Methods

mapIterator(callback, thisp?)

Returns an iterator for the respective return values of a callback for each value from this iteration.

filterIterator(callback, thisp?)

Returns an iterator for all values from this iterator that pass a test.

dropWhile(callback, thisp?)

Returns an iterator that will begin with the first value from this iteration that passes a test.

takeWhile(callback, thisp?)

Returns an iterator that will produce every value from this iteration until an entry fails a test.

zipIterator(...iterables)

Returns an iterator that will produce an array of values with the value at the same index of this iterator and each given iterable.

enumerateIterator(start?)

Creates an iterator that will produce [index, value] for each value in this iterator, on demand.

iterate|iterator()

Iterates every value in this collection.

forEach(callback, thisp?)

Calls the callback for each entry in the collection.

map(callback, thisp?)

Returns an array of the respective return values of a callback for each entry in this collection.

filter(callback, thisp?)

Returns an array with each value from this collection that passes the given test.

reduce(callback, basis)

Aggregates every value in this collection with the result collected up to that index.

group(callback, thisp?, equals?)

Returns an array of [key, class] entries where every value from the collection is placed into the same equivalence class if they return the same key through the given callback.

some(callback, thisp?)

Returns whether any entry in this collection passes a given test.

every(callback, thisp?)

Returns whether every entry in this collection passes a given test.

any()

Returns whether any value in the collection is truthy.

all()

Returns whether all values in the collection are truthy.

sorted(compare?)

Returns a sorted array of the values in this collection.

reversed()

Returns a copy of this collection with the values in reverse order.

sum(zero?)

Returns the sum of all values in this collection.

average()

Returns the arithmetic mean of the collection, by computing its sum and the count of values and returning the quotient.

min()

Returns the smallest value in this collection.

max()

Returns the largest value in this collection.

zip(...iterables)

Returns an array of the respective values in this collection and in each collection provided as an argument.

enumerate(start?)

Returns an array of [index, value] entries for each value in this collection, counting all values from the given index.

concat(...iterables)

Returns a new collection of the same type containing all the values of itself and the values of any number of other iterable collections in order.

flatten()

Assuming that this is a collection of collections, returns a new collection that contains all the values of each nested collection in order.

toArray()

Returns an array of each value in this collection.

toObject()

Returns an object with each property name and value corresponding to the entries in this collection.

constructClone(values?)

Creates a shallow clone of this collection.

Usage

var Iterator = require("collections/iterator");
  • Iterator(iterable)
  • Iterator(iterator)
  • Iterator(next)
Source code