我想从Ead转换为IO而没有来自Either的任何线索。是否有更优雅的方式来做到这一点?
我已经编写了这个函数,但我想使用库中的东西或Prelude:
liftEither :: forall t (m :: * -> *) a. Monad m => (t -> m a) -> Either String t -> m a
liftEither f (Right a) = f a
liftEither _ (Left msg) = fail msg
答案 0 :(得分:5)
使用either
中的Data.Either
函数:
liftEither :: Monad m => (t -> m a) -> Either String t -> m a
liftEither f xs = either fail f xs
甚至更简单:
liftEither :: Monad m => (t -> m a) -> Either String t -> m a
liftEither = either fail