Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/iter/lib/transformers.ts>takeWhile

A bunch of utilities for working with iterables, many inspired by the native array methods.
Latest
function takeWhile
import { takeWhile } from "https://deno.land/x/iter@v3.2.3/lib/transformers.ts";

Returns a new iterable which yields while f returns true.

Examples

Example 1

import * as iter from "https://deno.land/x/iter/mod.ts";

const naturals = iter.create.increments(1);
const numbers = iter.takeWhile(naturals, (n) => n <= 5);

for (const num of numbers) {
  console.log(num);
}

// -> 1
// -> 2
// -> 3
// -> 4
// -> 5

Parameters

it: Iterable<T>
  • The iterable being cut.
  • A function that accepts up to three arguments. The takeWhile function calls f one time for each item in the iterable. included.

Returns

A new iterables of it which terminates