Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
Created March 24, 2015 01:20
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/e60a22eb06b2ae7c0edc to your computer and use it in GitHub Desktop.
Save JadenGeller/e60a22eb06b2ae7c0edc to your computer and use it in GitHub Desktop.
Filter an Array with a Boolean Array as a mask
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 })
}
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