Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Sequence<T>

Describes a lazily computed sequence of elements that can be synchronously iterated over, allowing for composition of intermediate operations in an efficient, on-demand execution order.

Type parameters

  • T

    The type of the values contained by this sequence.

Hierarchy

  • Sequence

Implements

  • Iterable<T>

Index

Constructors

Protected constructor

  • new Sequence<T>(iterable: Iterable<T>, size?: number): Sequence<T>

Properties

Protected Readonly _size

_size: number

A cached size for sequences with available size information (eg. sequences made from an sized iterable, or sequences mapped from an already sized sequence). Less than 0 for non-deterministically sized sequences, like FilteringSequences.

Protected Readonly _values

_values: Iterable<T>

The underlying Iterable contained by this sequence. The values are dispatched through its iterator, so any type implementing the iterator symbol is fine.

Methods

[iterator]

  • [iterator](): Generator<T, any, unknown>
  • Returns a generator yielding all values contained by this sequence.

    Returns Generator<T, any, unknown>

[toStringTag]

  • [toStringTag](): string

concat

  • Returns a new Sequence containing the values of this sequence and the one provided as an argument. This operation is intermediate and stateless.

    The created sequence will be sized if and only if both of the concatenated sequences are of known size. Otherwise, size information is lost.

    Parameters

    Returns Sequence<T>

constrainOnce

contains

  • contains(value: T): boolean
  • Returns true if value is identical to one contained by this sequence. This is a short-circuiting terminal operation.

    Parameters

    • value: T

    Returns boolean

containsAll

  • containsAll(values: Iterable<T>): boolean
  • Returns true if all the values in values are contained by this sequence. This is a short-circuiting terminal operation.

    Parameters

    • values: Iterable<T>

    Returns boolean

count

  • count(): number
  • count(predicate: (item: T) => boolean): number
  • count(predicate: (item: T, index?: number) => boolean): number
  • If a predicate is provided, returns the amount of elements in this sequence that fulfilled it. Otherwise, returns the amount of elements in this sequence. This is a terminal operation.

    Returns number

  • Parameters

    • predicate: (item: T) => boolean
        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns number

  • Parameters

    • predicate: (item: T, index?: number) => boolean
        • (item: T, index?: number): boolean
        • Parameters

          • item: T
          • Optional index: number

          Returns boolean

    Returns number

drop

  • Returns a new Sequence skipping the first n elements. This operation is intermediate and stateful.

    The sequence created retains size information.

    Parameters

    • n: number

    Returns Sequence<T>

dropWhile

  • dropWhile(predicate: (item: T) => boolean): Sequence<T>
  • dropWhile(predicate: (item: T, index?: number) => boolean): Sequence<T>
  • Returns a new Sequence skipping the first n elements that fulfill a given predicate. This operation is intermediate and stateful.

    The sequence created is non-sized.

    Parameters

    • predicate: (item: T) => boolean
        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns Sequence<T>

  • Parameters

    • predicate: (item: T, index?: number) => boolean
        • (item: T, index?: number): boolean
        • Parameters

          • item: T
          • Optional index: number

          Returns boolean

    Returns Sequence<T>

elementAt

  • elementAt(index: number): undefined | T
  • Returns the element at index, or undefined if the index is out of bounds. Since Sequences are not a Random Access collection, this operation is O(n). This is a short-circuiting terminal operation.

    Parameters

    • index: number

    Returns undefined | T

every

  • every(predicate: (item: T) => boolean): boolean
  • every(predicate: (item: T, index?: number) => boolean): boolean
  • Returns true if all elements in this sequence fulfill the given predicate. This is a short-circuiting terminal operation.

    Parameters

    • predicate: (item: T) => boolean
        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns boolean

  • Parameters

    • predicate: (item: T, index?: number) => boolean
        • (item: T, index?: number): boolean
        • Parameters

          • item: T
          • Optional index: number

          Returns boolean

    Returns boolean

filter

  • filter(predicate: (item: T) => boolean): Sequence<T>
  • filter(predicate: (item: T, index?: number) => boolean): Sequence<T>
  • filter<S>(predicate: (item: T) => item is S): Sequence<S>
  • filter<S>(predicate: (item: T, index?: number) => item is S): Sequence<S>
  • Returns a new Sequence keeping the elements in this sequence that fulfill the given predicate. This operation is intermediate and stateless.

    The sequence created is non-sized.

    Parameters

    • predicate: (item: T) => boolean
        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns Sequence<T>

  • Parameters

    • predicate: (item: T, index?: number) => boolean
        • (item: T, index?: number): boolean
        • Parameters

          • item: T
          • Optional index: number

          Returns boolean

    Returns Sequence<T>

  • Type parameters

    • S

    Parameters

    • predicate: (item: T) => item is S
        • (item: T): item is S
        • Parameters

          • item: T

          Returns item is S

    Returns Sequence<S>

  • Type parameters

    • S

    Parameters

    • predicate: (item: T, index?: number) => item is S
        • (item: T, index?: number): item is S
        • Parameters

          • item: T
          • Optional index: number

          Returns item is S

    Returns Sequence<S>

find

  • find(predicate: (item: T) => boolean): undefined | T
  • find(predicate: (item: T, index?: number) => boolean): undefined | T
  • Returns the first item fulfilling a predicate, or undefined if none does. This is a short-circuiting terminal operation.

    Parameters

    • predicate: (item: T) => boolean
        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns undefined | T

  • Parameters

    • predicate: (item: T, index?: number) => boolean
        • (item: T, index?: number): boolean
        • Parameters

          • item: T
          • Optional index: number

          Returns boolean

    Returns undefined | T

findIndex

  • findIndex(predicate: (item: T) => boolean): number
  • Returns the index of the first item fulfilling a predicate, or -1 if none does. This is a short-circuiting terminal operation.

    Parameters

    • predicate: (item: T) => boolean
        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns number

findLast

  • findLast(predicate: (item: T) => boolean): undefined | T
  • findLast(predicate: (item: T, index?: number) => boolean): undefined | T
  • Returns the last item fulfilling a predicate, or undefined if none does. This is a terminal operation.

    Parameters

    • predicate: (item: T) => boolean
        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns undefined | T

  • Parameters

    • predicate: (item: T, index?: number) => boolean
        • (item: T, index?: number): boolean
        • Parameters

          • item: T
          • Optional index: number

          Returns boolean

    Returns undefined | T

findLastIndex

  • findLastIndex(predicate: (item: T) => boolean): number
  • Returns the index of the last item fulfilling a predicate, or -1 if none does. This is a terminal operation.

    Parameters

    • predicate: (item: T) => boolean
        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns number

first

  • first(): undefined | T
  • Returns the first element of this sequence, or undefined if it's empty. This is a short-circuiting terminal operation and it does not hang for infinite sequences.

    Returns undefined | T

flatMap

  • Returns a new Sequence flattening every sequence generated by the transformator function by one level. This operation is intermediate and stateless.

    The sequence created is non-sized.

    Type parameters

    • U

    Parameters

    Returns Sequence<U>

  • Type parameters

    • U

    Parameters

    • transform: (item: T, index?: number) => Sequence<U>
        • Parameters

          • item: T
          • Optional index: number

          Returns Sequence<U>

    Returns Sequence<U>

flatten

  • Returns a new Sequence flattening every inner sequence of this collection by one level. This operation is intermediate and stateless.

    The sequence created is non-sized.

    Type parameters

    • T

    Returns Sequence<T>

fold

  • fold<R>(initial: R, operation: (accumulator: R, current: T) => R): R
  • fold<R>(initial: R, operation: (accumulator: R, current: T, index?: number) => R): R
  • Returns a value resulting from recursively applying an operation on an initial value and the current value for every item of this sequence. This is a terminal operation.

    Type parameters

    • R

    Parameters

    • initial: R
    • operation: (accumulator: R, current: T) => R
        • (accumulator: R, current: T): R
        • Parameters

          • accumulator: R
          • current: T

          Returns R

    Returns R

  • Type parameters

    • R

    Parameters

    • initial: R
    • operation: (accumulator: R, current: T, index?: number) => R
        • (accumulator: R, current: T, index?: number): R
        • Parameters

          • accumulator: R
          • current: T
          • Optional index: number

          Returns R

    Returns R

forEach

  • forEach(action: (item: T) => void): void
  • forEach(action: (item: T, index?: number) => void): void
  • Performs an action on every item of this sequence. This is a terminal operation.

    Parameters

    • action: (item: T) => void
        • (item: T): void
        • Parameters

          • item: T

          Returns void

    Returns void

  • Parameters

    • action: (item: T, index?: number) => void
        • (item: T, index?: number): void
        • Parameters

          • item: T
          • Optional index: number

          Returns void

    Returns void

groupBy

  • groupBy<K>(selector: (item: T) => K): Map<K, T[]>
  • Groups items in this sequence into a map by a key generated by the selector provided. All items that generate the same key (according to {@link Map.has()} equality) will end up in the same bucket. This is a terminal operation.

    Note: TinyLazySeq is opinionated about grouping algorithms and does not provide a "group to object" equivalent (this method is equivalent to {@link Map.groupBy()}, and Sequence does not provide an {@link Object.groupBy()} version). The library developer discourages the usage of objects as maps, as frequent dynamic addition of keys degrades property access performance.

    Type parameters

    • K

    Parameters

    • selector: (item: T) => K
        • (item: T): K
        • Parameters

          • item: T

          Returns K

    Returns Map<K, T[]>

indexOf

  • indexOf(value: T): number
  • Returns the index of the first item of this sequence value is identical to. If there is none, returns -1. This is a short-circuiting terminal operation.

    Parameters

    • value: T

    Returns number

isEmpty

  • isEmpty(): boolean
  • Returns true if this collection contains no elements. This is a short-circuiting terminal operation and it does not hang for infinite sequences.

    Returns boolean

join

  • join(): string
  • join(options: { limit?: number; postfix?: string; prefix?: string; separator?: string; truncated?: string; transform?: any }): string
  • Returns a string representation of this sequence. This is a terminal operation.

    Returns string

  • Parameters

    • options: { limit?: number; postfix?: string; prefix?: string; separator?: string; truncated?: string; transform?: any }
      • Optional limit?: number
      • Optional postfix?: string
      • Optional prefix?: string
      • Optional separator?: string
      • Optional truncated?: string
      • transform?:function
        • transform(item: T): string

    Returns string

last

  • last(): undefined | T
  • Returns the last element contained by this sequence. This is a terminal operation.

    Returns undefined | T

lastIndexOf

  • lastIndexOf(value: T): number
  • Returns the index of the last element of this sequence value is identical to, or -1 if none is. This is a terminal operation.

    Parameters

    • value: T

    Returns number

map

  • map<U>(transform: (item: T) => U): Sequence<U>
  • map<U>(transform: (item: T, index?: number) => U): Sequence<U>
  • Returns a new Sequence transforming each value of this sequence. This operation is intermediate and stateless.

    The sequence created retains size information.

    Type parameters

    • U

    Parameters

    • transform: (item: T) => U
        • (item: T): U
        • Parameters

          • item: T

          Returns U

    Returns Sequence<U>

  • Type parameters

    • U

    Parameters

    • transform: (item: T, index?: number) => U
        • (item: T, index?: number): U
        • Parameters

          • item: T
          • Optional index: number

          Returns U

    Returns Sequence<U>

reduce

  • reduce<R>(operation: (accumulator: R, current: T) => R): null | R
  • reduce<R>(operation: (accumulator: R, current: T, index?: number) => R): null | R
  • Returns a value resulting from recursively applying an operation on the first element of this sequence and the current value for every item of this sequence after the first. This is a terminal operation.

    Type parameters

    • R

    Parameters

    • operation: (accumulator: R, current: T) => R
        • (accumulator: R, current: T): R
        • Parameters

          • accumulator: R
          • current: T

          Returns R

    Returns null | R

  • Type parameters

    • R

    Parameters

    • operation: (accumulator: R, current: T, index?: number) => R
        • (accumulator: R, current: T, index?: number): R
        • Parameters

          • accumulator: R
          • current: T
          • Optional index: number

          Returns R

    Returns null | R

size

  • size(): number
  • Returns the size of the iterable wrapped by this sequence if said iterable is a sized collection (by implementing a length or size property). Otherwise, returns a negative number.

    Returns number

some

  • some(): boolean
  • some(predicate?: (item: T) => boolean): boolean
  • some(predicate?: (item: T, index?: number) => boolean): boolean
  • If a predicate is provided, returns true if at least one element of this sequence fulfills it. Otherwise, returns true if there is at least one element in this sequence. This is short-circuiting terminal operation.

    Returns boolean

  • Parameters

    • Optional predicate: (item: T) => boolean
        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns boolean

  • Parameters

    • Optional predicate: (item: T, index?: number) => boolean
        • (item: T, index?: number): boolean
        • Parameters

          • item: T
          • Optional index: number

          Returns boolean

    Returns boolean

take

  • Returns a new Sequence dropping all elements after the first n. This operation is intermediate and stateful.

    The sequence created retains size information.

    Parameters

    • n: number

    Returns Sequence<T>

takeWhile

  • takeWhile(predicate: (item: T) => boolean): Sequence<T>
  • takeWhile(predicate: (item: T, index?: number) => boolean): Sequence<T>
  • Returns a new Sequence dropping all elements after a predicate stops fulfilling for the first time. This operation is intermediate and stateful.

    The sequence created is non-sized.

    Parameters

    • predicate: (item: T) => boolean
        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns Sequence<T>

  • Parameters

    • predicate: (item: T, index?: number) => boolean
        • (item: T, index?: number): boolean
        • Parameters

          • item: T
          • Optional index: number

          Returns boolean

    Returns Sequence<T>

toArray

  • toArray(): T[]
  • Returns an array containing the elements of this sequence. This is a terminal operation.

    Returns T[]

toString

  • toString(): string
  • Returns a string representation of this sequence and its estimated size, which could be one of the following:

    • empty: Sequence (empty)
    • an integer: Sequence (5)
    • unknown size: Sequence (unknown)

    Returns string

Static empty

  • Creates a lazy sequence containing no elements.
    The sequence created is sized (0).

    Type parameters

    • T

    Returns Sequence<T>

Static from

  • from<T>(source: Iterable<T> | Iterator<T, any, undefined>): Sequence<T>
  • Creates a lazy sequence wrapping the provided iterable or iterator.
    If an Iterable is provided, the sequence created is sized, as long as the provided iterable has a length or size property. Otherwise, size is less than 0 (unknown).
    If an Iterator is provided, the sequence created is constrained to only one iteration and unsized, as it is impossible to know the size ahead of time.

    Type parameters

    • T

    Parameters

    • source: Iterable<T> | Iterator<T, any, undefined>

    Returns Sequence<T>

Static generate

  • generate<T>(initial: T, nextValue: (current: T) => null | T): Sequence<T>
  • Creates a lazy sequence whose elements are generated by the recursive application of nextValue on initial until nextValue returns null.
    The sequence created is non-sized (unknown).

    Type parameters

    • T

    Parameters

    • initial: T
    • nextValue: (current: T) => null | T
        • (current: T): null | T
        • Parameters

          • current: T

          Returns null | T

    Returns Sequence<T>

Static of

  • Creates a lazy sequence containing the provided arguments.
    The sequence created is sized (known).

    Type parameters

    • T

    Parameters

    • Rest ...args: T[]

    Returns Sequence<T>

Generated using TypeDoc