@jasmith79/sequable
Version:
Library functions for working with generators
33 lines (32 loc) • 1.76 kB
TypeScript
import { Sequable } from "./sequable.js";
/**
* Partitions the given Iterable, Iterator, or generator function into an
* Iterable of Iterables of size n.
*
* @param n The size of the sub-sequences. **NOTE**: must be finite! This
* function maintains an internal buffer of the intermediate results to
* force consumption of the supplied iterator. The supplied iterator can
* be infinite, but n must be a finite integer number.
* @param sequable The Iterable, Iterator, or generator function to chunk.
*/
export declare function partition<T>(n: number, sequable: Sequable<T>): Iterable<Iterable<T>>;
/**
* Partitions the given Iterable, Iterator, or generator function into two
* Iterables that pass and fail the given predicate, respectively.
*
* **NOTE:** this function maintains an internal cache of results for the
* other Iterable when the first iterable is being consumed. When working with
* infinite sequences if there is a long enough run of either passing or failing
* while the other Iterable is being consumed it is possible to run out of
* heap memory.
*
* For a concrete example, consider a sequence of integers partitioned by
* isEven. The sequence has one odd integer followed by 1000 even
* integers repeated endlessly. A consumer that tries to take a large number of
* odd numbers will grow the cache of even numbers as the odd iterator is
* consumed to the point where the allocation eventually fails.
*
* @param pred The predicate to split the sequence by.
* @param sequable The Iterable, Iterator, or generator function to partition.
*/
export declare function partitionBy<P extends (x: any) => boolean>(pred: P, sequable: Sequable<Parameters<P>[0]>): [Iterable<Parameters<P>[0]>, Iterable<Parameters<P>[0]>];