Haskell语言学习笔记(61)Distributive
Posted zwvista
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Haskell语言学习笔记(61)Distributive相关的知识,希望对你有一定的参考价值。
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 f
cotraverse :: (Distributive g, Functor f) => (f a -> b) -> f (g a) -> g b
cotraverse f = fmap f . distribute
comapM :: (Distributive g, Monad m) => (m a -> b) -> m (g a) -> g b
comapM f = fmap f . distributeM
((->)e) 是个 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语言学习笔记(61)Distributive的主要内容,如果未能解决你的问题,请参考以下文章