Options
All
  • Public
  • Public/Protected
  • All
Menu

Seq describes a lazy operation, allowing them to efficiently chain use of all the higher-order collection methods (such as map and filter) by not creating intermediate collections.

Seq is immutable — Once a Seq is created, it cannot be changed, appended to, rearranged or otherwise modified. Instead, any mutative method called on a Seq will return a new Seq.

Seq is lazySeq does as little work as necessary to respond to any method call. Values are often created during iteration, including implicit iteration when reducing or converting to a concrete data structure such as a List or JavaScript Array.

For example, the following performs no work, because the resulting Seq's values are never iterated:

const { Seq } = require('immutable')
const oddSquares = Seq([ 1, 2, 3, 4, 5, 6, 7, 8 ])
.filter(x => x % 2 !== 0)
.map(x => x * x)

Once the Seq is used, it performs only the work necessary. In this example, no intermediate arrays are ever created, filter is called three times, and map is only called once:

oddSquares.get(1); // 9

Any collection can be converted to a lazy Seq with Seq().

const { Map } = require('immutable')
const map = Map({ a: 1, b: 2, c: 3 })
const lazySeq = Seq(map)

Seq allows for the efficient chaining of operations, allowing for the expression of logic that can otherwise be very tedious:

lazySeq
.flip()
.map(key => key.toUpperCase())
.flip()
// Seq { A: 1, B: 1, C: 1 }

As well as expressing logic that would otherwise seem memory or time limited, for example Range is a special kind of Lazy sequence.

const { Range } = require('immutable')
Range(1, Infinity)
.skip(1000)
.map(n => -n)
.filter(n => n % 2 === 0)
.take(2)
.reduce((r, n) => r * n, 1)
// 1006008

Seq is often used to provide a rich collection API to JavaScript Object.

Seq({ x: 0, y: 1, z: 2 }).map(v => v * 2).toObject();
// { x: 0, y: 2, z: 4 }

Index

Functions

  • Always returns Seq.Indexed, discarding associated keys and supplying incrementing indices.

    Note: Seq.Indexed is a conversion function and not a class, and does not use the new keyword during construction.

    Type parameters

    • T

    Parameters

    Returns <internal>.Indexed<T>

  • Always returns a Seq.Keyed, if input is not keyed, expects an collection of [K, V] tuples.

    Note: Seq.Keyed is a conversion function and not a class, and does not use the new keyword during construction.

    Type parameters

    • K

    • V

    Parameters

    Returns <internal>.Keyed<K, V>

  • Type parameters

    • V

    Parameters

    • obj: {}
      • [key: string]: V

    Returns <internal>.Keyed<string, V>

  • Always returns a Seq.Set, discarding associated indices or keys.

    Note: Seq.Set is a conversion function and not a class, and does not use the new keyword during construction.

    Type parameters

    • T

    Parameters

    Returns <internal>.Set<T>

  • True if maybeSeq is a Seq, it is not backed by a concrete structure such as Map, List, or Set.

    Parameters

    • maybeSeq: unknown

    Returns maybeSeq is <internal>.Keyed<unknown, unknown> | <internal>.Indexed<unknown> | <internal>.Set<unknown>

Generated using TypeDoc