码迷,mamicode.com
首页 > 编程语言 > 详细

Haskell语言学习笔记(61)Distributive

时间:2017-12-10 14:22:14      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:mon   down   haskell   markdown   log   fun   collect   data   学习笔记   

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

标签:mon   down   haskell   markdown   log   fun   collect   data   学习笔记   

原文地址:http://www.cnblogs.com/zwvista/p/8016689.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!