Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
Created March 24, 2015 01:57
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 JadenGeller/a99d51d662b14dcad72e to your computer and use it in GitHub Desktop.
Save JadenGeller/a99d51d662b14dcad72e to your computer and use it in GitHub Desktop.
Swift Choose Function
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
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