Created
March 24, 2015 01:57
-
-
Save JadenGeller/a99d51d662b14dcad72e to your computer and use it in GitHub Desktop.
Swift Choose Function
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
func choose<S : SequenceType, T : SequenceType, U : SequenceType where U.Generator.Element == Bool, S.Generator.Element == T.Generator.Element>(onTrue: S, onFalse: T, picker: U) -> [S.Generator.Element]{ | |
return map2(Zip2(onTrue, onFalse), picker, { zipped, choice in choice ? zipped.0 : zipped.1 }) | |
} | |
// Note map2 is defined here: https://gist.github.com/JadenGeller/6f5c5dcb16c39b936d17 |
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
let x = [1,2,3,4,5] | |
let y = [6,7,8,9,10] | |
let choices = [false, true, false, true, false] | |
choose(x, y, choices) // -> [6, 2, 8, 4, 10] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment