partition
import { partition } from "https://github.gh-proxy.cn/raw.githubusercontent.com/baetheus/fun/main/async_iterable.ts";Partition an AsyncIterable into two based on a predicate.
@example
import { partition } from "./async_iterable.ts";
import { fromIterable } from "./async_iterable.ts";
import { pipe } from "./fn.ts";
const numbers = fromIterable([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
const [evens, odds] = pipe(
numbers,
partition(n => n % 2 === 0)
);
for await (const value of evens) {
console.log(`Even: ${value}`); // Even: 2, Even: 4, Even: 6, Even: 8, Even: 10
}
for await (const value of odds) {
console.log(`Odd: ${value}`); // Odd: 1, Odd: 3, Odd: 5, Odd: 7, Odd: 9
}
function partition<A, B extends A>(refinement: (a: A) => a is B): (ua: AsyncIterable<A>) => Pair<AsyncIterable<A>, AsyncIterable<B>>;
function partition<A>(predicate: (a: A) => boolean): (ua: AsyncIterable<A>) => Pair<AsyncIterable<A>, AsyncIterable<A>>;
function partition<A>(predicate: (a: A) => boolean): (ua: AsyncIterable<A>) => Pair<AsyncIterable<A>, AsyncIterable<A>>;
§
partition<A, B extends A>(refinement: (a: A) => a is B): (ua: AsyncIterable<A>) => Pair<AsyncIterable<A>, AsyncIterable<B>>
[src]Partition an AsyncIterable into two based on a predicate.
@example
import { partition } from "./async_iterable.ts";
import { fromIterable } from "./async_iterable.ts";
import { pipe } from "./fn.ts";
const numbers = fromIterable([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
const [evens, odds] = pipe(
numbers,
partition(n => n % 2 === 0)
);
for await (const value of evens) {
console.log(`Even: ${value}`); // Even: 2, Even: 4, Even: 6, Even: 8, Even: 10
}
for await (const value of odds) {
console.log(`Odd: ${value}`); // Odd: 1, Odd: 3, Odd: 5, Odd: 7, Odd: 9
}