Skip to main content
Module

x/better_iterators/mod.ts>LazyAsync

Chainable iterators (sync and async) for TypeScript, with support for opt-in, bounded parallelism
Go to Latest
class LazyAsync
implements AsyncIterable<T>, LazyShared<T>
import { LazyAsync } from "https://deno.land/x/better_iterators@v1.2.1/mod.ts";

Constructors

new
private
LazyAsync(iter: AsyncIterable<T>)

Methods

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

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

associateBy<Key>(uniqueKeyFn: Transform<T, Key>): Promise<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>): Promise<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)

avg(): Promise<T extends number ? number : never>
filter<S extends T>(f: (t: T) => t is S): LazyAsync<S>

Overload to support type narrowing.

Keeps only items for which f is true.

first(): Promise<T>

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

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

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

Flattens a Lazy<Iterable> to a Lazy

fold<I>(initial: I, foldFn: (i: I, t: T) => I): Promise<I>
groupBy<Key>(keyFn: Transform<T, Key>): Promise<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>): Promise<Map<Key, Value[]>>

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

limit(count: number): LazyAsync<T>

Limit the iterator to returning at most count items.

map<Out>(transform: Transform<T, Awaitable<Out>>): LazyAsync<Out>

Apply transform to each element.

Works like Array#map.

mapPar<Out>(max: number, transform: Transform<T, Promise<Out>>): LazyAsync<Out>

Like map, but performs up to max operations in parallel.

Note: This function will ensure that the order of outputs is the same as the order of the inputs they were mapped from. This can introduce head-of-line blocking, which can slow performance. If you don't need this, use mapParUnordered instead.

mapParUnordered<Out>(max: number, transform: Transform<T, Promise<Out>>): LazyAsync<Out>

A version of mapPar that does not enforce ordering, so doesn't suffer from head-of-line blocking.

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

Partition items into matches and others according to Filter f.

skip(count: number): LazyAsync<T>

Skip (up to) the first count elements

sum(): Promise<T extends number ? number : never>
toArray(): Promise<T[]>

Collect all items into an array.

[Symbol.asyncIterator](): AsyncIterator<T>

Static Methods

from<T>(iter: AsyncIterable<T>): LazyAsync<T>

This lets you directly create an AsyncIterable, but you might prefer the shorter lazy function.