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.
open Seq
let users = [
{ name: "Frida", groupIds: [12, 144] },
{ name: "Hassan", groupIds: [30, 1] }
];
return users.collect(u => u.groupIds); // returns [12, 144, 30, 1]
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.
open Seq;
let employees = [
{ name: "Birgitta", location: null },
{ name: "Comanche", location: "USA" },
{ name: "Theodor", location: "Germany" }
];
return employees.choose(e => e.location); // returns ["USA", "Germany"]
The choose function also is useful for simply filtering out the null values from a sequence of nullable values:
open Seq;
let xs = [1, 2, null, 3];
return xs.choose(x => x);
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.
open Seq;
let boxes = [
{ id: 1, weight: 100 },
{ id: 2, weight: 25 },
{ id: 3, weight: 300 }
]
return boxes.filter(b => b.weight > 50);
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.
open Seq;
let users = [
{ id: 0, department: 'IT' },
{ id: 1, department: 'Manufacturing' },
{ id: 2, department: 'IT' }
];
// Group the users by department
return users.groupBy(u => u.department);
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.
open Seq;
let xs = [1, 2, 4];
function isEven(n: number) => n mod 2 == 0;
// Return a value indicating whether all the numbers in "xs" are even.
return xs.forAll(x => isEven(x));
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.
open Seq;
let xs = [1, 2, 4];
function isEven(n: number) => n mod 2 == 0;
// Return a value indicating whether any numbers in "xs" is even.
return xs.exists(x => isEven(x));
takeWhile
Signature: (self sequence: T*, predicate: T -> boolean) -> T*
Returns all elements from the beginning of the sequence for which the predicate returns true.
open Seq;
let numbers = range(1, 1000);
return numbers.takeWhile(n => n < 10);
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.
open Seq;
let numbers = range(1, 10);
return numbers.skipWhile(n => n < 5);
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.
open Seq;
let orders = [
{ orderNo: 'B23455', customerId: 1120 },
{ orderNo: 'B23456', customerId: 3450 }
];
// Add a number to each order
return orders.indexed();
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),.
open Seq;
let posts = [
{ userId: 1, title: "Lorem ipsum" },
{ userId: 1, title: "Dolor sit amet" },
{ userId: 2, title: "Consectetur adipiscing elit" }
]
// Create a lookup from user ID to posts by that user
let lookup = posts.toLookup(p => p.userId);
// Return all posts created by used ID 1
return lookup(1);
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.
// Use fold to implement a function returning the maximum
// of a sequence of nonnegative numbers:
open Seq;
function maximum(self xs: number*) => xs.fold(0, max);
return [1, 5, 2].maximum();
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.
open Seq;
// Calculate a cumulative sum using the Scan function
let numbers = [1, 2, 3];
// For each number in the sequence, add it to the sum of the previous numbers.
return numbers.scan(0, (acc, next) => acc + next); // result will be [0, 1, 3, 6]
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.
open Seq;
// Create the score card for players 1 and 2.
let player1Score = [1, 0, 0, 3];
let player2Score = [6, 2, 0, 0];
// Calculate the maximum score for each round
let maxScorePerRound = player1Score.zip(player2Score, max);
// Calculate the total score for each round
let totalScorePerRound = player1Score.zip(player2Score, (a, b) => a + b);
return { maxScorePerRound, totalScorePerRound };
reverse
Signature: (self sequence: T*) -> T*
Returns a cope of the sequence with elements in reverse order.
open Seq;
return ["A", "B", "C"].reverse() // returns ["C", "B", "A"]
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.
open Seq;
// Replace the element at index 1 with "_"
return ["A", "B", "C"].updateAt(1, "_"); // returns ["A", "_", "C"]
Last updated
Was this helpful?