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) <<$>> a <<*>> b (<<*) :: p a b -> p c d -> p a b
a <<* b = bimap const const <<$>> a <<*>> b (<<$>>) :: (a -> b) -> a -> b
(<<$>>) = id

Biapplicative 是个类型类。主要用于在二元数据结构上应用 Applicative。

(,) 是个 Biapplicative

instance Biapplicative (,) where
bipure = (,)
(f, g) <<*>> (a, b) = (f a, g b)

Const 是个 Biapplicative

instance Biapplicative Const where
bipure a _ = Const a
Const f <<*>> Const x = Const (f x)

应用 Biapplicative

Prelude Data.Biapplicative> ((+2),(*3)) <<*>> (3,4)
(5,12)
Prelude Data.Biapplicative Control.Applicative> Const (+2) <<*>> Const 3
Const 5
Prelude Data.Biapplicative> bimap (+) (*) <<$>> (2,3) <<*>> (3,4)
(5,12)
Prelude Data.Biapplicative Control.Applicative> bimap (+) (*) <<$>> Const 2 <<*>> Const 3
Const 5

(<<**>>), biliftA2, biliftA3

(<<**>>) :: Biapplicative p => p a c -> p (a -> b) (c -> d) -> p b d
(<<**>>) = biliftA2 (flip id) (flip id) biliftA2 :: Biapplicative w => (a -> b -> c) -> (d -> e -> f) -> w a d -> w b e -> w c f
biliftA2 f g a b = bimap f g <<$>> a <<*>> b biliftA3 :: Biapplicative w => (a -> b -> c -> d) -> (e -> f -> g -> h) -> w a e -> w b f -> w c g -> w d h
biliftA3 f g a b c = bimap f g <<$>> a <<*>> b <<*>> c
Prelude Data.Biapplicative> (2,3) <<**>> ((2^), (3*))
(4,9)
Prelude Data.Biapplicative> biliftA2 (+) (*) (2,3) (4,5)
(6,15)
Prelude Data.Biapplicative Control.Applicative> biliftA2 (+) (*) (Const 2) (Const 3)
Const 5

Haskell语言学习笔记(60)Biapplicative的更多相关文章

  1. Haskell语言学习笔记(88)语言扩展(1)

    ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...

  2. Haskell语言学习笔记(79)lambda演算

    lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...

  3. Haskell语言学习笔记(69)Yesod

    Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...

  4. Haskell语言学习笔记(20)IORef, STRef

    IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...

  5. Haskell语言学习笔记(39)Category

    Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...

  6. Haskell语言学习笔记(72)Free Monad

    安装 free 包 $ cabal install free Installed free-5.0.2 Free Monad data Free f a = Pure a | Free (f (Fre ...

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

    Data.ByteString String 是 [Char] 的同义词,在使用上存在List的惰性所带来的性能问题. 在处理大型二进制文件时,可以使用 ByteString 来代替 String. ...

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

    自定义 Lens 和 Isos -- Some of the examples in this chapter require a few GHC extensions: -- TemplateHas ...

  9. Haskell语言学习笔记(38)Lens(1)

    Lens Lens是一个接近语言级别的库,使用它可以方便的读取,设置,修改一个大的数据结构中某一部分的值. view, over, set Prelude> :m +Control.Lens P ...

随机推荐

  1. JUC原子类之 原子操作数据类型

    根据修改的数据类型,可以将JUC包中的原子操作类可以分为4类. 基本类型: AtomicInteger, AtomicLong, AtomicBoolean ; 数组类型: AtomicInteger ...

  2. Linux下C/C++代码调用PHP代码(转)

    Linux下C/C++代码可以通过popen系统函数调用PHP代码并通过fgets函数获取PHP代码echo输出的字符串. //main.c char str[1024] = {0}; char *  ...

  3. module.exports用法

    module.exports 对象是由模块系统创建的.在我们自己写模块的时候,需要在模块最后写好模块接口,声明这个模块对外暴漏声明内容,module.exports提供了暴漏接口的方法. 1.返回一个 ...

  4. Spring的IOC原理 通俗解释

    1. IoC理论的背景我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑. 图1:软件系统中耦合的对象 如果我们打开机械 ...

  5. 纯文本文件 numbers.txt, 里面的内容(包括方括号)如下所示

    from collections import OrderedDict import xlwt with open('student.txt','r') as f: number_list = jso ...

  6. MySQL优化技巧之四:mysql数据库开发常见问题及优化[转]

    mysql 数据库是被广泛应用的关系型数据库,其体积小.支持多处理器.开源并免费的特性使其在 Internet 中小型网站中的使用率尤其高.在使用 mysql 的过程中不规范的 SQL 编写.非最优的 ...

  7. H-Index II @python

    Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...

  8. 管理oracle 11g RAC 常用命令

    1).检查集群状态: [grid@rac02 ~]$ crsctl check cluster CRS-4537: Cluster Ready Services is online CRS-4529: ...

  9. laravel 5.3 ——路由(资源,别名)

    laravel的路由定义中,其中route:resoure(),可以直接定义类似restful风格的URL 例如:Route::resource('system/role','System\RoleC ...

  10. spark streaming 概述

    批处理 & 流处理 像这个是批处理 像这样就是流处理 为什么需要流处理--更多场景需要 Spark Core & RDD 本质上是离线运算 Spark Streaming是什么(分布式 ...