# Static Methods

Takes a normal JavaScript array and turns it into a Sequence of the same type.

Usage

Type Definition

const sequence: Seq<number> = fromArray([1, 2, 3])

type fromArray<T> = (data: T[]) => Seq<T>

The

`iterate`

method simplifies the construction of infinite sequences which are built using their previous value.Usage

Type Definition

// Builds an infinite sequence that counts up from 0.

const sequence: Seq<number> = iterate(a => a + 1, 0)

type iterate = <T>(fn: (current: T) => T, start: T): Seq<T>

The

`fib`

method generates a sequence of fibonacci numbers.Usage

Type Definition

const sequence: Seq<number> = fib()

type fib = () => Seq<number>

Generates a random sequence using

`Math.random`

.Usage

Type Definition

// Builds sequence of random numbers between 0 and 1.

const sequence: Seq<number> = random()

type random = () => Seq<number>

Creates a sequence that counts between a start and end value. Takes an optional step parameter.

Usage

Type Definition

// Step by 1 from 0 through 10.

const sequence: Seq<number> = range(0, 10)

// Step by 1 from 0 through Infinity.

const sequence: Seq<number> = range(0, Infinity)

// Step by 1 from -10 through +10.

const sequence: Seq<number> = range(-10, 10)

// Step down from +10 through -10.

const sequence: Seq<number> = range(10, -10)

// Step by 2 from 0 through 10.

const sequence: Seq<number> = range(0, 10, 2)

type range(

start: number,

end: number,

step = 1

) => Seq<number>;

A sequence that counts from

`0`

to `Infinity`

.Usage

Type Definition

const sequence: Seq<number> = infinite()

type infinite = () => Seq<number>

A sequence with only a single value inside. Also known as "singleton."

Usage

Type Definition

const sequence: Seq<number> = of(2)

type of = <T>(...values: T[]): Seq<T>;

Create a sequence that is the infinite repetition of a series of values.

Usage

Type Definition

const sequence: Seq<string> = cycle(["a", "b", "c"])

type cycle = <T>(items: T[]) => Seq<T>

Create a sequence which repeats the same value X number of times. The length of the sequence defaults to Infinity.

Usage

Type Definition

// Creates a sequence of 5 true values.

const sequence: Seq<boolean> = repeat(true, 5)

// Creates a sequence of 0 which repeats infinitely.

const sequence: Seq<number> = repeat(0)

type repeat = <T>(value: T, times = Infinity) => Seq<T>

Creates a sequence which pulls from an impure callback to generate the sequence. The second parameter can cap the length of the sequence. By default, it will call the callback infinitely to generate values. Useful for asking about current time or cache values.

Usage

Type Definition

const sequence: Seq<Date> = repeatedly(() => new Date())

type repeatedly = <T>(value: () => T, times = Infinity) => Seq<T>

A sequence with nothing in it. Useful as a "no op" for certain code-paths when joining sequences together.

Usage

Type Definition

const sequence: Seq<never> = empty()

type empty = () => Seq<never>

Takes two sequences and lazily combines them 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.

Usage

Type Definition

const seq1 = fromArray(["zero", "one", "two", "three"])

const seq2 = range(0, 3)

// Gives: ["zero", 0] -> ["one", 1] -> ["two", 2] -> ["three", 3]

const sequence: Seq<[string, number]> = zip(seq1, seq2)

type zip = <T1, T2>(

seq1: Seq<T1>,

seq2: Seq<T2>,

) => Seq<[T1 | undefined, T2 | undefined]>

Takes two sequences and lazily combines them 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.

Usage

Type Definition

const seq1 = range(0, 3)

const seq2 = repeat(2)

// Gives: 0 -> 2 -> 4 -> 6

const sequence: Seq<number> = zipWith(

([num, multiplier]) => num * multiplier,

seq1,

seq2,

)

type zipWith = <T1, T2, T3>(

fn: (

[result1, result2]: [T1, T2] | [T1, undefined] | [undefined, T2],

index: number,

) => T3,

seq1: Seq<T1>,

seq2: Seq<T2>,

) => Seq<T3>

Takes three sequences and lazily combines them to produce a 3-tuple with the current step in each of the three positions.

Usage

Type Definition

const seq1 = fromArray(["zero", "one", "two", "three"])

const seq2 = range(0, 3)

const seq3 = range(3, 0)

// Gives: ["zero", 0, 3] -> ["one", 1, 2] -> ["two", 2, 1] -> ["three", 3, 0]

const sequence: Seq<[string, number]> = zip3(seq1, seq2, seq3)

type zip3 = <T1, T2, T3>(

seq1: Seq<T1>,

seq2: Seq<T2>,

seq3: Seq<T3>,

) => Seq<[T1 | undefined, T2 | undefined, T3 | undefined]>

Takes three sequences and lazily combines them to produce an arbitrary value by mapping the current value of the three positions through a user-supplied function.

Usage

Type Definition

const seq1 = range(0, 3)

const seq2 = repeat(2)

const seq3 = repeat(1)

// Gives: 0 -> 2 -> 4 -> 6

const sequence: Seq<number> = zip3With(

([num, multiplier, divisor]) => (num * multiplier) / divisor,

seq1,

seq2,

seq3,

)

type zip3With = <T1, T2, T3, T4>(

fn: (

[result1, result2, resul3]:

| [T1, T2, T3]

| [T1, undefined, undefined]

| [T1, T2, undefined]

| [T1, undefined, T3]

| [undefined, T2, undefined]

| [undefined, T2, T3]

| [undefined, undefined, T3],

index: number,

) => T4,

seq1: Seq<T1>,

seq2: Seq<T2>,

seq3: Seq<T3>,

) => Seq<T4>

Combines 2 or more sequences into a single sequence.

Usage

Type Definition

const sequence: Seq<number> = concat(

fromArray([0, 1]),

fromArray([2, 3]),

fromArray([4, 5]),

)

type concat = <T>(...items: Array<Seq<T>>) => Seq<T>

Takes 2 or more sequences and creates a new sequence built by pulling the next value from each of the sequences in order.

Usage

Type Definition

// Builds: a -> 1 -> b -> 2 -> c -> 3

const sequence: Seq<string | number> = interleave(

fromArray(["a", "b", "c"]),

range(1, 3),

)

type interleave = <T>(...items: Array<Seq<T>>) => Seq<T>

Last modified 9mo ago