Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Last active March 11, 2016 06:59
Show Gist options
  • Save chriseidhof/a3b6107c5ed829f6515a to your computer and use it in GitHub Desktop.
Save chriseidhof/a3b6107c5ed829f6515a to your computer and use it in GitHub Desktop.
Protocol Extensions
protocol OptionalType {
typealias T
var optional: T? { get }
}
extension Optional : OptionalType {
var optional: T? { return self }
}
extension SequenceType where Generator.Element: OptionalType {
var flatten: [Self.Generator.Element.T] {
return self.map { $0.optional }.filter { $0 != nil }.map { $0! }
}
}
let z: [Int?] = [1,2,nil,3]
z.flatten
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment