Static Methods

fromArray

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

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

iterate

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

// Builds an infinite sequence that counts up from 0.
const sequence: Seq<number> = iterate(a => a + 1, 0)

fib

The fib method generates a sequence of fibonacci numbers.

const sequence: Seq<number> = fib()

random

Generates a random sequence using Math.random.

// Builds sequence of random numbers between 0 and 1.
const sequence: Seq<number> = random()

range

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

// 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)

infinite

A sequence that counts from 0 to Infinity.

const sequence: Seq<number> = infinite()

of

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

Similar to Array.of.

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

cycle

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

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

repeat

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

// 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)

repeatedly

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.

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

empty

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

const sequence: Seq<never> = empty()

zip

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.

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)

zipWith

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.

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,
)

zip3

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

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)

zip3With

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.

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,
)

concat

Combines 2 or more sequences into a single sequence.

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

interleave

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

// Builds: a -> 1 -> b -> 2 -> c -> 3
const sequence: Seq<string | number> = interleave(
  fromArray(["a", "b", "c"]),
  range(1, 3),
)

Last updated