Skip to main content

compress

compress() function

Returns Iterable/AsyncIterable that filters elements from 'iterable' returning only those that have a corresponding element in 'selectors' that evaluates to 'true'. Stops when either 'iterable' or 'selectors' has been exhausted.

Signature:
declare function compress<A, B>(selector: Array<A>, iterable: Iterable<B>): IterableIterator<B>;

declare function compress<A, B>(selector: Array<A>, iterable: AsyncIterable<B>): AsyncIterableIterator<B>;

declare function compress<A, B extends Iterable<unknown> | AsyncIterable<unknown>>(selector: Array<A>): (iterable: B) => ReturnIterableIteratorType<B>;

Example

const iter1 = compress([false,true,false,false,true],  [1, 2, 3, 4, 5]);
iter1.next(); // {value: 2, done:false}
iter1.next(); // {value: 5, done:false}
iter1.next(); // {value: undefined, done:true }

const iter2 = compress([1,0,0,1,0], "abcde");
iter2.next(); // {value: "a", done:false}
iter2.next(); // {value: "d", done:false}
iter2.next(); // {value: undefined, done:true }

// with pipe
pipe(
[1, 2, 3],
compress([false, true, true]),
toArray,
); // [2, 3]

see pipe, toArray

Open Source Code