Created
March 24, 2015 01:20
-
-
Save JadenGeller/e60a22eb06b2ae7c0edc to your computer and use it in GitHub Desktop.
Filter an Array with a Boolean Array as a mask
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 filter<S : SequenceType, T : SequenceType where T.Generator.Element == Bool>(source: S, mask: T, defaultValue: S.Generator.Element) -> [S.Generator.Element] { | |
return Array(Zip2(source, mask)).map({ (value, include) in include ? value : defaultValue }) | |
} |
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 sel = [1,2,3,4,5] | |
let mask = [false, true, false, true, false] | |
filter(sel, mask, 0) // -> [0, 2, 0, 4, 0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment