Haskell语言学习笔记(42)Bifunctor
Bifunctor
class Bifunctor p where
bimap :: (a -> b) -> (c -> d) -> p a c -> p b d
bimap f g = first f . second g
first :: (a -> b) -> p a c -> p b c
first f = bimap f id
second :: (b -> c) -> p a b -> p a c
second = bimap id
Bifunctor(双协变函子) 是个类型类。
Bifunctor类型类带两个协变类型参数。
Bifunctor类型类包含三个函数。
- bimap :: (a -> b) -> (c -> d) -> p a c -> p b d
bimap函数同时修改 Bifunctor 的两个参数。 - first :: (a -> b) -> p a c -> p b c
first函数只修改 Bifunctor 的第一个参数。 - second :: (b -> c) -> p a b -> p a c
second函数只修改 Bifunctor 的第二个参数。
Bifunctor 的法则
法则
bimap id id ≡ id
first id ≡ id
second id ≡ id
bimap f g ≡ first f . second g
推论
bimap (f . g) (h . i) ≡ bimap f h . bimap g i
first (f . g) ≡ first f . first g
second (f . g) ≡ second f . second g
Either 是个Bifunctor
instance Bifunctor Either where
bimap f _ (Left a) = Left (f a)
bimap _ g (Right b) = Right (g b)
(,) 是个Bifunctor
instance Bifunctor (,) where
bimap f g ~(a, b) = (f a, g b)
Const 是个Bifunctor
instance Bifunctor Const where
bimap f _ (Const a) = Const (f a)
应用 Bifunctor
Prelude Data.Bifunctor> bimap (+2) (*3) (1,2)
(3,6)
Prelude Data.Bifunctor> bimap (+2) (*3) (Left 2)
Left 4
Prelude Data.Bifunctor> bimap (+2) (*3) (Right 2)
Right 6
Prelude Data.Bifunctor> first (+2) (1,2)
(3,2)
Prelude Data.Bifunctor> second (*3) (1,2)
(1,6)
Prelude Data.Bifunctor> first (+2) (Left 2)
Left 4
Prelude Data.Bifunctor> second (*3) (Right 2)
Right 6
Prelude Data.Bifunctor Control.Applicative> first (+2) (Const 2)
Const 4
Prelude Data.Bifunctor Control.Applicative> second (+2) (Const 2)
Const 2
Haskell语言学习笔记(42)Bifunctor的更多相关文章
- Haskell语言学习笔记(88)语言扩展(1)
ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...
- Haskell语言学习笔记(79)lambda演算
lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...
- Haskell语言学习笔记(69)Yesod
Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...
- Haskell语言学习笔记(20)IORef, STRef
IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...
- Haskell语言学习笔记(39)Category
Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...
- Haskell语言学习笔记(84)Concurrent
Control.Concurrent Prelude> import Control.Concurrent Prelude Control.Concurrent> Control.Conc ...
- Haskell语言学习笔记(72)Free Monad
安装 free 包 $ cabal install free Installed free-5.0.2 Free Monad data Free f a = Pure a | Free (f (Fre ...
- Haskell语言学习笔记(49)ByteString Text
Data.ByteString String 是 [Char] 的同义词,在使用上存在List的惰性所带来的性能问题. 在处理大型二进制文件时,可以使用 ByteString 来代替 String. ...
- Haskell语言学习笔记(44)Lens(2)
自定义 Lens 和 Isos -- Some of the examples in this chapter require a few GHC extensions: -- TemplateHas ...
随机推荐
- uml 知识点
Unified Modeling Language (UML)又称统一建模语言或标准建模语言
- markdown 知识点
符号 说明 作用 ___ 三个下划线 一条直线 * 或_ 1个星号 或 1个下划线 文字斜体 ** 或__ 2个星号 或 2个下划线 文字加粗 全角2个空格 缩进2个汉字 竖线之间加3个间隔符放在第二 ...
- Java]运算符优先级
https://blog.csdn.net/xiaoli_feng/article/details/4567184
- PHP中的11个魔术方法总结:__construct,、__destruct、__call等
PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep ...
- bzoj1072排列
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1072 好像是这方面的裸题. 整除k 要想转移需要记录下 达到模k所有余数 的方案数. 为了生 ...
- hadoop最新版本介绍之dkhadoop版本选择
Hadoop对于从事互联网工作的朋友来说已经非常熟悉了,相信在我们身边有很多人正在转行从事hadoop开发的工作,理所当然也会有很多hadoop入门新手.Hadoop开发太过底层,技术难度远比我们想象 ...
- VBox修改uuid
1.使用VBoxManage命令时,需要先在命令行中切换到VirtualBox的安装目录下 2.修改vdi的uuid:VBoxManage internalcommands sethduuid D: ...
- js区分大小写
JavaScript 区分大小写 区分大小写 JavaScript 语言是区分大小写的,不管是命名变量还是使用关键字的时候. 如前面 alert弹出提示框 的例子,如果将 alert 命令改为 ALE ...
- word2vec剖析,资料整理备存
声明:word2vec剖析,资料整理备存,以下资料均为转载,膜拜大神,仅作学术交流之用. word2vec是google最新发布的深度学习工具,它利用神经网络将单词映射到低维连续实数空间,又称为单词嵌 ...
- (转!)大话websocket
邪正看眼鼻,真假看嘴唇,功名看气概,富贵看精神. ---曾国藩<冰鉴> 转自https://www.cnblogs.com/fuqiang88/p/5956363.html 原文http: ...