[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解析器会在程序执行时率先读取函数 ...
随机推荐
- 0522 json
一.概念 json依赖于js和xml,是一种数据交换格式,json对比xml的生成和处理要更加方便.因此在许多领域,json正逐步取代xml的使用. 二.使用 1.在JS当中 json在javascr ...
- windows php文件下载地址
http://windows.php.net/downloads/releases/archives/
- Java多线程技术-wait/notify/join
wait/notify的作用 wait()方法的作用是使当前执行代码的线程进行等待,wait()是Object类的方法,用来将当前线程置入预执行队列中,并且在wait()所在的代码处停止执行,直到接到 ...
- UILabel垂直方向显示(上下的顺序显示)。
NSString* text = @"一"; NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFont ...
- POJ 1985 求树的直径 两边搜OR DP
Cow Marathon Description After hearing about the epidemic of obesity in the USA, Farmer John wants h ...
- https 结合使用 对称加密和非对称加密
(一)对称加密(Symmetric Cryptography) ---共享密钥加密 对称加密是最快速.最简单的一种加密方式,加密(encryption)与解密(decryption)用的是同样的密钥( ...
- Multipart/form-data POST文件上传
简单的HTTP POST 大家通过HTTP向服务器发送POST请求提交数据,都是通过form表单提交的,代码如下: <form method="post"action=&qu ...
- (转)Arcgis for js加载天地图
http://blog.csdn.net/gisshixisheng/article/details/44494715 综述:本节讲述的是用Arcgis for js加载天地图的切片资源. 天地图的切 ...
- 备份-泛函编程(23)-泛函数据类型-Monad
泛函编程(23)-泛函数据类型-Monad http://www.cnblogs.com/tiger-xc/p/4461807.html https://blog.csdn.net/samsai100 ...
- String类的特点和使用步骤
概述 java.lang.String 类代表字符串.Java程序中所有的字符串文字(例如 "abc" )都可以被看作是实现此类的实例 类 String 中包括用于检查各个字符串的 ...