Prelude API
    Preparing search index...

    Function maybeFirst

    • Creates a function that returns the first element from an iterable or undefined if empty, with optional sorting.

      Type Parameters

      • T

        The type of elements in the iterable

      Parameters

      • Optionalcmp: Cmp.t<T, T>

        Optional comparator function to use for sorting the iterable before getting the first element

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

      A function that takes an iterable and returns its first element or undefined if empty

      // Get first element without sorting
      const first = G.maybeFirst();
      first([3, 1, 2]); // 3
      first([]); // undefined

      // Get smallest element with sorting
      const ascending = (a, b) => a - b;
      const smallest = G.maybeFirst(ascending);
      smallest([3, 1, 2]); // 1
      smallest([]); // undefined

      first - For a throwing variant when no elements exist