Since this is not built into the language, here's a simple function with a similar signature to the built-in zip
:
func cartesianProduct<Sequence1, Sequence2>(_ sequence1: Sequence1, _ sequence2: Sequence2) -> [(Sequence1.Element, Sequence2.Element)]
where Sequence1 : Sequence, Sequence2 : Sequence
{
var result: [(Sequence1.Element, Sequence2.Element)] = .init()
sequence1.forEach { value1 in
sequence2.forEach { value2 in
result.append((value1, value2))
}
}
return result
}
print(Array(zip([1, 2, 3], ["a", "b"]))) // [(1, "a"), (2, "b")]
print(cartesianProduct([1, 2, 3], ["a", "b"])) // [(1, "a"), (1, "b"), (2, "a"), (2, "b"), (3, "a"), (3, "b")]
In your case, you could do:
cartesianProduct([1,2,3,4,5], ["one","two","three","four","five"])
.map { "\($0.1) \($0.0)" }
or even:
cartesianProduct(1...5, ["one","two","three","four","five"])
.map { "\($0.1) \($0.0)" }
both of which will produce the sequence:
["one 1", "two 1", "three 1", "four 1", "five 1", "one 2", "two 2", "three 2", "four 2", "five 2", ...]
Since this is common to do on a collection's elements, I also created these two functional extensions:
extension Collection {
/// O(n^2)
func pairElementToEveryOtherElement() -> [(Self.Element, Self.Element)] {
var result = [(Self.Element, Self.Element)]()
for i in indices {
var j = index(after: i)
while j != endIndex {
result.append((self[i], self[j]))
j = index(after: j)
}
}
return result
}
/// O(n)
public func pairElementToNeighbors() -> [(Self.Element, Self.Element)] {
if isEmpty {
return .init()
}
var result: [(Self.Element, Self.Element)] = .init()
var i = startIndex
while index(after: i) != endIndex {
result.append((self[i], self[index(after: i)]))
i = index(after: i)
}
return result
}
}
These can be used like follows:
let inefficientHasDuplicatesCheck = myCollection
.pairElementToEveryOtherElement()
.contains { $0.0 == $0.1 }
zipWith
would work:_.zipWith(array1, array2, function(a,b) { return a + ' ' + b; });
– Soliloquy