Prelude API
    Preparing search index...

    Function maybeFind

    • Creates a function that finds the first element in an iterable that satisfies a predicate. Returns undefined if no element is found.

      Type Parameters

      • T

        The type of elements in the iterable

      Parameters

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

        Function that tests each element; receives element and index

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

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

      // Find first even number
      const findEven = G.maybeFind(x => x % 2 === 0);
      findEven([1, 3, 5, 6, 7]); // 6
      findEven([1, 3, 5, 7]); // undefined

      // Find using index
      const findSecondItem = G.maybeFind((_, i) => i === 1);
      findSecondItem(['a', 'b', 'c']); // 'b'

      find - For a throwing variant when no element is found