Skip to main content
Module

x/better_iterators/mod.ts>Lazy

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

Constructors

new
private
Lazy(iter: Iterable<T>)

Methods

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

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

associateBy<Key>(uniqueKeyFn: Transform<T, Key>): 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>): 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(): T extends number ? number : never
chunked(size: number): Lazy<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.

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

Overload to support type narrowing.

Keeps only items for which f is true.

first(): T

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

firstOr<D>(defaultValue: D): 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): I
groupBy<Key>(keyFn: Transform<T, Key>): 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>): Map<Key, Value[]>

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

Joins multiple string values into a string.

limit(count: number): Lazy<T>

Limit the iterator to returning at most count items.

loop(): Lazy<T>

Like #repeat, but repeates forever.

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

Apply transform to each element.

Works like Array#map.

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

Partition items into matches and others according to Filter f.

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

repeat(count: number): Lazy<T>

Repeat items count times.

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

let nine = range({to: 3}).repeat(3).sum()
skip(count: number): Lazy<T>

Skip (up to) the first count elements

sum(): T extends number ? number : never
toArray(): Array<T>

Collect all items into an array.

Convert to a LazyAsync, which better handles chains of Promises.

[Symbol.iterator](): Iterator<T>

Static Methods

from<T>(iter: Iterable<T>): Lazy<T>

Prefer to use the lazy function.