Last active
August 29, 2022 02:36
-
-
Save aplr/e57d50365b61c73abb367ca679cf40cf to your computer and use it in GitHub Desktop.
A combine operator for transforming unwrapped values and passing along nil values
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 { | |
associatedtype Wrapped | |
var value: Wrapped? { get } | |
} | |
extension Optional: OptionalType { | |
public var value: Wrapped? { | |
return self | |
} | |
} | |
extension Publisher where Output: OptionalType { | |
func mapUnwrapped<Result>(_ transform: @escaping (Output.Wrapped) -> Result?) -> Publishers.Map<Self, Result?> { | |
map { $0.value.flatMap(transform) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment