我经常发现我称之为“纯应用Either
”的东西,即只要我们不实现实例,实例就可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
'sMonad
实例更强大的“错误总结”概念。为此,我发现自己一遍又一遍地实施它。
某处有标准实例吗?有没有标准的名字?