Mapping over Either's Left

给你一囗甜甜゛ 提交于 2019-12-03 08:35:51

问题


Somewhere in my app I receive an Either ParserError MyParseResult from Parsec. Downstream this result gets some other parsing done over using other libs. During that second phase of parsing there also may occur some kind of error which I would like to pass as a Left String, but for that I need to convert the result from Parsec to String too. To achieve that I need a function which will allow me to map over a Left with a show function.

The mapping function I'm thinking of looks something like this:

mapLeft :: (a -> b) -> Either a c -> Either b c
mapLeft f (Left x) = Left $ f x
mapLeft _ x = x

But I was quite surprised not to find anything matching on hackage db. So now I'm having doubts whether I'm using a correct approach to my problem.

Why isn't there such a function in standard lib? What is wrong with my approach?


回答1:


We have such a function in the standard libraries,

Control.Arrow.left :: a b c -> a (Either b d) (Either c d)

is the generalisation to arbitrary Arrows. Substitute (->) for a and apply it infix, to get the specialisation

left :: (b -> c) -> Either b d -> Either c d

There is nothing wrong with your approach in principle, it's a sensible way to handle the situation.




回答2:


Another option is to use Bifunctor instance of Either. Then you have

first :: (a -> b) -> Either a c -> Either b c

(Also Bifunctor can be used to traverse over the first part of (a,b).)




回答3:


This can be done easily with lens:

import Control.Lens

over _Left (+1) $ Left 10   => Left 11
over _Left (+1) $ Right 10  => Right 10
over _Right (+1) $ Right 10 => Right 11



回答4:


Another simple option is mapLeft in Data.Either.Combinators:

mapLeft :: (a -> c) -> Either a b -> Either c b


来源:https://stackoverflow.com/questions/13503965/mapping-over-eithers-left

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!