Distributive
class Functor g => Distributive g where distribute :: Functor f => f (g a) -> g (f a) distribute = collect id collect :: Functor f => (a -> g b) -> f a -> g (f b) collect f = distribute . fmap f distributeM :: Monad m => m (g a) -> g (m a) distributeM = fmap unwrapMonad . distribute . WrapMonad collectM :: Monad m => (a -> g b) -> m a -> g (m b) collectM f = distributeM . liftM fcotraverse :: (Distributive g, Functor f) => (f a -> b) -> f (g a) -> g bcotraverse f = fmap f . distributecomapM :: (Distributive g, Monad m) => (m a -> b) -> m (g a) -> g bcomapM f = fmap f . distributeM
((e)) is a distributive
instance Distributive ((->)e) where distribute a e = fmap ($e) a collect f q e = fmap (flip f e) q
Prelude Data.Distributive> distribute [(+1),(+2)] 1[2,3]Prelude Data.Distributive> collect (^) [1,2] 3[1,8]
Haskell Language Learning note (distributive)