Created
January 7, 2017 03:27
-
-
Save toshi0383/276b83f4759fc0753c72eef1372b82d7 to your computer and use it in GitHub Desktop.
2つの配列の要素すべての組み合わせを出力 (swift) #CodePiece
This file contains 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
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