Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Sequence<T>

Sequence class, used for creating, manipulating & finishing sequences Sequences are created using static factories

author

Degubi

Type parameters

  • T

Hierarchy

  • Sequence

Index

Constructors

Private constructor

Methods

allMatches

  • allMatches(predicateFunction: (element: T) => boolean): boolean
  • Method for terminating the sequence which returns true only if the given predicate matches all of the elements in the sequence

    Parameters

    • predicateFunction: (element: T) => boolean

      The function to test against

        • (element: T): boolean
        • Parameters

          • element: T

          Returns boolean

    Returns boolean

    True if the given predicate matches all of the elements in the sequence

anyMatches

  • anyMatches(predicateFunction: (element: T) => boolean): boolean
  • Method for terminating the sequence which returns true if the given predicate matches any of the elements in the sequence

    Parameters

    • predicateFunction: (element: T) => boolean

      The function to test against

        • (element: T): boolean
        • Parameters

          • element: T

          Returns boolean

    Returns boolean

    True if the given predicate matches any of the elements in the sequence

average

  • average(): number
  • Method for terminating the sequence while calculating the average of the elements Note: This function returns null if the sequence is empty

    Returns number

    Average of the elements in the sequence or null if the sequence was empty

chunk

  • chunk(chunkSizes: number): Sequence<T[]>
  • Method for creating a new sequence where the elements are chunked into arrays with each array containing max of 'chunkSizes' elements

    Parameters

    • chunkSizes: number

      Max size of each chunk

    Returns Sequence<T[]>

    New sequence with arrays containing the sequence's elements, with their maximum lengths restricted to the given chunk size

count

  • count(): number
  • Method for terminating the sequence while counting the number of elements

    Returns number

    Count of the elements in the sequence

distinct

  • distinct(keySelectorFunction?: (element: T) => any): Sequence<T>
  • Method for creating a new sequence containing only unique values

    Parameters

    • Optional keySelectorFunction: (element: T) => any

      The Function for selecting a key for uniqueness, defaults to identity

        • (element: T): any
        • Parameters

          • element: T

          Returns any

    Returns Sequence<T>

    New sequence containing distinct elements according to the input function

filter

  • filter(predicateFunction: (element: T) => boolean): Sequence<T>
  • Method for creating a new sequence containing only the elements that match the given predicate

    Parameters

    • predicateFunction: (element: T) => boolean

      The predicate to test against the elements, if it returns true the element is kept in the sequence

        • (element: T): boolean
        • Parameters

          • element: T

          Returns boolean

    Returns Sequence<T>

    New sequence containing only the elements that match the given predicate

first

  • first(): T
  • Method for terminating the sequence and getting the first element from it Note: This function returns null if the sequence is empty

    Returns T

    The first element of the sequence or null if the sequence was empty

flatMap

  • flatMap<TT>(flatMapperFunction: (element: T) => TT[]): Sequence<TT>
  • Method for creating a new sequence with elements after applying the given flatMapper function

    Type parameters

    • TT

    Parameters

    • flatMapperFunction: (element: T) => TT[]

      The function to apply to each element

        • (element: T): TT[]
        • Parameters

          • element: T

          Returns TT[]

    Returns Sequence<TT>

    New sequence with elements after applying the given function

forEach

  • forEach(consumerFunction: (element: T) => void): void
  • Method for terminating the sequence and applying a function to each of the elements

    Parameters

    • consumerFunction: (element: T) => void

      Function to apply to each of the elements

        • (element: T): void
        • Parameters

          • element: T

          Returns void

    Returns void

    Nothing

groupBy

  • groupBy<K>(keySelectorFunction: (element: T) => K): Map<K, T[]>
  • groupBy<K, V>(keySelectorFunction: (element: T) => K, grouperFunction: Grouper<T, V>): Map<K, V>
  • Method for terminating the sequence and performing a grouping by operation on the elements of the sequence Note: This is the same as calling groupBy with Grouper.toArray()

    Type parameters

    • K

    Parameters

    • keySelectorFunction: (element: T) => K

      Function used for extracting the keys of the result

        • (element: T): K
        • Parameters

          • element: T

          Returns K

    Returns Map<K, T[]>

    The result of the grouping

  • Method for terminating the sequence and performing a grouping by operation on the elements of the sequence

    Type parameters

    • K

    • V

    Parameters

    • keySelectorFunction: (element: T) => K

      Function used for extracting the keys of the result

        • (element: T): K
        • Parameters

          • element: T

          Returns K

    • grouperFunction: Grouper<T, V>

      An instance of a Grouper object, defaults to Grouper.toArray()

    Returns Map<K, V>

    The result of the grouping

join

  • join(separator?: string): string
  • Method for terminating the sequence while joining the elements together using the given separator

    Parameters

    • Optional separator: string

      The separator used for joining the elements, defaults to empty string

    Returns string

    The final joined string

last

  • last(): T
  • Method for terminating the sequence and getting the last element from it Note: This function returns null if the sequence is empty

    Returns T

    The last element of the sequence or null if the sequence was empty

map

  • map<TT>(mapperFunction: (element: T) => TT): Sequence<TT>
  • Method for creating a new sequence with elements after applying the given function

    Type parameters

    • TT

    Parameters

    • mapperFunction: (element: T) => TT

      The function to apply to each element

        • (element: T): TT
        • Parameters

          • element: T

          Returns TT

    Returns Sequence<TT>

    New sequence with elements after applying the given function

max

  • max(keySelectorFunction?: (element: T) => number): T
  • Method for retrieving the largest element from the sequence Note: This function returns null if the sequence is empty

    Parameters

    • Optional keySelectorFunction: (element: T) => number

      Function used for extracting the key from the object to compare against, defaults to identity

        • (element: T): number
        • Parameters

          • element: T

          Returns number

    Returns T

    The largest element in the sequence according to the keySelector or null if the sequence was empty

min

  • min(keySelectorFunction?: (element: T) => number): T
  • Method for retrieving the smallest element from the sequence Note: This function returns null if the sequence is empty

    Parameters

    • Optional keySelectorFunction: (element: T) => number

      Function used for extracting the key from the object to compare against, defaults to identity

        • (element: T): number
        • Parameters

          • element: T

          Returns number

    Returns T

    The smallest element in the sequence according to the keySelector or null if the sequence was empty

partitionBy

  • partitionBy(predicateFunction: (element: T) => boolean): [T[], T[]]
  • Method for terminating the sequence and partitioning the elements into 2 arrays according to the given predicate

    Parameters

    • predicateFunction: (element: T) => boolean

      The predicate to test against

        • (element: T): boolean
        • Parameters

          • element: T

          Returns boolean

    Returns [T[], T[]]

    2 arrays where the first one contains the elements that matched the predicate and the second one that didn't

reduce

  • reduce<TT>(seed: TT, accumulatorFunction: (accumulator: TT, element: T) => TT): TT
  • Method for terminating the sequence and peforming a reduction on the elements of the sequence

    Type parameters

    • TT

    Parameters

    • seed: TT

      Used as an 'initial' or 'base' value

    • accumulatorFunction: (accumulator: TT, element: T) => TT

      Function used for combining the accumulator and the current element

        • (accumulator: TT, element: T): TT
        • Parameters

          • accumulator: TT
          • element: T

          Returns TT

    Returns TT

    Result of the reduction

skip

  • Method for creating a new sequence while skipping 'n' number of elements

    Parameters

    • count: number

      Number of elements to skip

    Returns Sequence<T>

    New sequence with the first 'n' elements skipped

skipWhile

  • skipWhile(predicateFunction: (element: T) => boolean): Sequence<T>
  • Method for creating a new sequence that skips elements until the given predicate returns true

    Parameters

    • predicateFunction: (element: T) => boolean

      The function to test against

        • (element: T): boolean
        • Parameters

          • element: T

          Returns boolean

    Returns Sequence<T>

    New sequence that skips elements until the given predicate returns true

sort

  • sort(comparerFunction: (element1: T, element2: T) => number): Sequence<T>
  • Method for creating a new sequence where the elements are sorted based on the given comparer

    Parameters

    • comparerFunction: (element1: T, element2: T) => number

      This function has the same properties as the comparer function given to Array.sort

        • (element1: T, element2: T): number
        • Parameters

          • element1: T
          • element2: T

          Returns number

    Returns Sequence<T>

    New sequence with elements are sorted based on the given comparer

sortAscending

  • sortAscending(keySelectorFunction?: (element: T) => any): Sequence<T>
  • Method for creating a new sequence where the elements are sorted in ascending order based on the given keySelector

    Parameters

    • Optional keySelectorFunction: (element: T) => any

      Function for selecting a key property to be used for sorting, defaults to identity

        • (element: T): any
        • Parameters

          • element: T

          Returns any

    Returns Sequence<T>

    New sequence with elements sorted in ascending order based on the given keySelector

sortDescending

  • sortDescending(keySelectorFunction?: (element: T) => any): Sequence<T>
  • Method for creating a new sequence where the elements are sorted in descending order based on the given keySelector

    Parameters

    • Optional keySelectorFunction: (element: T) => any

      Function for selecting a key property to be used for sorting, defaults to identity

        • (element: T): any
        • Parameters

          • element: T

          Returns any

    Returns Sequence<T>

    New sequence with elements sorted in descending order based on the given keySelector

statistics

  • Method for terminating the sequence and calculating the statistics of the elements of the sequence

    Returns NumberStatistics

    Object that contains the sum, count, min, max and average of the sequence

sum

  • sum(): number
  • Method for terminating the sequence while calculating the sum of the elements

    Returns number

    Sum of the elements in the sequence

take

  • Method for creating a new sequence with a specific amount of elements

    Parameters

    • count: number

      Number of elements to keep in the sequence

    Returns Sequence<T>

    New sequence containing the first 'n' number of elements

takeWhile

  • takeWhile(predicateFunction: (element: T) => boolean): Sequence<T>
  • Method for creating a new sequence that takes elements until the given predicate returns false

    Parameters

    • predicateFunction: (element: T) => boolean

      The function to test against

        • (element: T): boolean
        • Parameters

          • element: T

          Returns boolean

    Returns Sequence<T>

    New sequence that takes elements until the given predicate returns false

toArray

  • toArray(): T[]
  • Method for terminating the sequence and collecting the elements of the sequence into an array

    Returns T[]

    An array containing the elements of the sequence

toMap

  • toMap<K, V>(keySelectorFunction: (element: T) => K, valueSelectorFunction: (element: T) => V, duplicateResolverFunction?: (key: K, previousValue: V, currentValue: V) => V): Map<K, V>
  • Method for terminating the sequence and collecting the elements into an object using the key & value selector functions

    Type parameters

    • K

    • V

    Parameters

    • keySelectorFunction: (element: T) => K

      Function used for extracting the key from the object

        • (element: T): K
        • Parameters

          • element: T

          Returns K

    • valueSelectorFunction: (element: T) => V

      Function used for extracting the value from the object

        • (element: T): V
        • Parameters

          • element: T

          Returns V

    • Optional duplicateResolverFunction: (key: K, previousValue: V, currentValue: V) => V

      Takes 3 arguments: the key, the old value and the value we're trying to insert at the moment. Defaults to throwing an error. Return value is the value that gets inserted as the value of the duplicate key. Default is to throw an error

        • (key: K, previousValue: V, currentValue: V): V
        • Parameters

          • key: K
          • previousValue: V
          • currentValue: V

          Returns V

    Returns Map<K, V>

    An object where the keys are populated using the keySelector and the corresponding values are the values returned by the valueSelector

Static empty

  • Function for creating an empty sequence

    Type parameters

    • T

    Returns Sequence<T>

    Sequence with 0 elements

Static from

  • Function for creating a sequence of elements using the passed in array source

    Type parameters

    • T

    Parameters

    • elements: T[]

      Input elements

    Returns Sequence<T>

    Sequence populated from the input array

  • Function for creating a sequence of elements using the passed in elements as the source

    Type parameters

    • T

    Parameters

    • Rest ...elements: T[]

      Input elements

    Returns Sequence<T>

    Sequence populated from the input elements

Static generate

  • generate<T>(generatorFunction: () => T): Sequence<T>
  • Function for creating a sequence of elements using the input generator Note: This generates an infinite sequence

    Type parameters

    • T

    Parameters

    • generatorFunction: () => T

      A function that when called returns an element

        • (): T
        • Returns T

    Returns Sequence<T>

    Sequence with elements being populated lazily from the input generatorFunction

Static iterate

  • iterate<T>(seed: T, generatorFunction: (element: T) => T, limiterPredicateFunction?: (element: T) => boolean): Sequence<T>
  • Function for creating a sequence of elements using the seed as a base value and then applying the generator function to it, until the limiterPredicateFunction returns false Note: This generates an infinite sequence if the last parameter is ommited

    Type parameters

    • T

    Parameters

    • seed: T

      The initial value of the sequence

    • generatorFunction: (element: T) => T

      Function to generate the next element of the sequence

        • (element: T): T
        • Parameters

          • element: T

          Returns T

    • Optional limiterPredicateFunction: (element: T) => boolean

      Optional parameter, defaults to always returning to true (meaning that it's infinite)

        • (element: T): boolean
        • Parameters

          • element: T

          Returns boolean

    Returns Sequence<T>

    Sequence with elements being populated lazily

Static range

  • range(begin: number, end: number, step?: number): Sequence<number>
  • Function for creating a sequence of numbers using a non inclusive number range generator

    Parameters

    • begin: number

      The first value of the range

    • end: number

      The last value of the range

    • Optional step: number

      Default is 1

    Returns Sequence<number>

    Sequence from begin-to stepping with increment

Static rangeClosed

  • rangeClosed(begin: number, end: number, step?: number): Sequence<number>
  • Function for creating a sequence of numbers using an inclusive number range generator

    Parameters

    • begin: number

      The first value of the range

    • end: number

      The last value of the range

    • Optional step: number

      Default is 1

    Returns Sequence<number>

    Sequence from begin-to stepping with step

Generated using TypeDoc