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. Android之 广播

    (以下内容是阅读郭霖大神的<第一行代码>后自己总结的) 1.概述 广播是Android的四大组件之一. Android的广播机制十分灵活. 2.发送广播 如上图Android的广播主要分为 ...

  2. shell 文件传 参数

    n cross-platform, lowest-common-denominator sh you use: #!/bin/sh value=`cat config.txt` echo " ...

  3. React Native 系列(八)

    前言 本系列是基于React Native版本号0.44.3写的.我们都知道,一个App不可能只有一个不变的界面,而是通过多个界面间的跳转来呈现不同的内容.那么这篇文章将介绍RN中的导航. 导航 什么 ...

  4. RxJava 机制

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha rxjava 是  以 响应式 编程思想   编程的 java类库

  5. Topcoder Srm 726 Div1 Hard

    Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...

  6. [BZOJ3514]CodeChef MARCH14 GERALD07加强版(LCT+主席树)

    3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 2177  Solved: 834 ...

  7. 快速排序-C

    #include <stdio.h> #include <stdlib.h> #define N 6 int partition(int arr[], int low, int ...

  8. 20162325 金立清 S2 W6 C15

    20162325 2017-2018-2 <程序设计与数据结构>第6周学习总结 教材学习内容概要 队列是先进先出(FIFO)的集合 队列是保存重复编码k值的一种有效结构 实现模拟时常用队列 ...

  9. BFS洪水

    试题描述: 已经连续下了几天雨,却还是没有停的样子.土豪CCY刚从外地赚完1e元回来,知道不久除了自己别墅,其他的地方都将会被洪水淹没. CCY所在的城市可以用一个N*M(N,M<=50)的地图 ...

  10. hdu 1024 Max Sum Plus Plus DP

    Max Sum Plus Plus Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php ...