我经常发现我所谓的“纯粹应用性”的用处Either
", i.e. Either
与Applicative
只要我们不实现一个实例就可用Monad
实例也是如此。
newtype AEither e a = AEither { unAEither :: Either e a }
deriving Functor
-- technically we only need Semigroup
instance Monoid e => Applicative (AEither e) where
pure a = AEither (pure a)
AEither e1 <*> AEither e2 = AEither (combine e1 e2) where
combine (Right f) (Right a) = Right (f a)
combine (Left m1) (Left m2) = Left (m1 <> m2)
combine (Left m ) _ = Left m
combine _ (Left m ) = Left m
这是一个非常有用的Applicative
因为它提供了比“错误总结”更强大的概念Either
's Monad
实例可以做。为此,我发现自己一遍又一遍地实施它。
某处有标准实例吗?有没有标准的名字?
这看起来与 Acc Validation 类型非常相似validation
包裹:http://hackage.haskell.org/package/validation-0.3.1/docs/Data-Validation.html http://hackage.haskell.org/package/validation-0.3.1/docs/Data-Validation.html
Edit:
特别是以下实例声明:
instance Semigroup err => Apply (AccValidation err) where
AccFailure e1 <.> AccFailure e2 =
AccFailure (e1 <> e2)
AccFailure e1 <.> AccSuccess _ =
AccFailure e1
AccSuccess _ <.> AccFailure e2 =
AccFailure e2
AccSuccess f <.> AccSuccess a =
AccSuccess (f a)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)