Skip to content

Instantly share code, notes, and snippets.

@toshi0383
Created January 7, 2017 03:27
Show Gist options
  • Save toshi0383/276b83f4759fc0753c72eef1372b82d7 to your computer and use it in GitHub Desktop.
Save toshi0383/276b83f4759fc0753c72eef1372b82d7 to your computer and use it in GitHub Desktop.
2つの配列の要素すべての組み合わせを出力 (swift) #CodePiece
import Foundation
func combine<E1, E2>(_ arr1: [E1], _ arr2: [E2]) -> [(E1, E2)] {
guard !arr1.isEmpty && !arr2.isEmpty else {
return []
}
func _c(_ e1: E1, _ arr2: [E2]) -> [(E1, E2)] {
return arr2.map{(e1, $0)}
}
return _c(arr1[0], arr2) + combine(arr1.dropFirst().map{$0}, arr2)
}
print(combine([1,2,3], ["a", "b"]))
print(combine([1], ["a", "b"]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment