Skip to content

Instantly share code, notes, and snippets.

@denisoliveira
Last active November 13, 2019 20:54
Show Gist options
  • Save denisoliveira/e2b7b24da99f886cb3ebd8f6b702acf5 to your computer and use it in GitHub Desktop.
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.
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