Skip to content

Instantly share code, notes, and snippets.

@mbrandonw
Last active August 29, 2015 14:02
Show Gist options
  • Save mbrandonw/535295308a89da6a50fd to your computer and use it in GitHub Desktop.
Save mbrandonw/535295308a89da6a50fd to your computer and use it in GitHub Desktop.
`compact` function in Swift
func compact <A> (xs: [A?]) -> [A] {
return xs.reduce([]) { accum, x in
if let x = x {
return accum + [x]
}
return accum
}
}
var xs: [Int?] = [1, 2, nil, 3]
compact(xs) // => [1, 2, 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment