[Compose] 15. Applicative Functors for multiple arguments
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的更多相关文章
- [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 ...
- [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 ...
- [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, ...
- redux源码阅读之compose,applyMiddleware
我的观点是,看别人的源码,不追求一定要能原样造轮子,单纯就是学习知识,对于程序员的提高就足够了.在阅读redux的compose源码之前,我们先学一些前置的知识. redux源码阅读之compose, ...
- Redux源码分析之compose
Redux源码分析之基本概念 Redux源码分析之createStore Redux源码分析之bindActionCreators Redux源码分析之combineReducers Redux源码分 ...
- javac之Inferring Type Arguments Based on Actual Arguments
We use the following notational conventions in this section: Type expressions are represented using ...
- 小白日记15:kali渗透测试之弱点扫描-漏扫三招、漏洞管理、CVE、CVSS、NVD
发现漏洞 弱点发现方法: 1.基于端口服务扫描结果版本信息,比对其是否为最新版本,若不是则去其 官网查看其补丁列表,然后去逐个尝试,但是此法弊端很大,因为各种端口应用比较多,造成耗时大. 2.搜索已公 ...
- [转载] google mock cookbook
原文: https://code.google.com/p/googlemock/wiki/CookBook Creating Mock Classes Mocking Private or Prot ...
- 译:Spring框架参考文档之IoC容器(未完成)
6. IoC容器 6.1 Spring IoC容器和bean介绍 这一章节介绍了Spring框架的控制反转(IoC)实现的原理.IoC也被称作依赖注入(DI).It is a process wher ...
随机推荐
- 深入理解Java引用类型
深入理解Java引用类型 在Java中类型可分为两大类:值类型与引用类型.值类型就是基本数据类型(如int ,double 等),而引用类型,是指除了基本的变量类型之外的所有类型(如通过 class ...
- JAVAEE学习——hibernate02:实体规则、对象状态、缓存、事务、批量查询和实现客户列表显示
一.hibernate中的实体规则 实体类创建的注意事项 1.持久化类提供无参数构造 2.成员变量私有,提供共有get/set方法访问.需提供属性 3.持久化类中的属性,应尽量使用包装类型 4.持久化 ...
- Linux中用户与组相关配置文件(整理)
用户与组信息存放位置 说明 注释 /etc/passwd 存放用户基本信息 记录了每个用户的一些基本属性,并对所有用户可读,每一行记录对应一个用户,属性之间通过冒号分隔. 每一个行6个冒号,7个属性. ...
- U2随笔
Html 结构化 CSS 样式 JavaScript 行为交互 1.JavaScript基础 2.JavaScript操作BOM对象 3.JavaScript操作DOM对象***** 4.JavaSc ...
- 批量 修改 android eclipse 项目名
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 最外层的 文件夹名字.
- Codeforces 835 F. Roads in the Kingdom
\(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...
- bzoj 2693
收获: 1.积性函数的积也是积性函数,基本的积性函数:常数函数,正比例函数,欧拉函数,Mobius函数,积性函数一般都知道表达式,所以一般都可以在线性筛时搞定. 2.遇到整除求和时,这个东西就已经是最 ...
- April Fools Day Contest 2016 C. Without Text 信号与系统
C. Without Text 题目连接: http://www.codeforces.com/contest/656/problem/C Description You can preview th ...
- LT1946A-- Transformerless dc/dc converter produces bipolar outputs
Dual-polarity supply provides ±12V from one IC VC (Pin 1): Error Amplifier Output Pin. Tie external ...
- Registering DLL and ActiveX controls from code
http://delphi.about.com/od/windowsshellapi/l/aa040803a.htm How to register (and unregister) OLE cont ...