【发布时间】:2017-01-29 03:21:00
【问题描述】:
写这样的东西很好用:
data Either a b = Left a | Right b
instance Functor (Either a) where
fmap _ (Left x) = Left x
fmap f (Right x) = Right (f x)
现在假设我想反转这个,Left 将 f 应用于值:
instance Functor (Either a) where
fmap _ (Right x) = Right x
fmap f (Left x) = Left (f x)
这不编译,我想我需要像Functor (Either _ b) 这样的东西,我该怎么做?
【问题讨论】:
-
你不能,至少不能直接。您可以为
Either制作一个newtype包装器。
标签: haskell types typeclass functor bifunctor