从 Either 提升到 IO
Lifting from Either to IO
我想在没有 Either 的任何线索的情况下从 Either monad 转换为 IO。有更优雅的方法吗?
我写了这个函数,但我想使用库或 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
使用 Data.Either
中的 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
我想在没有 Either 的任何线索的情况下从 Either monad 转换为 IO。有更优雅的方法吗?
我写了这个函数,但我想使用库或 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
使用 Data.Either
中的 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