Skip to main content
Module

x/better_iterators/mod.ts>LazyShared

Chainable iterators (sync and async) for TypeScript, with support for opt-in, bounded parallelism
Latest
interface LazyShared
import { type LazyShared } from "https://deno.land/x/better_iterators@v1.5.0/mod.ts";

Shared interface for Lazy and LazyAsync. (Note: LazyAsync implements some methods that are not shared.)

You shouldn't need to interact with these classes or this interface directly, though. You can use lazy to automatically instantiate the correct one.

Operations on lazy iterators consume the underlying iterator. You should not use them again.

Methods

map<Out>(transform: Transform<T, Out>): LazyShared<Out>

Apply transform to each element.

Works like Array#map.

map<Out>(options: ParallelMapOptions<T, Out>): LazyAsync<Out>

Asynchronously transform elements in parallel.

filter(f: Filter<T>): LazyShared<T>

Keeps only items for which f is true.

filter<S extends T>(f: (t: T) => t is S): LazyShared<S>

Overload to support type narrowing.

limit(count: number): LazyShared<T>

Limit the iterator to returning at most count items.

toArray(): Awaitable<Array<T>>

Collect all items into an array.

also(fn: (t: T) => void): LazyShared<T>

Injects a function to run on each T as it is being iterated.

partition(f: Filter<T>): Awaitable<Partitioned<T>>

Partition items into matches and others according to Filter f.

first(): Awaitable<T>

Get the first item. @throws if there are no items.

firstOr<D>(defaultValue: D): Awaitable<T | D>

Get the first item. Returns defaultValue if there is no first item.

skip(count: number): LazyShared<T>

Skip (up to) the first count elements

groupBy<Key>(keyFn: Transform<T, Key>): Awaitable<Map<Key, T[]>>

Given keyFn, use it to extract a key from each item, and create a Map grouping items by that key.

groupBy<Key, Value>(keyFn: Transform<T, Key>, valueFn: Transform<T, Value>): Awaitable<Map<Key, Value[]>>

Adds a valueFn to extract that value (instead of T) into a group.

associateBy<Key>(uniqueKeyFn: Transform<T, Key>): Awaitable<Map<Key, T>>

Given uniqueKeyFn, use it to extract a key from each item, and create a 1:1 map from that to each item.

associateBy<Key, Value>(uniqueKeyFn: Transform<T, Key>, valueFn: Transform<T, Value>): Awaitable<Map<Key, Value>>

Takes an additional valueFn to extract a value from T. The returned map maps from Key to Value. (instead of Key to T)

flatten(): LazyShared<Flattened<T>>

Flattens a Lazy<Iterable> to a Lazy

joinToString(args?: JoinToStringArgs): Awaitable<JoinedToString<T>>

Joins multiple string values into a string.

fold<I>(initialValue: I, foldFn: (i: I, t: T) => I): Awaitable<I>

Fold values. See example in LazyShared#sum

sum(): Awaitable<T extends number ? number : never>

Sums a Lazy iterator of numbers.

This is equivalent to:

import { lazy, range } from "./mod.ts"

range({to: 10}).fold(0, (a, b) => a + b)
avg(): Awaitable<T extends number ? number : never>

Averages numbers from the Lazy iterable.

Will return NaN if no numbers exist.

Note: This algorithm does a simple left-to-right accumulation of the sum, so can suffer from loss of precision when summing things with vastly different scales, or working with sums over Number.MAX_SAFE_INTEGER.

peekable(): Peekable<T> | PeekableAsync<T>

Get a "peekable" iterator, which lets you peek() at the next item without removing it from the iterator.

chunked(size: number): LazyShared<T[]>

Iterate by chunks of a given size formed from the underlying Iterator.

Each chunk will be exactly size until the last chunk, which may be from 1-size items long.

repeat(count: number): LazyShared<T>

Repeat items count times.

import { range } from "./mod.ts"

let nine = range({to: 3}).repeat(3).sum()
loop(): LazyShared<T>

Like #repeat, but repeates forever.