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

x/iter/lib/reducers.ts>find

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

Returns the value of the first item in the iterable where predicate is true, and undefined otherwise.

Examples

Example 1

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

const naturals = iter.create.increments(1);
// Find a solution to 20n = 2n² - 6n, n ∈ ℕ
const solution1 = iter.find(naturals, (n) => 20 * n === 2 * n ** 2 - 6 * n);

console.log(solution1); // -> 13

// Will never return.
// Find a solution to n³ = 3n, n ∈ ℕ
// const solution2 = iter.find(naturals, (n) => n ** 3 === 3 * n);

Parameters

it: Iterable<T>
  • The iterable to search.
  • Find calls predicate once for each item in the iterable, in ascending order, until it finds one where predicate returns true. If such an item is found, find immediately returns that element value, Otherwise, find returns undefined.

Returns

T | undefined

The first item which satisfied predicate.