Skip to main content

reject

reject() function

The opposite of filter Iterable/AsyncIterable of all elements f returns falsy for

Signature:
declare function reject<A, B = unknown>(f: (a: A) => B, iterable: Iterable<A>): IterableIterator<A>;

declare function reject<A, B = unknown>(f: (a: A) => B, iterable: AsyncIterable<A>): AsyncIterableIterator<A>;

declare function reject<A extends Iterable<unknown> | AsyncIterable<unknown>, B = unknown>(f: (a: IterableInfer<A>) => B, iterable?: A): (iterable: A) => ReturnIterableIteratorType<A, IterableInfer<A>>;

Example

const iter = reject((a)=> a % 2 === 0, [0, 1, 2, 3, 4, 5, 6]);
iter.next() // {done:false, value: 1}
iter.next() // {done:false, value: 3}
iter.next() // {done:false, value: 5}
iter.next() // {done:true, value: undefined}

// with pipe
pipe(
[0, 1, 2, 3, 4, 5, 6],
reject(a => a % 2 === 0),
toArray,
); // [1, 3, 5]

await pipe(
Promise.resolve([0, 1, 2, 3, 4, 5, 6]),
reject(a => a % 2 === 0),
toArray,
); // [1, 3, 5]

// if you want to use asynchronous callback
await pipe(
Promise.resolve([0, 1, 2, 3, 4, 5, 6]),
toAsync,
reject(async a => a % 2 === 0),
toArray,
); // [1, 3, 5]

// with toAsync
await pipe(
[Promise.resolve(0), Promise.resolve(1), Promise.resolve(2),
Promise.resolve(3), Promise.resolve(4), Promise.resolve(5), Promise.resolve(6)],
toAsync,
reject(a => a % 2 === 0),
toArray,
); // [1, 3, 5]

Try It

see pipe, toAsync, toArray