It's map
, the most important method in programming! Exactly the same as ES6 map
, but lazy.
{% tabs %} {% tab title="Usage" %}
const sequence: Seq<string> = Seq.infinite().map(num => num.toString())
{% endtab %}
{% tab title="Type Definition" %}
type map = <U>(fn: (value: T, index: number) => U) => Seq<U>
{% endtab %} {% endtabs %}
window
takes a sequence and groups it into "windows" of a certain length. This works well with infinite sequences where you want to process some number of values at a time.
{% tabs %} {% tab title="Usage" %}
// Grab numbers in groups of 10.
const sequence: Seq<number[]> = Seq.infinite().window(10)
By default, only triggers chained responses when the window fills, guaranteeing the window is the exact size expect. Set allowPartialWindow
to false
to allow the trailing edge of a sequence to not be divisible by the window size.
// Gives: [0, 1, 2] -> [3, 4, 5] -> [6, 7, 8] -> [9, 10]
const sequence: Seq<number> = Seq.range(0, 10).window(3)
{% endtab %}
{% tab title="Type Definition" %}
type window = (size: number, allowPartialWindow = true) => Seq<T[]>
{% endtab %} {% endtabs %}
Works like window
, makes the window size 2. Groups a sequence as alternating pairs. Useful for processing data which alternates Map keys and values.
{% tabs %} {% tab title="Usage" %}
const sequence: Seq<[string, number]> = Seq.fromArray(["a", 1, "b", 2])
{% endtab %}
{% tab title="Type Definition" %}
type pairwise = () => Seq<[T, T]>
{% endtab %} {% endtabs %}
Ask whether a sequence is empty.
{% tabs %} {% tab title="Usage" %}
const anythingInThere: boolean = Seq.empty().isEmpty()
{% endtab %}
{% tab title="Type Definition" %}
type isEmpty = () => boolean
{% endtab %} {% endtabs %}
tap
lets you run side-effect generating functions on a sequence. Allows you to "tap in to" a data flow. Very useful for logging and debugging what values are flowing through the chain at a given location.
{% tabs %} {% tab title="Usage" %}
const sequence: Seq<number> = Seq.infinite().tap(num => console.log(num))
{% endtab %}
{% tab title="Type Definition" %}
type tap = (fn: (value: T, index: number) => void) => Seq<T>
{% endtab %} {% endtabs %}
log
provides the most common use-case for `tap. Add this to a sequence chain to log each value that passes through it.
{% tabs %} {% tab title="Usage" %}
const sequence: Seq<number> = Seq.infinite().log()
{% endtab %}
{% tab title="Type Definition" %}
type log = () => Seq<T>
{% endtab %} {% endtabs %}
Given a sequence where each item in an array of items, flatten all those arrays into a single flat sequence of values.
Works just like Array.prototype.flat
. See more here.
{% tabs %} {% tab title="Usage" %}
type Person = { name: string; friends: Person[] }
const sequence: Seq<Friend> = Seq.fromArray([person1, person2])
.map(person => person.friends)
.flat()
{% endtab %}
{% tab title="Type Definition" %}
type flat = <U>(this: Seq<U[]>) => Seq<U>
{% endtab %} {% endtabs %}
flatMap
is used when mapping a list to each items related items. For example, if you wanted to map from a list of people to each persons list of friends. Despite each mapping function returning an array, the final output is a flatten array of all the results concattenated.
Works just like Array.prototype.flatMap
. See more here.
Similar to [].map().flat()
, but in leisure
the item mappings won't execute until enough of the resulting values have been realized to trigger each map.
{% tabs %} {% tab title="Usage" %}
type Person = { name: string; friends: Person[] }
const sequence: Seq<Friend> = Seq.fromArray([person1, person2]).flatMap(
person => person.friends,
)
{% endtab %}
{% tab title="Type Definition" %}
type flatMap = <U>(fn: (value: T, index: number) => U[]) => Seq<U>
{% endtab %} {% endtabs %}
Runs a predicate function on each item in a sequence to produce a new sequence where only the values which responded with true
remain.
Exactly the same as Array.prototype.filter
, but lazy. See more here.
{% tabs %} {% tab title="Usage" %}
// Create a sequence of only even numbers.
const sequence: Seq<number> = Seq.infinite().filter(num => num % 2 === 0)
{% endtab %}
{% tab title="Type Definition" %}
type filter = (fn: (value: T, index: number) => unknown) => Seq<T>
{% endtab %} {% endtabs %}
Combines the current sequence with 1 or more additional sequences.
Exactly the same as Array.prototype.concat
, but lazy. See more here.
{% tabs %} {% tab title="Usage" %}
const sequence: Seq<number> = Seq.fromArray([0, 1]).concat(
Seq.fromArray([2, 3]),
Seq.fromArray([4, 5]),
)
{% endtab %}
{% tab title="Type Definition" %}
type concat = (...tail: Array<Seq<T>>) => Seq<T>
{% endtab %} {% endtabs %}
Takes 1 or more sequences and creates a new sequence built by pulling the next value from each of the sequences in order.
{% tabs %} {% tab title="Usage" %}
// Builds: a -> 1 -> b -> 2 -> c -> 3
const sequence: Seq<string | number> = Seq.fromArray([
"a",
"b",
"c",
]).interleave(Seq.range(1, 3))
{% endtab %}
{% tab title="Type Definition" %}
type interleave = (...tail: Array<Seq<T>>) => Seq<T>
{% endtab %} {% endtabs %}
Given a sequence, place a value between each value of the original sequence. Useful for adding punctuation between strings.
{% tabs %} {% tab title="Usage" %}
// Builds: Apples -> , -> Oranges -> , -> Bananas
const sequence: Seq<string> = Seq.fromArray([
"Apples",
"Oranges",
"Bananas",
]).interpose(", ")
console.log(sequence.toArray().join(""))
{% endtab %}
{% tab title="Type Definition" %}
type interpose = (separator: T) => Seq<T>
{% endtab %} {% endtabs %}
Given a sequence, only forwards the values which have no already been seen. Very similar to lodash's uniq
method.
{% tabs %} {% tab title="Usage" %}
// Builds: 1 -> 2 -> 3 -> 4
const sequence: Seq<number> = Seq.fromArray([1, 2, 3, 2, 1, 4]).distinct()
{% endtab %}
{% tab title="Type Definition" %}
type distinct = () => Seq<T>
{% endtab %} {% endtabs %}
Same as distinct
, but allows a function to describe on what value the sequence should be unique.
{% tabs %} {% tab title="Usage" %}
// Builds: { firstName: "A", lastName: "Z" } ->
// { firstName: "B", lastName: "Y" } ->
// { firstName: "C", lastName: "W" }
type Person = { firstName: string; lastName: string }
const sequence: Seq<Person> = Seq.fromArray([
{ firstName: "A", lastName: "Z" },
{ firstName: "B", lastName: "Y" },
{ firstName: "A", lastName: "X" },
{ firstName: "C", lastName: "W" },
]).distinctBy(person => person.firstName)
{% endtab %}
{% tab title="Type Definition" %}
type distinctBy = <U>(fn: (value: T) => U) => Seq<T>
{% endtab %} {% endtabs %}
Given a sequence, splits the values into two separate sequences. One represents the values where the partition function is true
and the other for false
.
{% tabs %} {% tab title="Usage" %}
const [isEven, isOdd] = Seq.infinite().partitionBy(num => num % 2 === 0)
{% endtab %}
{% tab title="Type Definition" %}
type partition = (fn: (value: T, index: number) => unknown) => [Seq<T>, Seq<T>]
{% endtab %} {% endtabs %}
Lazily checks if the sequence includes a value.
Exactly the same as Array.prototype.includes
, but lazy. See more here.
{% tabs %} {% tab title="Usage" %}
const doesItInclude = Seq.infinite().includes(10)
{% endtab %}
{% tab title="Type Definition" %}
type includes = (value: T) => boolean
{% endtab %} {% endtabs %}
Lazily searches for a value that matches the predicate.
Exactly the same as Array.prototype.find
, but lazy. See more here.
{% tabs %} {% tab title="Usage" %}
// Returns 11
const gtTen = Seq.infinite().find(num => num > 10)
{% endtab %}
{% tab title="Type Definition" %}
type find = (fn: (value: T, index: number) => unknown) => T | undefined
{% endtab %} {% endtabs %}
Exactly the same as Array.prototype.reduce
. See more here. This causes a full realization of the data. Not lazy.
{% tabs %} {% tab title="Usage" %}
// Returns 0 + 1 + 2 + 3 + 4 = 10
const sum = Seq.infinite()
.take(5)
.reduce((sum, num) => sum + num)
{% endtab %}
{% tab title="Type Definition" %}
type reduce = <A>(fn: (sum: A, value: T, index: number) => A, initial: A) => A
{% endtab %} {% endtabs %}
This method is helpful for chaining. Shocking, I know. Let's you "map" the entire sequence in a chain, rather than per-each-item. Allows adding arbitrary sequence helpers and methods to chain, even if they are written in user-land and not on the Seq
prototype.
{% tabs %} {% tab title="Usage" %}
// Same as `Seq.interpose(Seq.infinite(), Seq.infinite())`
const sequence = Seq.infinite().chain(seq => seq.interpose(Seq.infinite()))
{% endtab %}
{% tab title="Type Definition" %}
type chain = <U>(fn: (value: Seq<T>) => U) => U
{% endtab %} {% endtabs %}
Exactly the same as Array.prototype.some
, but lazy. See more here.
{% tabs %} {% tab title="Usage" %}
// Find the first even random number.
const areAnyEven = Seq.random()
.map(num => Math.round(num * 1000))
.some(num => num % 2 === 0)
{% endtab %}
{% tab title="Type Definition" %}
type some = (fn: (value: T, index: number) => unknown) => boolean
{% endtab %} {% endtabs %}
Exactly the same as Array.prototype.every
, but lazy. See more here.
{% tabs %} {% tab title="Usage" %}
// Fails fast if there are negative numbers
const areAllPositive = Seq.random()
.map(num => Math.round(num * 1000) - 500)
.every(num => num > 0)
{% endtab %}
{% tab title="Type Definition" %}
type every = (fn: (value: T, index: number) => unknown) => boolean
{% endtab %} {% endtabs %}
Given a sequence of unknown length, create a sub sequence of just the first X number of items.
{% tabs %} {% tab title="Usage" %}
// Grabs 0 -> 1 -> 2 -> 3 -> 4
const firstFive = Seq.infinite().take(5)
{% endtab %}
{% tab title="Type Definition" %}
type take = (num: number) => Seq<T>
{% endtab %} {% endtabs %}
Given a sequence of unknown length, create a sub sequence of as many items in a row that satisfy the predicate.
{% tabs %} {% tab title="Usage" %}
// Gives 0 -> 1 -> 2 -> 3 -> 4
const lessThanFive = Seq.infinite().takeWhile(num => num < 5)
{% endtab %}
{% tab title="Type Definition" %}
type takeWhile = (fn: (value: T, index: number) => unknown) => Seq<T>
{% endtab %} {% endtabs %}
Given a sequence of unknown length, skips the first X number of items.
{% tabs %} {% tab title="Usage" %}
// Gives 5 -> 6 -> 7 -> 8 -> 9
const secondFive = Seq.infinite().skip(5).take(5)
{% endtab %}
{% tab title="Type Definition" %}
type skip = (num: number) => Seq<T>
{% endtab %} {% endtabs %}
Given a sequence of unknown length, skip as many items in a row that satisfy the predicate.
{% tabs %} {% tab title="Usage" %}
// Gives 5 -> 6 -> 7 -> 8 -> 9
const greaterThanFive = Seq.infinite()
.skipWhile(num => num < 5)
.take(5)
{% endtab %}
{% tab title="Type Definition" %}
type skipWhile = (fn: (value: T, index: number) => unknown) => Seq<T>
{% endtab %} {% endtabs %}
Returns the nth
item. Items are 1-indexed.
{% tabs %} {% tab title="Usage" %}
const thirdItem = Seq.infinite().nth(3)
{% endtab %}
{% tab title="Type Definition" %}
type nth = (i: number) => T | undefined
{% endtab %} {% endtabs %}
Returns the index
item. Items are 0-indexed.
{% tabs %} {% tab title="Usage" %}
const fourthItem = Seq.infinite().index(3)
{% endtab %}
{% tab title="Type Definition" %}
type nth = (i: number) => T | undefined
{% endtab %} {% endtabs %}
Gets the first value in the sequence.
{% tabs %} {% tab title="Usage" %}
const fifth = Seq.infinite().skip(4).first()
{% endtab %}
{% tab title="Type Definition" %}
type first = () => T | undefined
{% endtab %} {% endtabs %}
Lazily combines a second sequence with this current one to produce a tuple with the current step in each of the two positions. Useful for zipping a sequence of keys with a sequence of values, before converting to a Map of key to value.
{% tabs %} {% tab title="Usage" %}
const seq2 = Seq.range(0, 3)
// Gives: ["zero", 0] -> ["one", 1] -> ["two", 2] -> ["three", 3]
const sequence: Seq<[string, number]> = Seq.fromArray([
"zero",
"one",
"two",
"three",
]).zip(seq2)
{% endtab %}
{% tab title="Type Definition" %}
type zip<T2> = (seq2: Seq<T2>) => Seq<[T | undefined, T2 | undefined]>
{% endtab %} {% endtabs %}
Takes a second sequence and lazily combines it to produce an arbitrary value by mapping the current value of the two positions through a user-supplied function. Useful for table (row/col) math.
{% tabs %} {% tab title="Usage" %}
const seq2 = Seq.repeat(2)
// Gives: 0 -> 2 -> 4 -> 6
const sequence: Seq<number> = Seq.range(0, 3).zipWith(
([num, multiplier]) => num * multiplier,
seq2,
)
{% endtab %}
{% tab title="Type Definition" %}
type zip2With = <T2, T3, T4>(
fn: (
[result1, result2, result3]:
| [T, T2, T3]
| [T, undefined, undefined]
| [T, T2, undefined]
| [T, undefined, T3]
| [undefined, T2, undefined]
| [undefined, T2, T3]
| [undefined, undefined, T3],
index: number,
) => T4,
seq2: Seq<T2>,
seq3: Seq<T3>,
) => Seq<T4>
{% endtab %} {% endtabs %}
Takes two sequences and lazily combines them with this one to produce a 3-tuple with the current step in each of the three positions.
{% tabs %} {% tab title="Usage" %}
const seq2 = Seq.range(0, 3)
const seq3 = Seq.range(3, 0)
// Gives: ["zero", 0, 3] -> ["one", 1, 2] -> ["two", 2, 1] -> ["three", 3, 0]
const sequence: Seq<[string, number]> = Seq.fromArray([
"zero",
"one",
"two",
"three",
]).zip2(seq2, seq3)
{% endtab %}
{% tab title="Type Definition" %}
type zip2 = <T2, T3>(
seq2: Seq<T2>,
seq3: Seq<T3>,
) => Seq<[T | undefined, T2 | undefined, T3 | undefined]>
{% endtab %} {% endtabs %}
Takes two sequences and lazily combine them with this sequence to produce an arbitrary value by mapping the current value of the three positions through a user-supplied function.
{% tabs %} {% tab title="Usage" %}
const seq2 = Seq.repeat(2)
const seq3 = Seq.repeat(1)
// Gives: 0 -> 2 -> 4 -> 6
const sequence: Seq<number> = Seq.range(0, 3).zip2With(
([num, multiplier, divisor]) => (num * multiplier) / divisor,
seq2,
seq3,
)
{% endtab %}
{% tab title="Type Definition" %}
type zip2With = <T2, T3, T4>(
fn: (
[result1, result2, result3]:
| [T, T2, T3]
| [T, undefined, undefined]
| [T, T2, undefined]
| [T, undefined, T3]
| [undefined, T2, undefined]
| [undefined, T2, T3]
| [undefined, undefined, T3],
index: number,
) => T4,
seq2: Seq<T2>,
seq3: Seq<T3>,
) => Seq<T4>
{% endtab %} {% endtabs %}
Converts the sequence to a real JavaScript array. Realizes the entire sequence.
{% tabs %} {% tab title="Usage" %}
const lessThanTen = Seq.infinite().take(10).toArray()
{% endtab %}
{% tab title="Type Definition" %}
type toArray = () => T[]
{% endtab %} {% endtabs %}
Works just like Array.prototype.forEach
. See more here. Realizes the full sequence.
{% tabs %} {% tab title="Usage" %}
{% endtab %}
{% tab title="Type Definition" %}
type forEach = (fn: (value: T, index: number) => void) => void
{% endtab %} {% endtabs %}
Given a sequence of numbers, adds them all together. This realizes the entire sequence.
{% tabs %} {% tab title="Usage" %}
// Returns 0 + 1 + 2 + 3 + 4 = 10
const sum = Seq.infinite().take(5).sum()
{% endtab %}
{% tab title="Type Definition" %}
type sum = (this: Seq<number>) => number
{% endtab %} {% endtabs %}
Given a sequence of arbitrary data, adds together the result of the mapping function. This realizes the entire sequence.
{% tabs %} {% tab title="Usage" %}
// Returns 0 + 1 + 2 + 3 + 4 = 10
const sum = Seq.fromArray([
{ balance: 0 },
{ balance: 1 },
{ balance: 2 },
{ balance: 3 },
{ balance: 4 },
]).sumBy(user => user.balance)
{% endtab %}
{% tab title="Type Definition" %}
```typescript
type sumBy = (fn: (value: T) => number) => number;
{% endtab %} {% endtabs %}
Given a sequence of numbers, averages them all together. Tise realizes the entire sequence.
{% tabs %} {% tab title="Usage" %}
// Returns (0 + 1 + 2 + 3 + 4) / 5 = 2
const sum = Seq.infinite().take(5).average()
{% endtab %}
{% tab title="Type Definition" %}
type average = (this: Seq<number>) => number
{% endtab %} {% endtabs %}
Given a sequence of arbitrary data, averages together the result of the mapping function. This realizes the entire sequence.
{% tabs %} {% tab title="Usage" %}
// Returns (0 + 1 + 2 + 3 + 4) / 5 = 2
const sum = Seq.fromArray([
{ balance: 0 },
{ balance: 1 },
{ balance: 2 },
{ balance: 3 },
{ balance: 4 },
]).averageBy(user => user.balance)
{% endtab %}
{% tab title="Type Definition" %}
type averageBy = (fn: (value: T) => number) => number
{% endtab %} {% endtabs %}
Given a non-infinite sequence, return a Map
which counts the occurances of each unique value. This realizes the entire sequence.
{% tabs %} {% tab title="Usage" %}
// Returns a Map of numbers from 0->100 and how many times they randomly occured in this set of 500.
const freq = Seq.random()
.map(num => Math.round(num * 100))
.take(500)
.frequencies()
{% endtab %}
{% tab title="Type Definition" %}
type frequencies = () => Map<T, number>
{% endtab %} {% endtabs %}
Group a sequence by the return of a mapping function. This realizes the entire sequence.
{% tabs %} {% tab title="Usage" %}
// Random generates 1000 years between 0-2000 and
// groups them by decade.
const groupedByDecade = Seq.random()
.map(num => Math.round(num * 2000))
.take(100)
.groupBy(year => Math.round(year / 10))
{% endtab %}
{% tab title="Type Definition" %}
type groupBy = <U>(fn: (item: T) => U) => Map<U, T[]>
{% endtab %} {% endtabs %}