Prelude API
    Preparing search index...

    Function extreme

    • Finds both the minimum and maximum values in an iterable based on a comparison function.

      Type Parameters

      • T

        Type of elements in the iterable

      Parameters

      • f: (a: T, b: T) => number

        Comparison function that returns a number (negative if a < b, 0 if a = b, positive if a > b)

      Returns (values: Iterable<T>) => { max: T; min: T } | undefined

      A function that takes an iterable and returns an object with min and max properties, or undefined if the iterable is empty

      // Find min and max numbers
      const result = extreme((a, b) => a - b)([3, 1, 4, 1, 5, 9, 2, 6])
      // Result: { min: 1, max: 9 }
      // Find min and max string lengths
      const result = extreme((a, b) => a.length - b.length)(["apple", "banana", "kiwi", "strawberry"])
      // Result: { min: "kiwi", max: "strawberry" }
      // With an empty iterable
      const result = extreme((a, b) => a - b)([])
      // Result: undefined