Class: Each

Each()

The Each class implements an abstract, immutable, lazy iterable interface.

Each instance is defined by its [Symbol.iterator]() implementation, and the class provides static and instance methods to construct, transform, and evaluate iterable sequences.

Each shares the same fluent semantic API as What and AsyncEach, exposing the 9 core methods: if(), sthen(), else(), which(), when(), match(), each(), self(), and what().

This enables declarative and composable pipelines over synchronous sequences with lazy evaluation.

Constructor

new Each()

See:
Example
const seq = Each.of(1, 2, 3)
  .sthen(x => x * 2)
  .if(x => x > 2);

console.log([...seq]); // => [4, 6]

Members

(static) NATURAL :Each.<number>

Infinite natural numbers starting from 0.

Type:

Methods

each(thatopt) → {Each|undefined}

Computes the Cartesian product of this iterable with another iterable.

Two behaviors:

  1. each(that) — Cartesian product
    When that is provided, returns a new Each producing all ordered pairs:

    [a, b]   for each a in this, for each b in that
    
  2. each() — consumption
    When called without arguments, the method simply consumes this iterable to completion, similar to invoking Array.prototype.forEach. It returns undefined.

Parameters:
Name Type Attributes Description
that Iterable | undefined <optional>

Optional iterable to pair with. If omitted, this iterable is consumed.

Returns:
  • With that: a new Each producing pairs [a, b].
    • Without that: undefined, after consumption.
Type
Each | undefined
Examples
// Cartesian product:
for (const [a, b] of seq.each(other)) {
  console.log(a, b);
}
// Consumption:
seq.each();  // exhausts `seq`, returns undefined

else(thatopt) → {Each}

Concatenates with another iterable or flattens one level.

Parameters:
Name Type Attributes Description
that Iterable <optional>
Returns:
Type
Each

equals(that) → {boolean}

Compares this Each with another iterable for deep equality.

Parameters:
Name Type Description
that Iterable

Another iterable.

Returns:
Type
boolean

if(popt) → {Each}

Filters this Each with a predicate.

Parameters:
Name Type Attributes Default Description
p function | What <optional>
item => item !== undefined
Returns:
Type
Each

match(thatopt) → {Each}

Zips this Each with another iterable.

If no iterable is provided, it zips with itself.

Parameters:
Name Type Attributes Default Description
that Iterable <optional>
this

The iterable to zip with. Defaults to the current Each instance.

Returns:

A new Each instance containing pairs [itemFromThis, itemFromThat].

Type
Each

self() → {Each}

Produces an infinite repetition of this Each.

Returns:
Type
Each

sthen(f) → {Each}

Maps a function over each item.

Parameters:
Name Type Description
f function | What

Transformation function.

Returns:
Type
Each

toArray() → {Array}

Converts this Each to a plain array.

Returns:
Type
Array

what(opopt, startopt) → {*}

Reduces this Each using an operation.

Parameters:
Name Type Attributes Description
op function <optional>
start * <optional>
Returns:
Type
*

when(p, startopt, inclusiveopt) → {Each|AsyncEach}

Slices this Each based on predicate or index.

Parameters:
Name Type Attributes Default Description
p function | number

Predicate or index.

start boolean <optional>
true

Start or end slice.

inclusive boolean <optional>
start

Include boundary element.

Returns:
Type
Each | AsyncEach

which(popt) → {Each}

Alias for Each.if, kept distinct for symmetry with What.

Parameters:
Name Type Attributes Default Description
p function | What <optional>
item => item !== undefined
Returns:
Type
Each

(static) along(start, next) → {Each}

Creates a potentially infinite Each sequence.

Starts with an initial value and repeatedly applies a function to produce the next value.

Parameters:
Name Type Description
start *

Initial value.

next function

Function producing the next value.

Returns:
Type
Each
Example
const naturals = Each.along(0, n => n + 1);

(static) as(items) → {Each}

Converts a value or iterable into an Each instance.

  • undefined → an empty Each.
  • Each instance → returned as-is.
  • iterable → wrapped lazily.
  • other values → wrapped in a single-element Each.
Parameters:
Name Type Description
items any | Iterable | undefined

Items to convert.

Returns:
Type
Each
Example
Each.as([1, 2, 3]); // iterable
Each.as(5);         // single element
Each.as();          // empty

(static) each(…aaa) → {What}

Computes the Cartesian product of multiple iterables (lazy).

Returns a What function that incrementally extends a path.

Parameters:
Name Type Attributes Description
aaa Iterable <repeatable>
Returns:
Type
What

(static) else(aaa) → {Each}

Flattens one level of nesting.

Parameters:
Name Type Description
aaa Iterable

Iterable of iterables.

Returns:
Type
Each

(static) equal(aa, bb) → {boolean}

Checks deep equality between two iterables (or values).

Parameters:
Name Type Description
aa Iterable | any
bb Iterable | any
Returns:
Type
boolean

(static) match(…aaa) → {Each}

Zips multiple iterables together.

Parameters:
Name Type Attributes Description
aaa Iterable <repeatable>
Returns:
Type
Each

(static) of(…items) → {Each}

Creates an Each from a fixed list of items.

Parameters:
Name Type Attributes Description
items any <repeatable>

Items to include.

Returns:
Type
Each
Example
const seq = Each.of(1, 2, 3);
[...seq]; // [1, 2, 3]