我将举例说明我想立即做些什么。
version1 :: IO ()
version1 =
if boolCheck
then case maybeCheck of
Nothing -> putStrLn "Error: simple maybe failed"
Just v -> case eitherCheck of
Left e -> putStrLn $ "Error: " ++ show e
Right w -> monadicBoolCheck v >>= \case
False -> putStrLn "Error: monadic bool check failed"
True -> print "successfully doing the thing"
else putStrLn "simple bool check failed"
基本上我想在一些检查结果为正的情况下“做一件事”。 每当一次检查结果为否定时,我想保留有关违规检查的信息并中止任务。 在现实生活中,这些检查有不同的类型,因此我称之为
boolCheck :: Bool
maybeCheck :: Maybe a
eitherCheck :: Show a => Either a b
monadicBoolCheck :: Monad m => m Bool
这些只是一些例子。
随意想到monadic Maybe,EitherT
或单个列表,我提取head
并且当它不是单身时失败。
现在我正在尝试改进上面的实现,而Either
monad进入我的脑海,因为它有一个错误消息中止的概念。
version2 :: IO ()
version2 = do
result <- runEitherT $ do
if boolCheck
then pure ()
else left "simple bool check failed"
v <- case maybeCheck of
Just x -> pure x
Nothing -> left "simple maybe check failed"
w <- hoistEither . mapLeft show $ eitherCheck
monadicBoolCheck v >>= \case
True -> pure ()
False -> left "monadic bool check failed"
case result of
Left msg -> putStrLn $ "Error: " ++ msg
Right _ -> print "successfully doing the thing"
虽然我更喜欢version2
,但可读性的提高可能是微不足道的。
在添加进一步检查时,Version2更胜一筹。
最终有这种方法吗?
我不喜欢的事情:
1)我部分地滥用了Either
monad,而我实际上做的更像是Maybe
monad,其中Just
和Nothing
的卷在monadic中切换结合
2)将支票转换为Either
需要使用case
或转换功能(如hoistEither
)。
提高可读性的方法可能是:
1)定义辅助函数以允许像
这样的代码v <- myMaybePairToEither "This check failed" monadicMaybePairCheck
monadicMaybePairCheck :: Monad m => m (Maybe x, y)
...
myMaybePairToEither :: String -> m (Maybe x, y) -> EitherT m e z
myMaybePairToEither _ (Just x, y) = pure $ f x y
myMaybePairToEither msg (Nothing, _) = left msg
2)始终使用明确的案例,甚至不使用hoistEither
3)定义我自己的monad以阻止Either
滥用......我可以提供所有转换功能(如果没有人已经做过类似的话)
4)尽可能使用maybe
和either
5)......?
答案 0 :(得分:5)
使用maybe
,either
和mtl
包。通过by,eitherCheck :: Show a => Either a b
的{{1}}约束可能不是你想要的:它允许调用者选择他们想要的任何类型,只要类型实现Show a
。您可能打算让Show a
成为一种类型,以便呼叫者只能 能够在值上调用a
。大概!
show
答案 1 :(得分:3)
&#34;定义帮助函数&#34;正是我如何处理这个问题。 errors库已经提供了很多,可能除了满足<div class="exclude">
函数之外。对于那些我会just use when
/unless
。
当然,在可能的范围内,您应该宣传您所谓的适当多态的行为,以便不需要转换。
答案 2 :(得分:1)
因此,我可能首先将您的version2
重新修改为类似
import Control.Monad.Trans
import Control.Monad.Trans.Either hiding (left, right)
import Control.Monad
import Control.Applicative
import Control.Arrow
version3 :: IO ()
version3 = eitherT onFailure onSuccess $ do
guard boolCheck <|> fail "simple bool check failed"
v <- hoistEither $ maybe (Left "simple maybe check failed") Right maybeCheck
w <- hoistEither . left show $ eitherCheck
lift (guard =<< monadicBoolCheck v) <|> fail "monadic boolcheck failed"
where
onFailure msg = putStrLn $ "Error: "++msg
onSuccess _ = print "successfully doing the thing"
我发现它更具可读性,但仍然有点尴尬,所以如果我做了很多 像这样的代码,我会介绍一些帮助:
version4 :: IO ()
version4 = eitherT onFailure onSuccess $ do
failUnless "simple bool check failed" boolCheck
v <- hoistMaybe "simple maybe check failed" maybeCheck
w <- hoistEitherWith show eitherCheck
failUnless "monadic boolcheck failed" =<< lift (monadicBoolCheck v)
where
onFailure msg = putStrLn $ "Error: "++msg
onSuccess _ = print "successfully doing the thing"
failUnless :: Monad m => String -> Bool -> m ()
failUnless _ True = return ()
failUnless msg _ = fail msg
hoistMaybe :: Monad m => e -> Maybe a -> EitherT e m a
hoistMaybe err = hoistEither . maybe (Left err) Right
hoistEitherWith :: Monad m => (e -> e') -> Either e a -> EitherT e' m a
hoistEitherWith f = hoistEither . left f
答案 3 :(得分:0)
为了获得全部可能的选项,请查看这个要点:
https://gist.github.com/rubenmoor/c390901247e4e7bb97cf
它定义了几个辅助函数,基本上将maybe
,either
和throwError
组合在一起。并得到这样的代码。
gauntlet :: MonadError Error m => m (a, b, c)
gauntlet = do
assertTrue boolCheck $ Error "simple bool check failed"
v <- assertJust maybeCheck $ Error "simple maybe check failed"
assertNothing maybeCheck' $ Error . show
w <- assertRight eitherCheck $ Error . show
b <- monadicBoolCheck
assertTrue b $ Error "monadic bool check failed"
x <- assertSingletonList list $ Error "list not singleton"
pure (v, w, x)
version3 :: IO ()
version3 = putStrLn $
case gauntlet of
Left (Error e) -> "Error: " ++ e
Right result -> "successfully doing thing with result"