import { filter } from "https://deno.land/x/aitertools@0.6.0/src/filter.ts";
Eliminates all elements from the iterable source
that do not satisfy
the predicate
function.
import { filter } from "./filter.ts";
async function* gen() { yield "foo"; yield "bar"; yield "baz"; yield "qux" }
const iterable = filter((v: string) => !!v.match(/^b/), gen());
for await (const value of iterable) console.log(value);
The above example will print the following 2 lines:
bar
baz
The predicate
function can take an index as well as the value.
import { filter } from "./filter.ts";
const iterable = filter(
(v: string, i: number) => !v.match(/^b/) && i % 2 === 0,
["foo", "bar", "baz", "qux", "quux"]
);
for await (const value of iterable) console.log(value);
The above example will print the following 2 lines:
foo
quux
Parameters
predicate: ((value: T) => Promise<boolean> | boolean) | ((value: T, index: number) => Promise<boolean> | boolean)
A predicate function that takes the value and the index of the element and returns a boolean, which indicates whether the element should be included in the resulting iterable. It can be either synchronous or asynchronous.