Skip to main content
Module

x/aitertools/filter.ts>filter

Well-tested utility functions dealing with async iterables
Go to Latest
function filter
import { filter } from "https://deno.land/x/aitertools@0.3.1/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

Type Parameters

T

The type of the elements in the iterable source and the returned async iterable.

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.

source: Iterable<T> | AsyncIterable<T>

An iterable to filter elements from. It can be either finite or infinite.

Returns

AsyncIterableIterator<T>

An async iterable that contains elements from the source iterable that satisfy the predicate function.