Created
May 4, 2017 03:01
-
-
Save xuyazhong/34fa6470e5fe02a4b3992ce935705a0a to your computer and use it in GitHub Desktop.
Optional Map 和flatMap
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
https://github.com/apple/swift/blob/master/stdlib/public/core/Optional.swift | |
@_inlineable | |
public func map<U>(_ transform: (Wrapped) throws -> U) rethrows -> U? { | |
switch self { | |
case .some(let y): | |
return .some(try transform(y)) | |
case .none: | |
return .none | |
} | |
} | |
@_inlineable | |
public func flatMap<U>(_ transform: (Wrapped) throws -> U?) rethrows -> U? { | |
switch self { | |
case .some(let y): | |
return try transform(y) | |
case .none: | |
return .none | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment