Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xuyazhong/34fa6470e5fe02a4b3992ce935705a0a to your computer and use it in GitHub Desktop.
Save xuyazhong/34fa6470e5fe02a4b3992ce935705a0a to your computer and use it in GitHub Desktop.
Optional Map 和flatMap
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