Last active
June 3, 2020 21:53
-
-
Save eneko/caad5cec0306143afb6d6c1ec231e950 to your computer and use it in GitHub Desktop.
Cartesian product of arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Cartesian product of two arrays | |
func * <U, V>(lhs: [U], rhs: [V]) -> [(U, V)] { | |
lhs.flatMap { left in | |
rhs.map { right in | |
(left, right) | |
} | |
} | |
} | |
print([1, 2, 3] * ["a", "b"]) | |
// [(1, "a"), (1, "b"), (2, "a"), (2, "b"), (3, "a"), (3, "b")] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment