import { findIndex } from "https://deno.land/x/iter@v3.2.3/lib/reducers.ts";
Returns the index of the first item in the iterable where predicate is true, and -1 otherwise.
Examples
Example 1
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 solutionIndex1 = iter.findIndex(
naturals,
(n) => 20 * n === 2 * n ** 2 - 6 * n,
);
console.log(solutionIndex1); // -> false
// Will never return.
// Find a solution to n³ = 3n, n ∈ ℕ
const solutionIndex2 = iter.findIndex(naturals, (n) => n ** 3 === 3 * n);
const first10 = iter.take(naturals, 10);
const pos = iter.findIndex(first10, x => x < 0);
console.log(pos); // -> -1
Parameters
it: Iterable<T>
- The iterable to search.
predicate: IterablePredicateCallback<T>
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, findIndex immediately returns that element index. Otherwise, findIndex returns -1.