Simultaneously mapping over publisher values and errors
31 Dec 2020 ⇐ Notes archive(This is an entry in my technical notebook. There will likely be typos, mistakes, or wider logical leaps — the intent here is to “let others look over my shoulder while I figure things out.”)
Dalton nerd sniped me earlier today when he asked:
Combine question: I’d love a “map”-like operator that is failable — like
tryMap
— but where I end up with a strongly-typedError
. I’m picturing something akin to,
func mapResult<NewOutput, NewFailure>(_ transform: @escaping (Result<Output, Failure>) -> Result<NewOutput, NewFailure>) -> AnyPublisher<NewOutput, NewFailure>
.Does this exist in the framework?
It doesn’t, yet Result
and Result.publisher
make this wicked fun to pull off.
The transform
argument is also a specialized form of Either.bimap
hiding in plain sight.