Last active
November 13, 2019 20:54
-
-
Save denisoliveira/e2b7b24da99f886cb3ebd8f6b702acf5 to your computer and use it in GitHub Desktop.
Add support on Swift Optional Type to perform a flatMap with a statically referenced instance method rather than returns a instance method references.
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
extension Optional { | |
func map(_ clojure: (Wrapped) -> () -> Wrapped) -> Wrapped? { | |
guard let self = self else { return nil } | |
return clojure(self)() | |
} | |
func map<T>(_ keyPath: KeyPath<Wrapped, T>) -> T? { | |
guard let self = self else { return nil } | |
return self[keyPath: keyPath] | |
} | |
func flatMap(_ clojure: (Wrapped) -> () -> Wrapped?) -> Wrapped? { | |
guard let self = self else { return nil } | |
return clojure(self)() | |
} | |
func flatMap<T>(_ keyPath: KeyPath<Wrapped, T?>) -> T? { | |
guard let self = self else { return nil } | |
return self[keyPath: keyPath] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment