Skip to content

Instantly share code, notes, and snippets.

@eneko
Last active June 3, 2020 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eneko/caad5cec0306143afb6d6c1ec231e950 to your computer and use it in GitHub Desktop.
Save eneko/caad5cec0306143afb6d6c1ec231e950 to your computer and use it in GitHub Desktop.
Cartesian product of arrays
// 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