Last active
March 11, 2016 06:59
-
-
Save chriseidhof/a3b6107c5ed829f6515a to your computer and use it in GitHub Desktop.
Protocol Extensions
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
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