Skip to main content
Module

x/aitertools/mod.ts>dropWhile

Well-tested utility functions dealing with async iterables
Go to Latest
function dropWhile
import { dropWhile } from "https://deno.land/x/aitertools@0.4.0/mod.ts";

Drops elements from the beginning of an async iterable as long as a specified condition is met, and yields the remaining elements.

import { dropWhile } from "./drop.ts";

async function* gen() { yield "foo"; yield "bar"; yield "baz"; yield "qux" }
const iterable = dropWhile(gen(), v => v !== "baz");
for await (const value of iterable) {
  console.log(value);
}

The above example will print the following 2 lines:

baz
qux

An async predicate function also works. The following example will print the same 2 lines as the previous example:

import { dropWhile } from "./drop.ts";

async function* gen() { yield "foo"; yield "bar"; yield "baz"; yield "qux" }
const iterable = dropWhile(gen(), v => Promise.resolve(v !== "baz"));
for await (const value of iterable) {
  console.log(value);
}

A predicate function can take an index as well as the value.

import { dropWhile } from "./drop.ts";

async function* gen() { yield "foo"; yield "bar"; yield "baz"; yield "qux" }
const iterable = dropWhile(gen(), (_, i) => i % 2 === 0);
for await (const value of iterable) {
  console.log(value);
}

The above example will print the following 3 lines:

bar
baz
qux

Type Parameters

T

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

Parameters

source: Iterable<T> | AsyncIterable<T>

The async iterable to drop elements from. It can be either finite or infinite.

predicate: (value: T, index: number) => boolean | Promise<boolean>

A predicate function to test each source element for a condition; the second parameter of the function represents the index of the source element. It can be either a synchronous or an asynchronous function.

Returns

AsyncIterableIterator<T>

An async iterable that contains elements from the source iterable that occur that and after the element at which the predicate first fails.