[Javascript Crocks] Apply a function in a Maybe context to Maybe inputs (curry & ap & liftA2)
Functions are first class in JavaScript. This means we can treat a function like any other data. This also means we can end up with a function as the wrapped value in a Maybe. The Maybe type gives us the ability to apply that wrapped function to other wrapped values, keeping both the value and the function in the safe confines of a Maybe.
The whole point is when we have params as safe params:
const safeNum1 = safe(isNumber, );
const safeNum2 = safe(isNumber, );
The function we need to opreate the params is not in a Maybe context:
const add = a => b => a + b;
Now we cannot just call 'add' function to add two Just(n) value together:
add(safeNum1, safeNum2) // doesn't work
Lucky we can also wrap function into Maybe context:
const safeAdd = Maybe.of(add);
We can use 'ap' function to apply context for a function
const crocks = require('crocks')
const { ap, safe, isNumber, Maybe } = crocks;
const safeNum1 = safe(isNumber, );
const safeNum2 = safe(isNumber, );
const add = a => b => a + b;
const res = Maybe.of(add)
.ap(safeNum1)
.ap(safeNum2);
console.log(res) // Just 3
We can improve the code by using 'curry' instead of
const add = a => b => a + b;
to:
const crocks = require('crocks')
const { curry } = crocks;
const add = curry((a, b) => a + b);
We can also using a helper method 'liftA2' to replace calling 'ap' multiy times:
const res = Maybe.of(add)
.ap(safeNum1)
.ap(safeNum2);
to:
const crocks = require('crocks')
const { ap, safe, isNumber, Maybe, curry, liftA2 } = crocks;
const safeNum1 = safe(isNumber, );
const safeNum2 = safe(isNumber, );
const add = curry((a, b) => a + b);
const addTwoSafeNums = liftA2(add);
const res = addTwoSafeNums(safeNum1, safeNum2);
console.log(res) // Just 3
[Javascript Crocks] Apply a function in a Maybe context to Maybe inputs (curry & ap & liftA2)的更多相关文章
- (转)深入浅出 妙用Javascript中apply、call、bind
原文连接 深入浅出 妙用Javascript中apply.call.bind 网上文章虽多,大多复制粘贴,且晦涩难懂,我希望能够通过这篇文章,能够清晰的提升对apply.call.bind的认识,并且 ...
- javascript中apply、call和bind的区别,容量理解,值得转!
a) javascript中apply.call和bind的区别:http://www.cnblogs.com/cosiray/p/4512969.html b) 深入浅出 妙用Javascrip ...
- 【JavaScript】apply和call的区别在哪?
我在一开始看到javascript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示例,总算是看的有点眉目了,在这里我做如下笔记,希望和大家 ...
- 解析JavaScript中apply和call以及bind
函数调用方法 在谈论JavaScript中apply.call和bind这三兄弟之前,我想先说下,函数的调用方式有哪些: 作为函数 作为方法 作为构造函数 通过它们的call()和apply()方法间 ...
- JavaScript之apply()和call()的区别
我 在一开始看到javascript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示 例,总算是看的有点眉目了,在这里我做如下笔记,希望和 ...
- javascript函数apply和call
apply:方法能劫持另外一个对象的方法,继承另外一个对象的属性. Function.apply(obj,args)方法能接收两个参数obj:这个对象将代替Function类里this对象args:这 ...
- JavaScript中apply与call方法
一.定义 apply:应用某一对象的一个方法,用另一个对象替换当前对象. call:调用一个对象的一个方法,以另一个对象替换当前对象. 二.apply //apply function Person( ...
- 理解 JavaScript call()/apply()/bind()
理解 JavaScript this 文章中已经比较全面的分析了 this 在 JavaScript 中的指向问题,用一句话来总结就是:this 的指向一定是在执行时决定的,指向被调用函数的对象.当然 ...
- javascript学习笔记 - 引用类型 Function
五 Function类型 每个函数都时Function类型的实例.函数也是对象. 声明函数: function func_name () {} //javascript解析器会在程序执行时率先读取函数 ...
随机推荐
- Django基础必备三件套: HttpResponse render redirect
1. HttpResponse : 它的作用是内部传入一个字符串参数, 然后发给浏览器 def index(request): return HttpResponse('ok') 2. render ...
- [转]RDLC 动态列
本文转自:http://blog.csdn.net/luochengbang/article/details/9964551 很久没有写博客了,关于动态列,国内很少资料有介绍动态列的,所想写点心得给哥 ...
- layui框架 各种小结
首先项目前端采用的是bootstrap和layui弹窗,验证,表格用的是bootstrapTable layui官方地址:http://www.layui.com/ 文档:http://www.lay ...
- POJ 3253 STL优先队列
题目大意:FJ需要修补牧场的围栏,他需要 N 块长度为 Li 的木头(N planks of woods).开始时,FJ只有一块无限长的木板,因此他需要把无限长的木板锯成 N 块长度 为 Li 的木板 ...
- 题解报告:hdu 4764 Stone(巴什博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4764 Problem Description Tang and Jiang are good frie ...
- 推荐系统:MovivLens20M数据集解析
MovieLens 是历史最悠久的推荐系统.它由美国 Minnesota 大学计算机科学与工程学院的 GroupLens 项目组创办,是一个非商业性质的.以研究为目的的实验性站点.MovieLens ...
- Android读写文件
1.从resource中的raw文件夹中获取文件并读取数据(资源文件只能读不能写) String res = ""; try{ InputStream in = getResour ...
- (转)基于openlayers实现聚类统计展示
http://blog.csdn.net/gisshixisheng/article/details/46137015 概述: 在前面的博文中讲述过基于Arcgis for js如何实现聚类统计展示, ...
- UNIX SOCKET编程简介
1 . Layered Model of Networking Socket 编程的层次模型如下图所示, 最上面是应用层,应用层下面的是 SOCKET API 层,再下面是传输层和网络层 ...
- marquee标签弹幕效果
播放个视频的时候看到很有趣的弹幕,想着前端能不能做个弹幕效果.弹幕是滚动的,所以首先想到了<marquee>标签.但事实上,<marquee>标签不是w3c的标准,只是主流的浏 ...