Prelude API
    Preparing search index...

    Function fork

    • Creates a function that applies multiple functions to the same iterable, returning an array of results. Uses memoization to ensure the iterable is only traversed once.

      Type Parameters

      • T

        Type of elements in the iterable

      • Args extends ((values: Iterable<T>, index: number) => unknown)[]

        Array of functions that take an iterable and return some value

      Parameters

      • ...fs: Args

        One or more functions to apply to the iterable

      Returns (
          values: Iterable<T>,
      ) => { [K in string | number | symbol]: ReturnType<Args[K]> }

      A function that takes an iterable and returns an array of results from each function

      // Calculate sum and product in a single pass
      const sumAndProduct = fork(
      values => [...values].reduce((a, b) => a + b, 0),
      values => [...values].reduce((a, b) => a * b, 1)
      )
      const [sum, product] = sumAndProduct([1, 2, 3, 4])
      // Result: [10, 24]
      // Calculate multiple statistics
      const stats = fork(
      values => Math.min(...values),
      values => Math.max(...values),
      values => [...values].reduce((a, b) => a + b, 0) / [...values].length
      )
      const [min, max, avg] = stats([5, 10, 15, 20])
      // Result: [5, 20, 12.5]