Extends and implements the IterableIterator interface. Methods marked with the @lazy prefix are chainable methods that modify the internal iterator, but don't start iterating. Methods without the @lazy prefix do start iterating some amount, depending on the method.

Type Parameters

  • T

Implements

  • IterableIterator<T>

Constructors

Properties

iterator: Iterator<T, any, undefined>

Methods

  • Type Parameters

    • Length extends number

    Parameters

    • length: Length

      The length of each chunk, must be greater than 0.

    • Optional fill: T

      Optional, the value to fill the last chunk with if it's not the same length as the rest of the iterator.

    Returns ExtendedIterator<Tuple<T, Length>>

    Lazy

    Yields non-overlapping chunks (tuples) of length from this iterator.

    Example

    iter([1,2,3,4,5,6,7,8,9]).chunk(3).toArray() // [[1,2,3], [4,5,6], [7,8,9]]
    iter([1,2,3,4,5,6,7,8,9]).chunk(2, 0).toArray() // [[1,2], [3,4], [5,6], [7,8], [9, 0]]
  • Start iterating through this iterator, but don't return the values from this method.

    Parameters

    • n: number = Infinity

      optional, the number of elements to consume (default: Infinity).

    Returns void

  • Parameters

    • times: number = Infinity

      The number of times to cycle through the iterator (default: Infinity).

    Returns ExtendedIterator<T>

    Lazy

    Makes this iterator cycle infinitely through it's values.

    Example

    equal(iter([1,2,3]).cycle().take(5).toArray(), [1,2,3,1,2])
    
  • Parameters

    • n: number = Infinity

      optional, the number of elements to exhaust.

    Returns void

    Deprecated

    Use consume instead, as this is the more standard name for this type of method. Start iterating through this iterator, but don't return the values from this method.

  • Finds the first value that passes a truthy value to predicate, then returns it. Only consumes the iterator's values up to the found value, then stops. So if it's not found, then the iterator is exhausted.

    Type Parameters

    • V

    Parameters

    • predicate: ((value) => value is V)
        • (value): value is V
        • Parameters

          • value: T

          Returns value is V

    Returns undefined | V

  • Parameters

    Returns undefined | T

  • Finds the index of the first value that passes a truthy vale to predicate, then returns it. Only consumes the iterator's values up to the found value, then stops. So if it's not found, then the iterator is exhausted.

    Parameters

    Returns number

  • Groups consecutive keys in the input iterator by some key identifier function or property. Functionally equivalent to the itertools.groupby function in Python. Generally the input iterator needs to be sorted by the same key. Each iteration will return the next group of values with the same key, until the key changes. So unless the input iterator is sorted beforehand, you may have broken up groups with the same keys.

    Returns ExtendedIterator<[T, T[]]>

    Example

    groupby('AAAABBBCCDAABBB') // =>
    // ['A', ['A', 'A', 'A', 'A']],
    // ['B', ['B', 'B', 'B']],
    // ['C', ['C', 'C']],
    // ['D', ['D']],
    // ['A', ['A', 'A']],
    // ['B', ['B', 'B', 'B']]
  • Type Parameters

    • K extends string | number | symbol | Iteratee<T, any>

    Parameters

    • key: K

    Returns ExtendedIterator<[KeyIdentifiersValue<T, K>, T[]]>

  • Returns true if value strictly equals some value in this iterator.

    Parameters

    • value: T

    Returns boolean

  • Returns this iterator as a string with each value joined by separator.

    Parameters

    • separator: string = ','

      The separator to use between each value (default: ',').

    Returns string

  • Returns a { value, done } object that adheres to the Iterator interface.

    Parameters

    • Rest ...args: any[]

    Returns IteratorResult<T, T>

  • Iterates and finds the element at index. Returns undefined if not found.

    Parameters

    • index: number

      The index to find. Only supports positive indices.

    Returns undefined | T

  • Returns ExtendedIterator<[T, T]>

    Lazy

    Return a new iterator of pairs (tuples) of the values in this one. The number of pairs will always be one fewer than this iterator. Will be empty if this iterator has fewer than two values.

    Example

    iter([1,2,3]).pairwise().toArray() // [[1,2], [2,3]]
    iter([1]).pairwise().toArray() // []
  • Partitions this iterator into a tuple of [falsey, truthy] corresponding to what predicate returns for each value.

    Parameters

    Returns [falsey: T[], truthy: T[]]

  • Peek ahead of where the current iteration is. This doesn't consume any values of the iterator.

    Type Parameters

    • N extends number = 1

    Parameters

    • ahead: N = ...

      optional, the number of elements to peek ahead.

    Returns Tuple<T, N>

  • Reduces this iterator to a single value.

    Parameters

    • reducer: ((accumulator, value) => T)
        • (accumulator, value): T
        • Parameters

          • accumulator: T
          • value: T

          Returns T

    Returns T

  • Type Parameters

    • R

    Parameters

    • reducer: ((accumulator, value) => R)
        • (accumulator, value): R
        • Parameters

          • accumulator: R
          • value: T

          Returns R

    • initialValue: R

    Returns R

  • Type Parameters

    • R

    Parameters

    • reducer: ((accumulator, value) => R)
        • (accumulator, value): R
        • Parameters

          • accumulator: T | R
          • value: T

          Returns R

    Returns R

  • Parameters

    • times: number = Infinity

      The number of times to resume the iterator (default: Infinity).

    Returns ExtendedIterator<T>

    Lazy

    Resumes this iterator a certain number of times after it's next value returns { done: true }.

    Example

    const it = iter([1,2,3]).resume(1);
    equal(it.toArray(), [1,2,3]);
    equal(it.toArray(), [1,2,3]);
    equal(it.toArray(), []);
  • Parameters

    • start: number = 0

      The index to start at (inclusive).

    • end: number = Infinity

      The index to end at (exclusive).

    Returns ExtendedIterator<T>

    A new ExtendedIterator that only includes the elements between start and end.

    Lazy

    Works like Array.prototype.slice, returns a new slice of this iterator.

    Note

    This does not support negative start and end indices, as it's not possible to know the length of the iterator while iterating.

  • Parameters

    Returns ExtendedIterator<T>

    Lazy

    Tap into this iterator by supplying func which is passed each value of this iterator. The return value of func is unused and this method is purely designed for a designated place to perform side effects.

    Example

    iter([1,2,3])
    .tap(console.log) // logs 1, 2, 3 to the console
    .map(n => n * n)
    .tap(console.log) // logs 1, 4, 9 to the console
    .toArray() // returns [1, 4, 9]
  • Type Parameters

    • N extends number

    Parameters

    • n: N

      The number of independent iterators to create.

    Returns Tuple<ExtendedIterator<T>, N>

    Lazy

    Returns n independent iterators, each of which is a copy of this iterator at the time of calling tee. Once tee has made a split, do not modify or call upon the original iterator, as the new iterators will not be updated/informed. This caches the original iterator's values as the new iterators are iterated through. So depending on the size of the original iterator, there could be significant memory overhead in using tee. tee's intended use is to iterate over the returned iterators in parallel, or at least somewhat in parallel. In general, if one returned iterator consumes most or all of it's values, then it is faster to just use toArray and then iterate over that.

  • Shorthand for new Map<K, V>(this). The type of this iterator must extend any[] for this to work. And you may also need to pass in your own values for the generics: e.g. iterator.toMap<string, number>();

    Type Parameters

    • K extends string | number = T extends any[]
          ? T<T>[0]
          : never
    • V = T extends any[]
          ? T<T>[1]
          : never

    Returns Map<K, V>

  • Returns ExtendedIterator<T>[]

    Lazy

    The inverse of zip and zipLongest. This method disaggregates the elements of this iterator. The nth iterator in the returned tuple contains the nth element of each value in this iterator. The length of the returned tuple is determined by the length of the first value in this iterator.

  • Type Parameters

    • Length extends number

    Parameters

    • length: Length

      The length of each window, must be greater than 0.

    • offset: number

      The offset of each window from each other. Must be greater than 0.

    • Optional fill: T

      Optional, the value to fill the last window with if it's not the same length as the rest of the iterator.

    Returns ExtendedIterator<Tuple<T, Length>>

    Lazy

    Yields sliding windows (tuples) of length from this iterator. Each window is separated by offset number of elements.

    Example

    iter([1,2,3,4,5]).windows(2, 1).toArray() // [[1,2], [2,3], [3,4], [4,5]]
    iter([1,2,3,4,5]).windows(2, 3).toArray() // [[1,2], [4,5]]
    iter([1,2,3,4,5]).windows(3, 3, 0).toArray() // [[1,2,3], [4,5,0]]