Seq module

The Seq (short for "sequence") module contains functions for working with sequences of data.

Seq module functions

map

Signature: (self sequence: T*, mapper: T -> U) -> U*

Calls the mapper function on each element of the original sequence, returning a new lazy sequence with the results.

open Seq;

let languages = [
     { name: "Swedish", code: "sv" },
     { name: "Tamil", code: "ta" },
     { name: "Dutch", code: "nl" },
]

return languages.map(l => l.name); // returns ["Swedish", "Tamil", "Dutch"]

collect

Signature: (self sequence: T*, collector: T -> U*) -> U*

Calls a collector function on each element of the original sequence, expecting some sequence as a result. The resulting sequences for each element are flattened into one single sequence.

If you are familiar with other programming languages, you may recognize this as the "flatMap" or "SelectMany" function.

choose

Signature: (self sequence: T*, choose: T -> U?) -> U*

Calls the chooser function on each element of the original sequence, returning a new sequence containing all the results that are not null.

The choose function also is useful for simply filtering out the null values from a sequence of nullable values:

filter

Signature: (self sequence: T*, predicate: T -> boolean) -> T*

Calls the predicate on each element of the original sequence, returning a new lazy sequence containing the elements for which the predicate return true.

groupBy

Signature: (self sequence: T*, keySelecor: T -> U) -> { key: U, values: T* }*

Groups the elements in the sequence by the key returned by the keySelector function, returning a new sequence where each group is a record containing the group key and the sequence of elements with that group key.

forAll

Signature: (self sequence: T*, predicate: (T -> boolean)) -> boolean

Returns a value indicating whether all elements in the sequence satisfy the predicate. If the sequence is empty, the function returns true.

exists

Signature: (self sequence: T*, predicate: (T -> boolean)) -> boolean

Returns a value indicating whether at least one element in the sequence satisfies the predicate. If the sequence is empty, the function returns false.

takeWhile

Signature: (self sequence: T*, predicate: T -> boolean) -> T*

Returns all elements from the beginning of the sequence for which the predicate returns true.

skipWhile

Signature: (self sequence: T*, predicate: T -> boolean) -> T*

Discards the elements at the beginning of the sequence for the predicate returns true, returning all remaining elements.

indexed

Signature: (self sequence: T*) -> { index: number, value: T }*

Wraps each element in a record containing a (0-based) element number and the original element.

toLookup

Signature: (self sequence: T*, keySelector: T -> U) -> (U -> T*)

Creates a lookup function which allows fast access to subsets of a sequence by some key. Conceptually similar to grouping, the toLookup function is a good optimization tool if the same sequence is accessed by some key often (in a loop, for example),.

fold

Signature: (self sequence: T*, seed: U, folder: (U, T) -> U) -> U

Calls the folder function on each successive element of the sequence, threading the result of the previous element into the next and using the seed value for the first item.

The fold function is famously expressive and can be used to implement many other functions.

scan

Signature: (self sequence: T*, seed: U, folder: (U, T) -> U) -> U*

Similar to fold but returns a sequence of each intermediate state created during the folding process.

zip

Signature: (self sequence: T*, other: U*, combiner: (T, U) -> V): V*

Places two sequences side-by-side and uses the combiner function on each successive pair of elements to create some resulting value.

reverse

Signature: (self sequence: T*) -> T*

Returns a cope of the sequence with elements in reverse order.

updateAt

Signature: (self sequence: T*, index: number, replacement: T) -> T*

Returns a copy of the sequence with the element at the position indicated by the zero-based index parameter replaced by the replacement value.

distinctBy

Signature: (self sequence: T*, keySelector: T -> primitive) -> T*

Returns a sequence of distinct elements based on the specified key selector function.

Last updated

Was this helpful?