Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface Sequence<T>

A Sequence provides a fluent functional API consisting of various intermediate and terminal operations for processing the iterated data. The operations are evaluated lazily to avoid examining all of the input data when it's not necessary. Sequences can be iterated only once.

Type parameters

  • T

Hierarchy

  • SequenceOperators<T>
    • Sequence

Index

Properties

iterator

iterator: Iterator<T>

Methods

all

  • all<T>(this: Sequence<T>, predicate: function): boolean
  • Returns true if all elements match the given predicate.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • predicate: function
        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns boolean

any

  • any<T>(this: Sequence<T>, predicate?: undefined | function): boolean
  • Returns true if at least one element match the given predicate.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • Optional predicate: undefined | function

    Returns boolean

asIterable

  • asIterable<T>(this: Sequence<T>): Iterable<T>

associate

  • associate<T, K, V>(this: Sequence<T>, transform: function): Map<K, V>
  • Transforms each element into a key-value pair and returns the results as map. In case of duplicate keys the last key-value pair overrides the other.

    Type parameters

    • T

    • K

    • V

    Parameters

    • this: Sequence<T>
    • transform: function
        • (value: T): [K, V]
        • Parameters

          • value: T

          Returns [K, V]

    Returns Map<K, V>

associateBy

  • associateBy<K>(keySelector: function): Map<K, T>
  • associateBy<K>(key: K): Map<T[K], T>
  • associateBy<K, V>(keySelector: function, valueTransformer: function): Map<K, V>
  • associateBy<K, V>(key: K, valueTransformer: function): Map<T[K], V>
  • Returns a map consisting of the elements mapped by the given keySelector.

    Type parameters

    • K

    Parameters

    • keySelector: function
        • (value: T): K
        • Parameters

          • value: T

          Returns K

    Returns Map<K, T>

  • Returns a map consisting of the elements indexed by the given key.

    Type parameters

    • K: keyof T

    Parameters

    • key: K

    Returns Map<T[K], T>

  • Returns a map consisting of the elements mapped by the given keySelector. The value is transformed into another value by the valueTransformer.

    Type parameters

    • K

    • V

    Parameters

    • keySelector: function
        • (value: T): K
        • Parameters

          • value: T

          Returns K

    • valueTransformer: function
        • (value: T): V
        • Parameters

          • value: T

          Returns V

    Returns Map<K, V>

  • Returns a map consisting of the elements indexed by the given key. The value is transformed into another value by the valueTransformer.

    Type parameters

    • K: keyof T

    • V

    Parameters

    • key: K
    • valueTransformer: function
        • (value: T): V
        • Parameters

          • value: T

          Returns V

    Returns Map<T[K], V>

average

  • average(this: Sequence<number>): number
  • Returns the average of all numbers of the sequence or NaN if the sequence is empty.

    Parameters

    Returns number

chunk

  • chunk<T>(this: Sequence<T>, chunkSize: number): Array<Array<T>>
  • Splits the elements of the sequence into arrays which length is determined by the given chunkSize and returns all chunks as array.

    Type parameters

    • T

    Parameters

    Returns Array<Array<T>>

contains

  • contains<T>(this: Sequence<T>, element: T): boolean
  • Returns true if the sequence contains the given element.

    Type parameters

    • T

    Parameters

    Returns boolean

count

  • count<T>(this: Sequence<T>, predicate?: undefined | function): number
  • Returns the number of elements of this sequence. If predicate is present, returns the number of elements matching the given predicate.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • Optional predicate: undefined | function

    Returns number

distinct

distinctBy

  • Returns a new sequence which discards all elements with duplicate items determined by the given selector.

    Type parameters

    • T

    • K

    Parameters

    • this: Sequence<T>
    • selector: function
        • (item: T): K
        • Parameters

          • item: T

          Returns K

    Returns Sequence<T>

drop

  • Returns a new sequence which discards the first n elements;

    Type parameters

    • T

    Parameters

    Returns Sequence<T>

dropWhile

  • Drops all elements of the sequence as long as the given predicate evaluates to true.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • predicate: function
        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns Sequence<T>

elementAt

  • elementAt<T>(this: Sequence<T>, index: number): T
  • Returns the element at position index (zero-based) or throws an error if index is out of bounds.

    Type parameters

    • T

    Parameters

    Returns T

elementAtOrElse

  • elementAtOrElse<T>(this: Sequence<T>, index: number, defaultValue: function): T
  • Returns the element at position index (zero-based). If index is out of bounds returns the result of the given defaultValue function.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • index: number
    • defaultValue: function
        • (index: number): T
        • Parameters

          • index: number

          Returns T

    Returns T

elementAtOrNull

  • elementAtOrNull<T>(this: Sequence<T>, index: number): T | null

filter

  • Returns a new sequence consisting of all elements that match the given predicate.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • predicate: function
        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns Sequence<T>

filterIndexed

  • Returns a new sequence consisting of all elements that match the given predicate.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • predicate: function
        • (index: number, value: T): boolean
        • Parameters

          • index: number
          • value: T

          Returns boolean

    Returns Sequence<T>

filterNot

  • Returns a new sequence consisting of all elements that don't match the given predicate.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • predicate: function
        • (value: T): boolean
        • Parameters

          • value: T

          Returns boolean

    Returns Sequence<T>

filterNotNull

find

  • find<T>(this: Sequence<T>, predicate?: undefined | function): T | null
  • Returns the first element of the sequence or the first element matching predicate if present, otherwise returns null.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • Optional predicate: undefined | function

    Returns T | null

findLast

  • findLast<T>(this: Sequence<T>, predicate?: undefined | function): T | null
  • Returns the last element of the sequence or the last element matching predicate if present, otherwise returns null.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • Optional predicate: undefined | function

    Returns T | null

first

  • first<T>(this: Sequence<T>, predicate?: undefined | function): T
  • Returns the first element of the sequence or the first element matching predicate if present, otherwise throws an error.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • Optional predicate: undefined | function

    Returns T

firstOrNull

  • firstOrNull<T>(this: Sequence<T>, predicate?: undefined | function): T | null
  • Returns the first element of the sequence or the first element matching predicate if present, otherwise returns null.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • Optional predicate: undefined | function

    Returns T | null

flatMap

  • Transforms each element into a sequence of items and returns a flat single sequence of all those items.

    Type parameters

    • S

    • T

    Parameters

    Returns Sequence<T>

flatten

fold

  • fold<T, R>(this: Sequence<T>, initial: R, operation: function): R
  • Accumulates all elements of the sequence into a single result by applying the given operation starting with the initial value. The result of the last operation will be passed as accumulated value to the getNext invocation of the operation until all elements of the sequence are processed.

    Type parameters

    • T

    • R

    Parameters

    • this: Sequence<T>
    • initial: R
    • operation: function
        • (acc: R, element: T): R
        • Parameters

          • acc: R
          • element: T

          Returns R

    Returns R

foldIndexed

  • foldIndexed<T, R>(this: Sequence<T>, initial: R, operation: function): R
  • Accumulates all elements of the sequence into a single result by applying the given operation starting with the initial value. The result of the last operation will be passed as accumulated value to the getNext invocation of the operation as well as the index of the current element (zero-based) until all elements of the sequence are processed.

    Type parameters

    • T

    • R

    Parameters

    • this: Sequence<T>
    • initial: R
    • operation: function
        • (index: number, acc: R, element: T): R
        • Parameters

          • index: number
          • acc: R
          • element: T

          Returns R

    Returns R

forEach

  • forEach<T>(this: Sequence<T>, action: function): void
  • Performs the given action (side-effect) for each element of the sequence.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • action: function
        • (item: T): void
        • Parameters

          • item: T

          Returns void

    Returns void

forEachIndexed

  • forEachIndexed<T>(this: Sequence<T>, action: function): void
  • Performs the given action (side-effect) for each element of the sequence and passes the index of the current element (zero-based).

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • action: function
        • (index: number, value: T): void
        • Parameters

          • index: number
          • value: T

          Returns void

    Returns void

groupBy

  • groupBy<T, K>(this: Sequence<T>, keySelector: function): Map<K, Array<T>>
  • Groups all elements of the sequence into a map. Keys are determined by the given keySelector function.

    Type parameters

    • T

    • K

    Parameters

    • this: Sequence<T>
    • keySelector: function
        • (value: T): K
        • Parameters

          • value: T

          Returns K

    Returns Map<K, Array<T>>

indexOf

  • indexOf<T>(this: Sequence<T>, element: T): number
  • Returns the zero-based index of the given element or -1 if the sequence does not contain the element.

    Type parameters

    • T

    Parameters

    Returns number

indexOfFirst

  • indexOfFirst<T>(this: Sequence<T>, predicate: function): number
  • Returns the zero-based index of the first element matching the given predicate or -1 if no element matches the predicate.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • predicate: function
        • (value: T): boolean
        • Parameters

          • value: T

          Returns boolean

    Returns number

indexOfLast

  • indexOfLast<T>(this: Sequence<T>, predicate: function): number
  • Returns the zero-based index of the last element matching the given predicate or -1 if no element matches the predicate.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • predicate: function
        • (value: T): boolean
        • Parameters

          • value: T

          Returns boolean

    Returns number

joinTo

joinToString

last

  • last<T>(this: Sequence<T>, predicate?: undefined | function): T
  • Returns the last element of the sequence or the last element matching predicate if present, otherwise throws an error.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • Optional predicate: undefined | function

    Returns T

lastOrNull

  • lastOrNull<T>(this: Sequence<T>, predicate?: undefined | function): T | null
  • Returns the last element of the sequence or the last element matching predicate if present, otherwise returns null.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • Optional predicate: undefined | function

    Returns T | null

map

  • Transforms each element into another value by applying the given transform function and returns a new sequence.

    Type parameters

    • S

    • T

    Parameters

    • this: Sequence<T>
    • transform: function
        • (element: T): S
        • Parameters

          • element: T

          Returns S

    Returns Sequence<S>

mapIndexed

  • Transforms each element into another value by applying the given transform function and returns a new sequence.

    Type parameters

    • T

    • R

    Parameters

    • this: Sequence<T>
    • transform: function
        • (index: number, value: T): R
        • Parameters

          • index: number
          • value: T

          Returns R

    Returns Sequence<R>

mapNotNull

  • Transforms each element into another value by applying the given transform function and returns a new sequence. Transformations into null values are discarded.

    Type parameters

    • T

    • R

    Parameters

    • this: Sequence<T>
    • transform: function
        • (value: T): R | null
        • Parameters

          • value: T

          Returns R | null

    Returns Sequence<R>

max

  • Returns the maximum element of the sequence or null if sequence is empty.

    Type parameters

    • T

    Parameters

    Returns T | null

maxBy

  • maxBy<T, R>(this: Sequence<T>, selector: function): T | null
  • Returns the maximum element by comparing the results of the given selector function for each element of the sequence or null if the sequence is empty.

    Type parameters

    • T

    • R

    Parameters

    • this: Sequence<T>
    • selector: function
        • (value: T): R
        • Parameters

          • value: T

          Returns R

    Returns T | null

maxWith

  • maxWith<T>(this: Sequence<T>, compare: function): T | null
  • Returns the maximum element of the sequence by evaluating the given compare function or null if sequence is empty.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • compare: function
        • (a: T, b: T): number
        • Parameters

          • a: T
          • b: T

          Returns number

    Returns T | null

merge

  • merge<T, S>(this: Sequence<T>, other: Sequence<T> | Iterable<T>, selector: function, prependNewValues?: boolean): Sequence<T>
  • Merges the elements of both sequences into a new sequence. Each element of this sequence is eventually replaced with an element of the other sequence by comparing results of the given selector function. If no value is found in the other sequence the element is retained. New elements of the other sequence are appended to the end of the new sequence or prepended to the start of the new sequence, if prependNewValues is set to true. This operation is not lazy evaluated.

    Type parameters

    • T

    • S

    Parameters

    • this: Sequence<T>
    • other: Sequence<T> | Iterable<T>
    • selector: function
        • (value: T): S
        • Parameters

          • value: T

          Returns S

    • Default value prependNewValues: boolean = false

    Returns Sequence<T>

min

  • Returns the minimum element of the sequence or null if sequence is empty.

    Type parameters

    • T

    Parameters

    Returns T | null

minBy

  • minBy<T, R>(this: Sequence<T>, selector: function): T | null
  • Returns the minimum element by comparing the results of the given selector function for each element of the sequence or null if the sequence is empty.

    Type parameters

    • T

    • R

    Parameters

    • this: Sequence<T>
    • selector: function
        • (value: T): R
        • Parameters

          • value: T

          Returns R

    Returns T | null

minWith

  • minWith<T>(this: Sequence<T>, compare: function): T | null
  • Returns the minimum element of the sequence by evaluating the given compare function or null if sequence is empty.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • compare: function
        • (a: T, b: T): number
        • Parameters

          • a: T
          • b: T

          Returns number

    Returns T | null

minus

  • Removes the given data and returns a new sequence. Data can either be a single element, an array of elements or a sequence of elements.

    Type parameters

    • T

    Parameters

    Returns Sequence<T>

none

  • none<T>(this: Sequence<T>, predicate?: undefined | function): boolean
  • Returns true if no element match the given predicate or if the sequence is empty if no predicate is present.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • Optional predicate: undefined | function

    Returns boolean

onEach

  • Performs the given action for each element and returns the sequence.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • action: function
        • (value: T): void
        • Parameters

          • value: T

          Returns void

    Returns Sequence<T>

partition

  • partition<T>(this: Sequence<T>, predicate: function): object
  • Evaluates the given predicate for each element of the sequence and assorts each element into one of two lists according to the result of the predicate. Returns both lists as an object.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • predicate: function
        • (value: T): boolean
        • Parameters

          • value: T

          Returns boolean

    Returns object

plus

  • Appends the given element to the end of the sequence and returns a new sequence.

    Type parameters

    • T

    Parameters

    Returns Sequence<T>

  • Appends the given array to the end of the sequence and returns a new sequence.

    Type parameters

    • T

    Parameters

    Returns Sequence<T>

  • Appends the given sequence to the end of the sequence and returns a new sequence.

    Type parameters

    • T

    Parameters

    Returns Sequence<T>

reduce

  • reduce<S, T>(this: Sequence<T>, operation: function): S
  • Reduces the whole sequence to a single value by invoking operation with each element from left to right. For every invocation of the operation acc is the result of the last invocation. For the first invocation of the operation acc is the first element of the sequence.

    Type parameters

    • S

    • T: S

    Parameters

    • this: Sequence<T>
    • operation: function
        • (acc: S, value: T): S
        • Parameters

          • acc: S
          • value: T

          Returns S

    Returns S

reduceIndexed

  • reduceIndexed<S, T>(this: Sequence<T>, operation: function): S
  • Reduces the whole sequence to a single value by invoking operation with each element from left to right. For every invocation of the operation acc is the result of the last invocation. For the first invocation of the operation acc is the first element of the sequence. In addition the index of the current element is also passed to the operation.

    Type parameters

    • S

    • T: S

    Parameters

    • this: Sequence<T>
    • operation: function
        • (index: number, acc: S, element: T): S
        • Parameters

          • index: number
          • acc: S
          • element: T

          Returns S

    Returns S

reverse

single

  • single<T>(this: Sequence<T>, predicate?: undefined | function): T
  • Returns the single element of the sequence or throws error if the sequence has more than one element or none at all. If a predicate is passed returns the single element matching the predicate or throws an error if more or less than one element match the predicate.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • Optional predicate: undefined | function

    Returns T

singleOrNull

  • singleOrNull<T>(this: Sequence<T>, predicate?: undefined | function): T | null
  • Returns the single element of the sequence or null if the sequence has more than one element or none at all. If a predicate is passed returns the single element matching the predicate or null if more or less than one element match the predicate.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • Optional predicate: undefined | function

    Returns T | null

sorted

  • sorted<T>(this: Sequence<T>, composeComparator?: undefined | function): Sequence<T>
  • Returns a new sequence with all elements sorted by the comparator specified by the given composeComparator function or in natural order if no arguments are given.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • Optional composeComparator: undefined | function

    Returns Sequence<T>

sortedBy

  • Returns a new sequence with all elements sorted ascending by the value specified by the given selector function.

    Type parameters

    • T

    • R

    Parameters

    • this: Sequence<T>
    • selector: function
        • (value: T): R
        • Parameters

          • value: T

          Returns R

    Returns Sequence<T>

sortedByDescending

sortedDescending

sortedWith

  • Returns a new sequence with all elements sorted be the given compare function.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • comparison: function
        • (a: T, b: T): number
        • Parameters

          • a: T
          • b: T

          Returns number

    Returns Sequence<T>

sum

  • Returns the sum of all numbers.

    Parameters

    Returns number

sumBy

  • sumBy<T>(this: Sequence<T>, selector: function): number
  • Returns the sum of all numbers specified by the given selector function.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • selector: function
        • (value: T): number
        • Parameters

          • value: T

          Returns number

    Returns number

take

  • Returns a new sequence consisting of the first n elements. All other elements are discarded.

    Type parameters

    • T

    Parameters

    Returns Sequence<T>

takeWhile

  • Takes all elements of the sequence as long as the given predicate evaluates to true.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • predicate: function
        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns Sequence<T>

toArray

  • toArray<T>(this: Sequence<T>, array?: Array<T>): Array<T>
  • Returns all elements of the sequence as array. If an array is passed the elements are appended to the end of the array.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • Optional array: Array<T>

    Returns Array<T>

toList

  • toList<T>(this: Sequence<T>, array?: Array<T>): Array<T>
  • Returns all elements of the sequence as array. If an array is passed the elements are appended to the end of the array.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • Optional array: Array<T>

    Returns Array<T>

toMap

  • toMap<K, V>(this: Sequence<[K, V]>, map?: Map<K, V>): Map<K, V>
  • Returns a map consisting of each key-value pair. If a map is passed the pairs are set on this map. Duplicate keys override each other.

    Type parameters

    • K

    • V

    Parameters

    Returns Map<K, V>

toSet

  • toSet<T>(this: Sequence<T>, set?: Set<T>): Set<T>
  • Returns all elements of the sequence as set. If a set is passed the elements are added to this set.

    Type parameters

    • T

    Parameters

    • this: Sequence<T>
    • Optional set: Set<T>

    Returns Set<T>

unzip

  • unzip<T, S>(this: Sequence<[T, S]>): [Array<T>, Array<S>]
  • Returns a pair of arrays where the first array contains all first values and the second array all second values from each input pair of the sequence.

    Type parameters

    • T

    • S

    Parameters

    Returns [Array<T>, Array<S>]

withIndex

zip

  • Returns a new sequence consisting of pairs built the elements of both sequences with the same index. The resulting sequence has the length of the shortest input sequence. All other elements are discarded.

    Type parameters

    • T

    • S

    Parameters

    Returns Sequence<[T, S]>