ابتدای محتوای صفحه
۲۵ - ۳ترانسفورمرِ Either یا EitherT
همونطور که Maybe یه نسخهی ترانسفورمر به شکلِ MaybeT داره، برای Either هم میشه یه نسخهی ترانسفورمر درست کرد، اسمش هم میذاریم EitherT. حالا نوبتِ شماست که نمونههاش رو بنویسین:
newtype EitherT e m a =
EitherT { runEitherT :: m (Either e a) }تمرینها: EitherT
۱.
برای EitherT نمونه ِ Functor بنویسین:
instance Functor m
=> Functor (EitherT e m) where
fmap = undefined۱.
برای EitherT نمونه ِ Applicative بنویسین:
instance Applicative m
=> Applicative (EitherT e m) where
pure = undefined
f <*> a = undefined۱.
برای EitherT نمونه ِ Monad بنویسین:
instance Monad m
=> Monad (EitherT e m) where
return = pure
v >>= a = undefined۴.
تابع کمکی ِ swapEitherT رو برای EitherT بنویسین.
-- swapEither نسخهی ترانسفورمر برای
swapEitherT :: (Functor m)
=> EitherT e m a
-> EitherT a m e
swapEitherT = undefinedراهنمایی: اول swapEither رو بنویسین، بعد به کمک اون swapEitherT رو بنویسین.
۵.
برای کاتامورفیسم ِ either نسخهی ترانسفورمر بنویسین.
eitherT :: Monad m =>
(a -> m c)
-> (b -> m c)
-> EitherT a m b
-> m c
eitherT = undefined