This type of combination/permutation is missing. What is it called?

As @pyrtsa said, it is the cartesian product (in the latter case the n-ary cartesian product). For the example you provided, where the collections are arrays of the same type, you can naively define your own variadic product function, e.g.

func product<Element>(_ collections: [Element]...) -> [[Element]] {
  guard let first = collections.first?.map({ [$0] }) else { return [] }

  return collections[1...].reduce(first) { partialResult, next in
    partialResult.flatMap { row in
      next.map { element in row + [element] }
    }
  }
}

Your latter example would then be:

product([min.x, max.x], [min.y, max.y], [min.z, max.z])

A better approach would require creating ProductSequence and ProductSequence.Iterator, following how product(_:_:) and all the other Swift Algorithms have been implemented.


Not quite, in the general case, the cartesian product count is the product of the of the counts of the sets involved.


A relevant topic:

2 Likes