Haskell语言学习笔记(58)Bifoldable

Bifoldable

class Bifoldable p where
  bifold :: Monoid m => p m m -> m
  bifold = bifoldMap id id

  bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> p a b -> m
  bifoldMap f g = bifoldr (mappend . f) (mappend . g) mempty

  bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> p a b -> c
  bifoldr f g z t = appEndo (bifoldMap (Endo #. f) (Endo #. g) t) z

  bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> p a b -> c
  bifoldl f g z t = appEndo (getDual (bifoldMap (Dual . Endo . flip f) (Dual . Endo . flip g) t)) z

Either 是 Bifoldable

instance Bifoldable Either where
  bifoldMap f _ (Left a) = f a
  bifoldMap _ g (Right b) = g b

(,) 是 Bifoldable

instance Bifoldable (,) where
  bifoldMap f g ~(a, b) = f a `mappend` g b

Const 是 Bifoldable

instance Bifoldable Const where
  bifoldMap f _ (Const a) = f a

应用 Bifoldable

Prelude Data.Bifoldable> bifoldr (^) (-) 2 (Left 2)
4
Prelude Data.Bifoldable> bifoldr (^) (-) 2 (Right 3)
1
Prelude Data.Bifoldable> bifoldr (^) (-) 2 (2,3)
2
Prelude Data.Bifoldable Control.Applicative> bifoldr (^) (-) 2 (Const 2)
4
Prelude Data.Bifoldable> bifoldl (^) (-) 4 (Left 2)
16
Prelude Data.Bifoldable> bifoldl (^) (-) 4 (Right 3)
1
Prelude Data.Bifoldable> bifoldl (^) (-) 4 (2,3)
13
Prelude Data.Bifoldable Control.Applicative> bifoldl (^) (-) 4 (Const 2)
16

手动计算

bifoldr (^) (-) 1 (2,3)
= appEndo (bifoldMap (Endo #. (^)) (Endo #. (-)) (2,3)) 1
= appEndo ((Endo #. (^) $ 2) (Endo #. (-) $ 3)) 1
= appEndo ((Endo (2^)) `mappend` (Endo (3-))) $ 1
= (2^) . (3-) $ 1
= 2 ^ (3 - 1) = 4
bifoldl (^) (-) 4 (2,3)
= appEndo (getDual (bifoldMap (Dual . Endo . flip (^)) (Dual . Endo . flip (-)) (2,3))) 4
= appEndo (getDual ((Dual . Endo . flip (^) $ 2)  `mappend` (Dual . Endo . flip (-) $ 3))) 4
= appEndo (getDual ((Dual $ Endo (^2)) `mappend` (Dual $ Endo (subtract 3)))) 4
= (subtract 3) . (^2) $ 4
= (4 ^ 2) - 3 = 13
时间: 2024-11-09 00:49:01

Haskell语言学习笔记(58)Bifoldable的相关文章

Haskell语言学习笔记(64)Lens(4)

Prisms data NewTask = SimpleTask String | HarderTask String Int | CompoundTask String [NewTask] deriving (Show) makePrisms ''NewTask *Main> a ^? _SimpleTask Just "Clean" *Main> b ^? _HarderTask Just ("Clean Kitchen",15) *Main>

Haskell语言学习笔记(69)Yesod

Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安装失败) $ cabal install yesod Installed yesod-1.4.5 Hello World -- helloworld.hs {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE QuasiQuotes #-} {-# LANG

Haskell语言学习笔记(59)Bitraversable

Bitraversable class (Bifunctor t, Bifoldable t) => Bitraversable t where bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> t a b -> f (t c d) bitraverse f g = bisequenceA . bimap f g bisequenceA :: (Bitraversable t, Applicati

Haskell语言学习笔记(19)File IO

关于IO Action 类型为IO t. 运算时不执行,因而没有任何效果,只有执行时才会有效果,产生副作用. 一个IO Action只有在其他IO Action中才能被执行. 类型为IO t的IO Action被执行后的结果类型为t. File IO 函数 openFile hClose

Haskell语言学习笔记(44)Lens(2)

preview, review Prelude Control.Lens> view _Left (Left "abc") "abc" Prelude Control.Lens> view _Right (Right "abc") "abc" Prelude Control.Lens> view _Just (Just "abc") "abc" Prelude Cont

Haskell语言学习笔记(49)ByteString Text

Data.ByteString String 是 [Char] 的同义词,在使用上存在List的惰性所带来的性能问题. 在处理大型二进制文件时,可以使用 ByteString 来代替 String. ByteString 包含 Lazy 模块 Data.ByteString.Lazy 和 Strict 模块 Data.ByteString. 其中 Lazy 模块内部使用 chunks(64K数据块). Prelude> import qualified Data.ByteString.Lazy

Haskell语言学习笔记(55)Data.Vector

Data.Vector Construction Prelude V> import Data.Vector as V Prelude V> V.empty [] Prelude V> V.singleton 1 [1] Prelude V> V.replicate 5 1 [1,1,1,1,1] Prelude V> generate 5 (+1) [1,2,3,4,5] Prelude V> iterateN 5 (*2) 1 [1,2,4,8,16]

Haskell语言学习笔记(57)Parsec(4)

Parser 类型 data ParsecT s u m a type Parsec s u = ParsecT s u Identity type Parser = Parsec String () data ParsecT s u m a ParsecT 带四个类型参数:数据流类型 s,用户状态类型 u,底层Monad类型 m,返回类型 a. ParsecT 是一个Monad转换器. type Parsec s u = ParsecT s u Identity Parsec 类型是 Pars

Haskell语言学习笔记(60)Biapplicative

Biapplicative class Bifunctor p => Biapplicative p where bipure :: a -> b -> p a b (<<*>>) :: p (a -> b) (c -> d) -> p a c -> p b d (*>>) :: p a b -> p c d -> p c d a *>> b = bimap (const id) (const id) &