Last active
August 29, 2015 14:12
-
-
Save apparentsoft/cc0a67de7301351f1ae2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 catOptionals<T,S where S:SequenceType, S.Generator.Element == Optional<T>>(optionals: S) -> [T] { | |
return reduce(optionals, []) { (acc, value: T?) -> [T] in | |
switch value { | |
case let .Some(v): return acc + [v] | |
default: return acc | |
} | |
} | |
} | |
let a: [Int?] = [4,7,nil,8] | |
catOptionals(a) | |
// returns [4,7,8] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I guess I'll stick with a O(2n) pass with a filter then map will be better than a O(n^2) for now.