Prelude API
    Preparing search index...

    Function find

    • Creates a function that returns the first element in an iterable that satisfies the predicate. Similar to Array.prototype.find(), but works with any iterable and throws an error if no match is found.

      Type Parameters

      • T

        Type of elements in the iterable

      Parameters

      • predicate: (value: T, index: number) => boolean

        Function to test each element and its index

      Returns (values: Iterable<T>) => T

      A function that takes an iterable and returns the first matching element

      Error if no element passes the predicate test

      // Find first even number
      const firstEven = find((n: number) => n % 2 === 0)([1, 3, 4, 5, 6])
      // Result: 4
      // This would throw "Not found." error
      try {
      const firstNegative = find((n: number) => n < 0)([1, 2, 3, 4])
      } catch (error) {
      // Error: "Not found."
      }

      maybeFind - For a non-throwing variant that returns undefined if not found