Prelude API
    Preparing search index...

    Function first

    • Creates a function that returns the first element from an iterable, with optional sorting. If a comparator is provided, sorts the values before returning the first element. Otherwise, returns the first element from the values in their original order.

      Type Parameters

      • T

        Type of elements in the iterable

      Parameters

      • Optionalcmp: Cmp.t<T, T>

        Optional comparator function to use for sorting

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

      A function that takes an iterable and returns its first element

      Error if the iterable is empty

      // Get first element without sorting
      const firstElement = first()([3, 1, 4, 2, 5])
      // Result: 3
      // Get first element after sorting (ascending)
      const firstSorted = first((a: number, b: number) => a - b)([3, 1, 4, 2, 5])
      // Result: 1
      // Get first element after sorting (descending)
      const firstDescending = first((a: number, b: number) => b - a)([3, 1, 4, 2, 5])
      // Result: 5

      maybeFirst - For a non-throwing variant that returns undefined if the iterable is empty