Skip to content

Instantly share code, notes, and snippets.

@aplr
Last active August 29, 2022 02:36
Show Gist options
  • Save aplr/e57d50365b61c73abb367ca679cf40cf to your computer and use it in GitHub Desktop.
Save aplr/e57d50365b61c73abb367ca679cf40cf to your computer and use it in GitHub Desktop.
A combine operator for transforming unwrapped values and passing along nil values
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