Working our way backwards from solution to problem, we define an applicative functor, then use it to apply a function of multiple arguments.

For example we have this line of code:

const res = Box(x => x +).ap(Box())// Box(3);

We want to use a funciton 'ap' (apply) on Box. And x will be 2.

To define 'ap' function.

const Box = x =>
({
chain: f => f(x),
ap: other => other.map(x),
map: f => Box(f(x)),
fold: f => f(x),
inspect: () => `Box(${x})`
})

So '

Box(x => x +1).ap(Box(2))

'

Can be translated to:

Box() => Box().map(x => x + );

This can be useful when apply curry function:

const res = Box(x => y => x + y).ap(Box()).ap(Box());
console.log(res.inspect()); //Box(3)

after apply .ap(Box(1)), it becomes to:

Box(y => 1 +y).ap(Box(2))

after apply .ap(Box(2)), it becomes to:

Box( + )

It ends up, we have a function and continue to using 'ap':

const add = x => y => x + y;
const res = Box(add).ap(Box()).ap(Box());

This partten is called click-functor!

The rule is:

F(val).map(fn) === F(fn).ap(F(val))

For example now we have:

const liftA2 = (fn, Fx, Fy) =>
F(fn).ap(Fx).ap(Fy);

The problem is we don't know what 'F' it is here...

So what we can do is transform accorind to the rule we have:

const liftA2 = (fn, Fx, Fy) =>
Fx.map(fn).ap(Fy)

Therefore we don't need to memtion any Functor.

Example:

const res2 = liftA2(add, Box(), Box());
console.log(res2.inspect()); //Box(3)

Applicate Functor is really good to work with Async functor, because async by natural, data arrives different time:

const add = x => y => z=> x + y + z;
const addAsyncNumbers = liftA3(add);
const res = addAsyncNumbers(
Async.of(),
Async((_, res) => {
setTimeout(() => {
console.log('resolve 2');
res()
}, )
}), Async((_, res) => {
setTimeout(() => {
console.log('resolve 3');
res()
}, )
}));
res.fork(e => console.error(e), x => console.log('async', x)) //

[Compose] 15. Applicative Functors for multiple arguments的更多相关文章

  1. [Compose] 17. List comprehensions with Applicative Functors

    We annihilate the need for the ol' nested for loop using Applicatives. For example we have this kind ...

  2. [Functional Programming] Working with two functors(Applicative Functors)-- Part1 --.ap

    What is applicative functor: the ability to apply functors to each other. For example we have tow fu ...

  3. [Functional Programming] Working with two functors(Applicative Functors)-- Part2 --liftAN

    Let's examine a pointfree way to write these applicative calls. Since we know map is equal to of/ap, ...

  4. redux源码阅读之compose,applyMiddleware

    我的观点是,看别人的源码,不追求一定要能原样造轮子,单纯就是学习知识,对于程序员的提高就足够了.在阅读redux的compose源码之前,我们先学一些前置的知识. redux源码阅读之compose, ...

  5. Redux源码分析之compose

    Redux源码分析之基本概念 Redux源码分析之createStore Redux源码分析之bindActionCreators Redux源码分析之combineReducers Redux源码分 ...

  6. javac之Inferring Type Arguments Based on Actual Arguments

    We use the following notational conventions in this section: Type expressions are represented using ...

  7. 小白日记15:kali渗透测试之弱点扫描-漏扫三招、漏洞管理、CVE、CVSS、NVD

    发现漏洞 弱点发现方法: 1.基于端口服务扫描结果版本信息,比对其是否为最新版本,若不是则去其 官网查看其补丁列表,然后去逐个尝试,但是此法弊端很大,因为各种端口应用比较多,造成耗时大. 2.搜索已公 ...

  8. [转载] google mock cookbook

    原文: https://code.google.com/p/googlemock/wiki/CookBook Creating Mock Classes Mocking Private or Prot ...

  9. 译:Spring框架参考文档之IoC容器(未完成)

    6. IoC容器 6.1 Spring IoC容器和bean介绍 这一章节介绍了Spring框架的控制反转(IoC)实现的原理.IoC也被称作依赖注入(DI).It is a process wher ...

随机推荐

  1. BZOJ3262陌上花开(三维偏序问题(CDQ分治+树状数组))+CDQ分治基本思想

    emmmm我能怎么说呢 CDQ分治显然我没法写一篇完整的优秀的博客,因为我自己还不是很明白... 因为这玩意的思想实在是太短了: fateice如是说道: 如果说对于一道题目的离线操作,假设有n个操作 ...

  2. Kail Linux渗透测试教程之在Metasploit中扫描

    Kail Linux渗透测试教程之在Metasploit中扫描 在Metasploit中扫描 在Metasploit中,附带了大量的内置扫描器.使用这些扫描器可以搜索并获得来自一台计算机或一个完整网络 ...

  3. SpringBoot学习(三)

    一.单个 controller 范围的异常处理 package com.xxx.secondboot.web; import org.springframework.web.bind.annotati ...

  4. 解决ubuntu解压windows生成的zip文件时乱码问题

    在windows上压缩的文件,是以系统默认编码中文来压缩文件.由于zip文件中没有声明其编码,所以linux上的unzip一般以默认编码解压,中文文件名会出现乱码. 虽然2005年就有人把这报告为bu ...

  5. pair 对组

    pair 对组 c++ 基础 2016-05-10 19:42 154人阅读 评论(0) 收藏 举报  分类: 头文件的函数精粹(12)  版权声明:本文为博主原创文章,未经博主允许不得转载. 与关联 ...

  6. Django错误大汇总

    1.安装django报错解决方案 找到第一条报错信息: File "c:\users\chenwei\envs\testvir2\lib\site-packages\pip\basecomm ...

  7. Hibernate 基于外键的单项一对一关联映射

    在开发过程中很多时候会用到表与表之间一对一的关联关系,本文简单介绍在Hibernate4中单项一对一的关联映射. 1.设计表结构 2.创建Person对象 3.创建IdCard对象 4.写hbm.xm ...

  8. luogu P1011 车站

    题目描述 火车从始发站(称为第1站)开出,在始发站上车的人数为a,然后到达第2站,在第2站有人上.下车,但上.下车的人数相同,因此在第2站开出时(即在到达第3站之前)车上的人数保持为a人.从第3站起( ...

  9. CF1051D Bicolorings dp

    水题一道 $f[i][j][S]$表示$2 * i$的矩形,有$j$个联通块,某尾状态为$S$ 然后转移就行了... #include <vector> #include <cstd ...

  10. 【计算几何】【二分图判定】Gym - 101485C - Cleaning Pipes

    题意:有n个水井,每个水井发出一些管线(都是线段),然后每条管线上最多只有一个水井.所有从不同的水井发出的管线的相交点都是清洁点(不存在清洁点是大于两条管线点的交点).你需要在某些管线上放出一些机器人 ...