Class IterableElementBase<E, R>Abstract

Base class that makes a data structure iterable and provides common element-wise utilities (e.g., map/filter/reduce/find).

This class implements the JavaScript iteration protocol (via Symbol.iterator) and offers array-like helpers with predictable time/space complexity.

Type Parameters

  • E

    The public element type yielded by the structure.

  • R

    The underlying "raw" element type used internally or by converters.

Hierarchy (view full)

Implements

  • Iterable<E>

Constructors

Properties

_toElementFn?: ((rawElement: R) => E)

The converter used to transform a raw element (R) into a public element (E).

Time O(1), Space O(1).

Accessors

  • get toElementFn(): undefined | ((rawElement: R) => E)
  • Exposes the current toElementFn, if configured.

    Returns undefined | ((rawElement: R) => E)

    The converter function or undefined when not set.

    Time O(1), Space O(1).

Methods

  • Internal iterator factory used by the default iterator.

    Parameters

    • Rest...args: unknown[]

      Optional iterator arguments.

    Returns IterableIterator<E, any, any>

    An iterator over elements.

    Implementations should yield in O(1) per element with O(1) extra space when possible.

  • Returns an iterator over the structure's elements.

    Parameters

    • Rest...args: unknown[]

      Optional iterator arguments forwarded to the internal iterator.

    Returns IterableIterator<E, any, any>

    An IterableIterator<E> that yields the elements in traversal order.

    Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.

  • Creates a structural copy with the same element values and configuration.

    Returns this

    A clone of the current instance (same concrete type).

    Expected Time O(n) to copy elements; Space O(n).

  • Tests whether all elements satisfy the predicate.

    Parameters

    • predicate: ElementCallback<E, R, boolean>

      Function invoked for each element with signature (value, index, self).

    • OptionalthisArg: unknown

      Optional this binding for the predicate.

    Returns boolean

    true if every element passes; otherwise false.

    Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).

  • Filters elements using the provided predicate and returns the same concrete structure type.

    Parameters

    • predicate: ElementCallback<E, R, boolean>

      Function with signature (value, index, self) => boolean.

    • OptionalthisArg: unknown

      Optional this binding for the predicate.

    Returns this

    A new instance of the same concrete type containing only elements that pass the predicate.

    Time O(n), Space O(k) where k is the number of kept elements.

  • Finds the first element that satisfies the predicate and returns it.

    Finds the first element of type S (a subtype of E) that satisfies the predicate and returns it.

    Type Parameters

    • S

    Parameters

    • predicate: ElementCallback<E, R, S>

      Type-guard predicate: (value, index, self) => value is S.

    • OptionalthisArg: unknown

      Optional this binding for the predicate.

    Returns undefined | S

    The matched element typed as S, or undefined if not found.

    Time O(n) in the worst case; may exit early on the first match. Space O(1).

  • Invokes a callback for each element in iteration order.

    Parameters

    • callbackfn: ElementCallback<E, R, void>

      Function invoked per element with signature (value, index, self).

    • OptionalthisArg: unknown

      Optional this binding for the callback.

    Returns void

    void.

    Time O(n), Space O(1).

  • Checks whether a strictly-equal element exists in the structure.

    Parameters

    • element: E

      The element to test with === equality.

    Returns boolean

    true if an equal element is found; otherwise false.

    Time O(n) in the worst case. Space O(1).

  • Indicates whether the structure currently contains no elements.

    Returns boolean

    true if empty; otherwise false.

    Expected Time O(1), Space O(1) for most implementations.

  • Maps each element to a new element and returns a new iterable structure.

    Type Parameters

    • EM

      The mapped element type.

    • RM

      The mapped raw element type used internally by the target structure.

    Parameters

    • callback: ElementCallback<E, R, EM>

      Function with signature (value, index, self) => mapped.

    • Optionaloptions: IterableElementBaseOptions<EM, RM>

      Optional options for the returned structure, including its toElementFn.

    • OptionalthisArg: unknown

      Optional this binding for the callback.

    Returns IterableElementBase<EM, RM>

    A new IterableElementBase<EM, RM> containing mapped elements.

    Time O(n), Space O(n).

  • Maps each element to the same element type and returns the same concrete structure type.

    Parameters

    • callback: ElementCallback<E, R, E>

      Function with signature (value, index, self) => mappedValue.

    • OptionalthisArg: unknown

      Optional this binding for the callback.

    Returns this

    A new instance of the same concrete type with mapped elements.

    Time O(n), Space O(n).

  • Tests whether at least one element satisfies the predicate.

    Parameters

    • predicate: ElementCallback<E, R, boolean>

      Function invoked for each element with signature (value, index, self).

    • OptionalthisArg: unknown

      Optional this binding for the predicate.

    Returns boolean

    true if any element passes; otherwise false.

    Time O(n) in the worst case; may exit early on first success. Space O(1).

  • Returns a representation of the structure suitable for quick visualization. Defaults to an array of elements; subclasses may override to provide richer visuals.

    Returns E[]

    A visual representation (array by default).

    Time O(n), Space O(n).

  • Returns an iterator over the values (alias of the default iterator).

    Returns IterableIterator<E, any, any>

    An IterableIterator<E> over all elements.

    Creating the iterator is O(1); full iteration is Time O(n), Space O(1).