@mirei/ts-collections
    Preparing search index...

    Function permutations

    • Generates permutations from the distinct elements of the source iterable.

      Type Parameters

      • TElement

        Type of elements within the source iterable.

      Parameters

      • source: Iterable<TElement>

        The source iterable.

      • Optionalsize: number

        Optional target length for each permutation. When omitted, permutations use all distinct elements of the source.

      Returns IEnumerable<IEnumerable<TElement>>

      A lazy sequence of permutations, each materialised as an enumerable.

      Thrown when size is less than 1 or greater than the number of distinct elements.

      source is enumerated to collect distinct elements before permutations are produced. Expect combinatorial growth in the number of permutations.

      const numbers = [1, 2, 3];
      const perms = permutations(numbers, 2);
      console.log(perms.select(p => p.toArray()).toArray()); // [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]