Let's examine a pointfree way to write these applicative calls. Since we know map is equal to of/ap, we can write generic functions that will ap as many times as we specify:

  1. const liftA2 = curry((g, f1, f2) => f1.map(g).ap(f2));
  2.  
  3. const liftA3 = curry((g, f1, f2, f3) => f1.map(g).ap(f2).ap(f3));
  4.  
  5. // liftA4, etc

Let's see the previous examples written this way:

  1. const profile = name => email => `${name}__${email}`;
  2. const safeProfile = liftA2(profile);
  3. const res1 = safeProfile(prop('name', user), prop('email', user)); // John Doe__blurp_blurp
  1. liftA2(add, Maybe.of(), Maybe.of());
  2. // Maybe(5)
  3.  
  4. liftA2(renderPage, Http.get('/destinations'), Http.get('/events'));
  5. // Task('<div>some page with dest and events</div>')
  6.  
  7. liftA3(signIn, getVal('#email'), getVal('#password'), IO.of(false));
  8. // IO({ id: 3, email: 'gg@allin.com' })

liftAN: Lift a curry function into a Functor context, which will be define later;

  1. liftA2(add, Maybe.of(2), Maybe.of(3)); Maybe will be the Functor context for 'add' function which has been lifted

Laws:

Identity

  1. // identity
  2. A.of(id).ap(v) === v;

For example:

  1. const v = Identity.of('Pillow Pets');
  2. Identity.of(id).ap(v) === v;

Homomorphism

  1. // homomorphism
  2. A.of(f).ap(A.of(x)) === A.of(f(x));

homomorphism is just a structure preserving map. In fact, a functor is just a homomorphism between categories as it preserves the original category's structure under the mapping.

A quick example:

  1. Either.of(toUpperCase).ap(Either.of('oreos')) === Either.of(toUpperCase('oreos'));

Interchange

The interchange law states that it doesn't matter if we choose to lift our function into the left or right side of ap.

  1. // interchange
  2. v.ap(A.of(x)) === A.of(f => f(x)).ap(v);

Here is an example:

  1. const v = Task.of(reverse);
  2. const x = 'Sparklehorse';
  3.  
  4. v.ap(Task.of(x)) === Task.of(f => f(x)).ap(v);

Composition

  1. // composition
  2. A.of(compose).ap(u).ap(v).ap(w) === u.ap(v.ap(w));
  1. const u = IO.of(toUpperCase);
  2. const v = IO.of(concat('& beyond'));
  3. const w = IO.of('blood bath ');
  4.  
  5. IO.of(compose).ap(u).ap(v).ap(w) === u.ap(v.ap(w));

Examples:

  1. const safeAdd = curry((a, b) => Maybe.of(add).ap(a).ap(b));
  2. const safeAdd = liftA2(add);
  3.  
  4. const localStorage = {
  5. player1: { id:, name: 'Albert' },
  6. player2: { id:, name: 'Theresa' },
  7. };
  8.  
  9. // getFromCache :: String -> IO User
  10. const getFromCache = x => new IO(() => localStorage[x]);
  11.  
  12. // game :: User -> User -> String
  13. const game = curry((p1, p2) => `${p1.name} vs ${p2.name}`);
  14. // startGame :: IO String
  15. const startGame = liftA2(game, getFromCache('player1'), getFromCache('player2'));

[Functional Programming] Working with two functors(Applicative Functors)-- Part2 --liftAN的更多相关文章

  1. [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 ...

  2. UCF Local Programming Contest 2016 J题(二分+bfs)

    题目链接如下: https://nanti.jisuanke.com/t/43321 思路: 显然我们要采用二分的方法来寻找答案,给定一个高度如果能确定在这个高度时是否可以安全到达终点,那我们就可以很 ...

  3. Programming | 中/ 英文词频统计(MATLAB实现)

    一.英文词频统计 英文词频统计很简单,只需借助split断句,再统计即可. 完整MATLAB代码: function wordcount %思路:中文词频统计涉及到对"词语"的判断 ...

  4. Coursera Algorithms Programming Assignment 4: 8 Puzzle (100分)

    题目原文:http://coursera.cs.princeton.edu/algs4/assignments/8puzzle.html 题目要求:设计一个程序解决8 puzzle问题以及该问题的推广 ...

  5. Coursera Algorithms Programming Assignment 3: Pattern Recognition (100分)

    题目原文详见http://coursera.cs.princeton.edu/algs4/assignments/collinear.html 程序的主要目的是寻找n个points中的line seg ...

  6. The 2019 Asia Nanchang First Round Online Programming Contest C. Hello 2019(动态dp)

    题意:要找到一个字符串里面存在子序列9102 而不存在8102 输出最小修改次数 思路:对于单次询问 我们可以直接区间dpOn求出最小修改次数 但是对于多次询问 我在大部分题解看到的解释一般是用线段树 ...

  7. Functional Programming 资料收集

    书籍: Functional Programming for Java Developers SICP(Structure and Interpretation of Computer Program ...

  8. Sth about 函数式编程(Functional Programming)

    今天开会提到了函数式编程,针对不同类型的百年城方式,查阅了一部分资料,展示如下: 编程语言一直到近代,从汇编到C到Java,都是站在计算机的角度,考虑CPU的运行模式和运行效率,以求通过设计一个高效的 ...

  9. iOS 开发之函数式编程思想(Functional Programming)

    函数式编程(Functional Programming), 函数式编程强调的函数:1.不依赖外部状态:2.不改变外部状态. 函数式编程可解决线程安全问题,每一个函数都是线程安全的. 时间状态:变量一 ...

随机推荐

  1. 洛谷P2731 骑马修栅栏 [欧拉回路]

    题目传送门 骑马修栅栏 题目背景 Farmer John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. 题目描述 John是一个与其他农民一样懒的人.他讨厌骑马,因此从来不两次经 ...

  2. 【2-SAT】The Ministers’ Major Mess UVALive – 4452

    题目链接:https://cn.vjudge.net/contest/209474#problem/C 题目大意: 一共有m个提案,n个政客,每个政客都会对一些提案(最多四个)提出自己的意见——通过或 ...

  3. jQuery before 和 after

    A.after(B) ==== B.insertAfter(A) B 放在 A 的后面A.before(B) ==== B.insertBefore(A) B 放在 A 的前面 A.append(B) ...

  4. POJ2955【区间DP】

    题目链接[http://poj.org/problem?id=2955] 题意:[].()的匹配问题,问一个[]()串中匹配的字符数,匹配方式为[X],(X),X为一个串,问一个长度为N(N<= ...

  5. 【BZOJ 1853】 1853: [Scoi2010]幸运数字 (容斥原理)

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 2472  Solved: 911 Description 在中国 ...

  6. WebLogic Server

    前几天,看了几集J2ee , 给我的感觉就是,看不懂!! 一点也不懂! 那怎么办呢? 听老师的,不管懂不懂,先看看再说.接下来,就开始了J2ee "艰苦"的历程.在J2ee中,经常 ...

  7. Pollard-rho算法:模板

    #include<algorithm> #include<cstdio> #include<cstdlib> #define N 5500 using namesp ...

  8. bzoj 1458: 士兵占领 -- 最大流

    1458: 士兵占领 Time Limit: 10 Sec  Memory Limit: 64 MB Description 有一个M * N的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵 ...

  9. bzoj 3669: [Noi2014]魔法森林 -- 动点spfa

    3669: [Noi2014]魔法森林 Time Limit: 30 Sec  Memory Limit: 512 MB 动点spfa Description 为了得到书法大家的真传,小E同学下定决心 ...

  10. 不按装mysql情况下,php安装pdo_mysql

    安装pdo时遇到 --with-pdo-mysql  这个要指向mysql安装目录:可是我这台机器不安装mysql; 解决方法: [root@localhost app]#  yum install ...