Skip to main content
Module

x/lazy/lib/lazy.ts>Lazy

A linq-like lazy-evaluation enumerable/iteration library that aims to support deno, node & browser
Latest
class Lazy
implements Iterable<TElement>
Abstract
import { Lazy } from "https://deno.land/x/lazy@v1.7.3/lib/lazy.ts";

The base class that all lazy iterable objects derive from. This can be extended with custom iterators if needed.

Methods

Applies an accumulator function over an interable.

aggregate<TAcc>(agg: AggFn<TElement, TAcc>, seed: TAcc): TAcc

Applies an accumulator function over an interable.

all(predicate: BoolPredicate<TElement>): boolean

Returns whether all elements satisfy the given condition.

any(): boolean

Returns whether the iterable is not empty.

any(predicate: BoolPredicate<TElement>): boolean

Returns whether any of the elements satisfy the given condition.

Appends the element to the end of the iterable.

apply<TLazy extends Lazy<TResult>, TResult = TElement>(fn: (t: Lazy<TElement>) => TLazy): Lazy<TResult>

Applies the given lazy iterable implementation to the current object. This allows for using custom Lazy implementations using the standard chaining syntax.

average(): TElement extends number ? number : never

Computes the average of the iterable.

average(selector: MapFn<TElement, number>): number

Computes the average of result of the selector function over the iterable.

batchIn(batchSize: number, includeIncomplete?): Lazy<Iterable<TElement>>

Batches elements in groups of the given batch size.

Resolves the underlaying iterable completely and returns a lazy of the result.

concat(...iterables: Array<Iterable<TElement>>): Lazy<TElement>

Concatinates multiple iterables in order.

contains(element: TElement, comparer?: ComparerFn<TElement>): boolean

Determines whether the iterable has a given element.

count(): number

Returns the number of elements in the iterable.

count(predicate: BoolPredicate<TElement>): number

Returns the number of elements that satify the given condition.

Returns the elements in the iterable, or the given default value as the only element if it contained none.

distinct<TKey>(compareOn?: MapFn<TElement, TKey>): Lazy<TElement>

Returns the distinct elements in the iterable.

elementAt(index: number): TElement

Returns the element at the given index of the iterable.

elementAtOrDefault(index: number, defaultValue: TElement): TElement

Returns the element at the given index of the iterable, or the given default value if out of range.

elementAtOrDefault<TDefault>(index: number, defaultValue: TDefault): TElement | TDefault

Returns the element at the given index of the iterable, or the given default value if out of range.

except<TKey = TElement>(second: Iterable<TElement>, compareOn?: MapFn<TElement, TKey>): Lazy<TElement>

Returns the set difference between 2 iterables. This like doing an XOR over the 2 iterables.

Returns the first element in the iterable.

Returns the first element that satisfies the given condition.

Returns the first element in the iterable, or the given default value if the iterable was empty.

firstOrDefault<TDefault>(defaultValue: TDefault): TElement | TDefault

Returns the first element in the iterable, or the given default value if the iterable was empty.

Returns the first element in the iterable that satisfies the condition, or the given default value.

firstOrDefault<TDefault>(defaultValue: TDefault, predicate: BoolPredicate<TElement>): TElement | TDefault

Returns the first element in the iterable that satisfies the condition, or the given default value.

forEach(callbackFn: CallbackFn<TElement>): void

Mimics the behaviour of Array.prototype.forEach, with the exception of not providing the entire array as the 3rd param of the callback.

groupBy<TKey>(keyFn: MapFn<TElement, TKey>): Lazy<IGrouping<TKey, TElement>>

Groups the elements by key.

groupBy<TKey, TItem>(keyFn: MapFn<TElement, TKey>, elementSelector: MapFn<TElement, TItem>): Lazy<IGrouping<TKey, TItem>>

Groups the elements by key and projects each element using the given function.

groupBy<TKey, TItem, TResult>(
keyFn: MapFn<TElement, TKey>,
elementSelector: MapFn<TElement, TItem>,
resultSelector: CombineFn<TKey, Iterable<TItem>, TResult>,
): Lazy<TResult>

Groups the elements by key and projects each element using the given function. The elements of each group are projected using the given function.

groupJoin<TSecond, TKey, TResult>(
second: Iterable<TSecond>,
firstKeyFn: MapFn<TElement, TKey>,
secondKeyFn: MapFn<TSecond, TKey>,
joinFn: CombineFn<TElement, Iterable<TSecond>, TResult>,
): Lazy<TResult>

Joins 2 iterables on the given key and groups the results.

intersect<TKey = TElement>(second: Iterable<TElement>, compareOn?: MapFn<TElement, TKey>): Lazy<TElement>

Returns the set intersection between 2 iterables. This like doing an AND over the 2 iterables.

iterableEquals(second: Iterable<TElement>, comparer?: ComparerFn<TElement>): boolean

Determines whether 2 iterables are equal.

join<TSecond, TKey, TResult>(
second: Iterable<TSecond>,
firstKeyFn: MapFn<TElement, TKey>,
secondKeyFn: MapFn<TSecond, TKey>,
joinFn: CombineFn<TElement, TSecond, TResult>,
): Lazy<TResult>

Joins 2 iterables on the given matching keys. This is similar to a JOIN in SQL.

Returns the last element in the iterable.

Returns the last element in the iterable that satisfies the given condition.

Returns the last element in the iterable, or the given default value if the iterable was empty.

lastOrDefault<TDefault>(defaultValue: TDefault): TElement | TDefault

Returns the last element in the iterable, or the given default value if the iterable was empty.

Returns the last element in the iterable that satisfies the given condition, or the given default value.

lastOrDefault<TDefault>(defaultValue: TDefault, predicate: BoolPredicate<TElement>): TElement | TDefault

Returns the last element in the iterable that satisfies the given condition, or the given default value.

max(): TElement extends number ? number : never

Returns the maximum value in the iterable.

max(selector: MapFn<TElement, number>): number

Returns the maximum value of result of the selector function over the iterable.

min(): TElement extends number ? number : never

Returns the minimum value in the iterable.

min(selector: MapFn<TElement, number>): number

Returns the minimum value of result of the selector function over the iterable.

orderBy<TKey>(keyFn: MapFn<TElement, TKey>, compareFn?: SortFn<TKey>): Lazy<TElement>

Sorts the iterable in ascending order.

orderByDecending<TKey>(keyFn: MapFn<TElement, TKey>, compareFn?: SortFn<TKey>): Lazy<TElement>

Sorts the iterable in descending order.

Sorts the iterable in acending order according to the numeric result of the key function.

Sorts the iterable in descending order according to the numeric result of the key function.

Prepends the element to the beginning of the iterable.

resolveAll(): Promise<TElement extends PromiseLike<infer TResult> ? Lazy<TResult> : Lazy<TElement>>

Resolves all of the promises in the iterable, and returns a new Lazy iterable from the result.

Reverses the order of the iterable.

select<TResult>(selector: IndexMapFn<TElement, TResult>): Lazy<TResult>

Projects the elements of the iterable into a new form.

selectMany<TResult>(selector: IndexMapFn<TElement, Iterable<TResult>>): Lazy<TResult>

Projects the elements of the iterable into a new form, and flattens the iterable of iterables into a single iterable.

Returns a single element from the iterable that matches the given condition.

Returns a single element from the iterable that matches the given condition, or a default value if no element was found.

singleOrDefault<TDefault>(predicate: BoolPredicate<TElement>, defaultValue: TDefault): TElement | TDefault

Returns a single element from the iterable that matches the given condition, or a default value if no element was found.

skip(count: number): Lazy<TElement>

Skips the given number of elements from the start of the iterable and returns the rest.

skipLast(count: number): Lazy<TElement>

Skips the given number of elements from the end of the iterable, returning the rest.

Skips all elements in the iterable until the condition returns true, after which all elements are returned regardless.

stringJoin(separator?: string, strFn?: StrFn<TElement>): string

Joins all the elements in the iterable together into a single string, split by the given separator.

sum(): TElement extends number ? number : never

Computes the sum of all the elements in the iterable.

sum(selector: MapFn<TElement, number>): number

Returns the sum of all the results of the selector function over the iterable.

take(count: number): Lazy<TElement>

Returns the given number of elements from the start of the iterable, ignoring the rest.

takeLast(count: number): Lazy<TElement>

Returns the given number of elements from the end of the iterable, ignore the elements before.

Takes all elements in the iterable until the condition returns true, after which the iterable is considered to have ended.

Converts the iterable into a standard JavaScript Array.

Converts the iterable to a JSON-serialisable array.

toMap<TKey, TResult = TElement>(keyFn: MapFn<TElement, TKey>, valueFn?: MapFn<TElement, TResult>): Map<TKey, TResult>

Converts the iterable into a map using the key and value function.

union<TKey = TElement>(second: Iterable<TElement>, compareOn?: MapFn<TElement, TKey>): Lazy<TElement>

Returns the set union between 2 iterables. This like doing an OR over the 2 iterables.

where<TResult extends TElement>(predicate: IndexIsPredicate<TElement, TResult>): Lazy<TResult>

Filters elements based on the given predicate.

Filters elements based on the given predicate.

zip<TSecond>(second: Iterable<TSecond>): Lazy<[TElement, TSecond]>

Combines 2 iterables into an iterable of tuples. This will merge elements on the same index, finishing on the iterable that finishes first. If the first iterable has 3 elements, and the second 4, then the result will have 3 elements.

zip<TSecond, TResult>(second: Iterable<TSecond>, selector: CombineFn<TElement, TSecond, TResult>): Lazy<TResult>

Combines 2 iterables using the given selector. This will merge elements on the same index, finishing on the iterable that finishes first. If the first iterable has 3 elements, and the second 4, then the result will have 3 elements.

abstract
[Symbol.iterator](): Iterator<TElement>

Static Methods

empty<TElement>(): Lazy<TElement>

Creates an empty lazy iterable.

from<TElement>(iterable: Iterable<TElement>): Lazy<TElement>

Creates a lazy iterable object from the given iterable object.

range(start: number, end?: number): Lazy<number>

Creates a lazy iterable object that will produce a range of integers.

repeat<TElement>(element: TElement, count?: number): Lazy<TElement>

Creates a lazy iterable object that will repeate the element a given number of times.