Skip to content

Class: LazyList<T> ​

Defined in: src/utils/LazyList.ts:11

LazyList implementation for deferred computation.

This class provides a list-like interface where elements are computed on-demand using a generator function. This is useful for large datasets where computing all elements upfront would be expensive.

Direct translation of Python's LazyList functionality.

Type Parameters ​

T ​

T

Implements ​

  • Iterable<T>

Constructors ​

Constructor ​

ts
new LazyList<T>(generator, length): LazyList<T>;

Defined in: src/utils/LazyList.ts:20

Create a new LazyList.

Parameters ​

generator ​

(index) => T

Function that generates element at given index

length ​

number

Total number of elements

Returns ​

LazyList<T>

Properties ​

length ​

ts
readonly length: number;

Defined in: src/utils/LazyList.ts:22

Total number of elements

Methods ​

get() ​

ts
get(index): T;

Defined in: src/utils/LazyList.ts:31

Get element at index, computing if necessary.

Parameters ​

index ​

number

Zero-based index

Returns ​

T

Element at index


toArray() ​

ts
toArray(): T[];

Defined in: src/utils/LazyList.ts:48

Convert to array, computing all elements.

Returns ​

T[]

Array of all elements


map() ​

ts
map<U>(fn): LazyList<U>;

Defined in: src/utils/LazyList.ts:62

Map over elements lazily.

Type Parameters ​

U ​

U

Parameters ​

fn ​

(value, index) => U

Mapping function

Returns ​

LazyList<U>

New LazyList with mapped elements


filter() ​

ts
filter(fn): T[];

Defined in: src/utils/LazyList.ts:76

Filter elements lazily. Note: This requires computing all elements to determine the final length.

Parameters ​

fn ​

(value, index) => boolean

Filter predicate

Returns ​

T[]

Array of filtered elements


take() ​

ts
take(n): LazyList<T>;

Defined in: src/utils/LazyList.ts:93

Take first n elements.

Parameters ​

n ​

number

Number of elements to take

Returns ​

LazyList<T>

New LazyList with first n elements


skip() ​

ts
skip(n): LazyList<T>;

Defined in: src/utils/LazyList.ts:107

Skip first n elements.

Parameters ​

n ​

number

Number of elements to skip

Returns ​

LazyList<T>

New LazyList with remaining elements


slice() ​

ts
slice(start?, end?): LazyList<T>;

Defined in: src/utils/LazyList.ts:125

Slice the list.

Parameters ​

start? ​

number = 0

Start index (inclusive)

end? ​

number = ...

End index (exclusive)

Returns ​

LazyList<T>

New LazyList with sliced elements


[iterator]() ​

ts
iterator: Iterator<T>;

Defined in: src/utils/LazyList.ts:139

Iterate over elements.

Returns ​

Iterator<T>

Implementation of ​

ts
Iterable.[iterator]

forEach() ​

ts
forEach(fn): void;

Defined in: src/utils/LazyList.ts:150

For each element, execute a function.

Parameters ​

fn ​

(value, index) => void

Function to execute for each element

Returns ​

void


some() ​

ts
some(fn): boolean;

Defined in: src/utils/LazyList.ts:162

Check if any element satisfies the predicate.

Parameters ​

fn ​

(value, index) => boolean

Predicate function

Returns ​

boolean

True if any element satisfies the predicate


every() ​

ts
every(fn): boolean;

Defined in: src/utils/LazyList.ts:177

Check if all elements satisfy the predicate.

Parameters ​

fn ​

(value, index) => boolean

Predicate function

Returns ​

boolean

True if all elements satisfy the predicate


find() ​

ts
find(fn): T | undefined;

Defined in: src/utils/LazyList.ts:192

Find first element that satisfies the predicate.

Parameters ​

fn ​

(value, index) => boolean

Predicate function

Returns ​

T | undefined

First matching element or undefined


findIndex() ​

ts
findIndex(fn): number;

Defined in: src/utils/LazyList.ts:208

Find index of first element that satisfies the predicate.

Parameters ​

fn ​

(value, index) => boolean

Predicate function

Returns ​

number

Index of first matching element or -1


reduce() ​

ts
reduce<U>(fn, initialValue): U;

Defined in: src/utils/LazyList.ts:224

Reduce the list to a single value.

Type Parameters ​

U ​

U

Parameters ​

fn ​

(accumulator, currentValue, currentIndex) => U

Reducer function

initialValue ​

U

Initial value

Returns ​

U

Reduced value


clearCache() ​

ts
clearCache(): void;

Defined in: src/utils/LazyList.ts:238

Clear the cache.

Returns ​

void

Released under the MIT License.