partition() function
将 Iterable/AsyncIterable 分割为两个数组:一个包含所有满足 f 的元素,另一个包含所有不满足的元素。
Signature:
typescript
declare function partition<
A,
L extends A,
R extends A = A extends object ? ExcludeObject<A, L> : Exclude<A, L>,
>(f: (a: A) => a is L, iterable: Iterable<A>): [L[], R[]];Example
ts
partition((a) => a % 2 === 0, [1, 2, 3, 4, 5]); // [[2, 4], [1, 3, 5]]
// with pipe
pipe(
[1, 2, 3, 4, 5],
partition((a) => a % 2 === 0), // [[2, 4], [1, 3, 5]]
);
await pipe(
Promise.resolve([1, 2, 3, 4, 5]),
partition((a) => a % 2 === 0), // [[2, 4], [1, 3, 5]]
);
// if you want to use asynchronous callback
await pipe(
Promise.resolve([1, 2, 3, 4, 5]),
toAsync,
partition(async (a) => a % 2 === 0), // [[2, 4], [1, 3, 5]]
);
// with toAsync
await pipe(
[
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3),
Promise.resolve(4),
Promise.resolve(5),
],
toAsync,
partition((a) => a % 2 === 0), // [[2, 4], [1, 3, 5]]
);