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|What}

Computes the Cartesian product with another iterable.

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

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.

Parameters:
Name Type Attributes Description
that Iterable <optional>
Returns:
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);
console.log([...naturals.when(x => x > 3)]); // [4, 5, 6, ...]

(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) if(aa, popt) → {Each}

Filters an iterable using a predicate.

Parameters:
Name Type Attributes Default Description
aa Iterable

Input iterable.

p function <optional>
item => item !== undefined
Returns:
Type
Each

(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]

(static) self(aa) → {Each}

Infinite repetition of an iterable.

Parameters:
Name Type Description
aa Iterable
Returns:
Type
Each

(static) sthen(aa, f) → {Each}

Safe mapping operation (like then but without clashing with Promises).

Parameters:
Name Type Description
aa Iterable

Input iterable.

f function

Mapping function (item, index) => result.

Returns:
Type
Each
Example
const doubled = Each.sthen([1, 2, 3], x => x * 2);
[...doubled]; // [2, 4, 6]

(static) what(aa, opopt, gotopt) → {*}

Reduces an iterable to a single value.

Parameters:
Name Type Attributes Description
aa Iterable
op function <optional>
got * <optional>
Returns:
Type
*

(static) when(aa, popt, startopt, inclusiveopt) → {Each|AsyncEach}

Slices an iterable based on a predicate or index, or bridges to asynchronous iteration.

  • If p is a predicate function, yields items starting or ending where the predicate matches.
  • If p is a number, treats it as an index boundary for slicing.
  • If called without arguments, this method interprets the iterable as containing Promises and returns an AsyncEach that yields their resolved values.
    This provides a natural bridge from a synchronous Each of Promises to an asynchronous AsyncEach of values.
Parameters:
Name Type Attributes Default Description
aa Iterable

Input iterable.

p function | number <optional>

Predicate function or index boundary.

start boolean <optional>
true

Whether to start or end the slice at the matching element.

inclusive boolean <optional>
start

Whether to include the boundary element in the result.

Returns:

A sliced Each, or an AsyncEach if no arguments are provided.

Type
Each | AsyncEach
Examples

Slice by predicate

const e = Each.of(1, 2, 3, 4, 5).when(x => x > 2);
console.log([...e]); // [3, 4, 5]

Slice by index

const f = Each.of('a','b','c','d').when(2);
console.log([...f]); // ['c','d']

Bridge from promises to async iteration

const g = Each.of(Promise.resolve(1), Promise.resolve(2)).when();
for await (const v of g) {
  console.log(v); // 1, then 2
}

(static) which(aa, popt) → {Each}

Filters an iterable with a predicate.

Parameters:
Name Type Attributes Default Description
aa Iterable

Input iterable.

p function <optional>
item => item !== undefined
Returns:
Type
Each