Prelude API
    Preparing search index...

    Function combinations

    • Creates a generator that yields all k-element combinations from an iterable. This is a safe version that creates a new array for each combination.

      Type Parameters

      • T

        Type of elements in the iterable

      Parameters

      • Optionalk: number

        Optional size of each combination (defaults to the length of the input, i.e. a single combination of all elements)

      Returns (values: Iterable<T>) => Generator<T[]>

      A function that takes an iterable and returns a generator of combinations

      Arrays containing each k-element combination from the input

      // Generate all 2-element combinations
      const pairs = [...combinations(2)([1, 2, 3, 4])]
      // Result: [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
      // Generate all 3-element combinations
      const triplets = [...combinations(3)([1, 2, 3, 4])]
      // Result: [[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]

      unsafeCombinations - The underlying implementation that reuses arrays