当前位置:网站首页>万物皆可柯里化的 Ramda.js
万物皆可柯里化的 Ramda.js
2022-08-09 12:48:00 【华为云】
我们前段时间写过好几篇关于 RxJS 的文章,RxJS api 操作符理解起来确实比较复杂,RxJS 是函数式编程中的 lodash 库,它消除了“时序”而带来的困扰,它核心思想是:函数式 + 响应式。
本篇, 要讲的不是 RxJS,而是另外一个函数式编程库 Ramda.js ,它同样也可以与 loadsh 对比理解,不过它的设计思路又不同了,它最大的特点是:所有函数都可以柯里化传参!以此来践行函数式编程思想。
往下看,后面我们就能明白:Ramda 所有 Api 都能柯里化的意义所在。
Function first,Data last
在 lodash 中,我们是这样写的,
var square = n => n * n;_.map([4, 8], square)
参数在前,执行函数在后。
而在 Ramda 中,强调:函数在前,参数在后。
这样做有什么好处呢?
就是为了更好实现:柯里化。柯里化只需要参数一个一个的在后追加
var R = require('ramda');R.map(square, [4, 8])// 等同于var R = require('ramda');R.map(square)([4, 8])
再举个栗子:
var R = require('ramda');const odd = x => x%2 === 1const data = [3, 5, 6];R.filter(odd, data); // [3, 5]// 等同于R.filter(odd)(data); // [3, 5]// 也可以延迟调用const filter1 = R.filter(odd);// filter1 等待参数的传入// 后续再传入 dataconst filter2 = filter1(data)
如果不借用 Ramda.js , 需要自行实现柯里化,就会显得麻烦:
const _curry = f => a => b => f(a, b)const odd = x => x%2 === 1const _filter = _curry( (fn, arr) => arr.filter(fn) );_filter(odd)([3,5,6]) // [3, 5]
Ramda 非常强调:R.api(fn, data)
这样的范式;
API
来看看 Ramda 有哪些神奇的、好用的、常用的 API~
- map
map 让每个成员依次执行通过某个函数;
const double = x => x * 2;R.map(double, [1, 2, 3]); //=> [2, 4, 6]R.map(double, {x: 1, y: 2, z: 3}); //=> {x: 2, y: 4, z: 6}
- filter
用于过滤;
const isEven = n => n % 2 === 0;R.filter(isEven, [1, 2, 3, 4]); //=> [2, 4]R.filter(isEven, {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, d: 4}
- add
求和;
R.add(2, 3); //=> 5R.add(7)(10); //=> 17
- multiply
求积;
R.multiply(2)(5) // 10
- compose
函数组合,从右到左;
R.compose(Math.abs, R.add(1), R.multiply(2))(-4)// |-4*2 + 1|,等于 7
- pipe
函数组合,从左到右;
var negative = x => -1 * x;var increaseOne = x => x + 1;var f = R.pipe(Math.pow, negative, increaseOne)(3,4);// -(3^4) + 1 ,等于 -80
- curry
将多个参数转换为单个参数
const addFourNumbers = (a, b, c, d) => a + b + c + d;const curriedAddFourNumbers = R.curry(addFourNumbers);curriedAddFourNumbers(1, 2)(3)(4)
Ramda 还有其它丰富的 api,也可以结合 compose/pipe 自定义特定功能函数,用这些方法来简化程序,让代码变成函数式风格;
以上的例子都可在 https://jsrun.net/DTNKp/edit 可以在线运行测试;
更多 Ramda api 可见文档:https://ramda.cn/
OK,以上便是本篇分享。点赞关注评论,为好文助力
我是掘金安东尼 100 万阅读量人气前端技术博主 INFP 写作人格坚持 1000 日更文 关注我,陪你一起度过漫长编程岁月
边栏推荐
- Yocto 可以下载的第三方库
- FFmpeg多媒体文件处理(ffmpeg操作目录及list的实现)
- Unicom network management protocol block diagram
- Flutter Getting Started and Advanced Tour (1) - Getting to Know Flutter
- 正则引擎的几种分类
- [FPGA Tutorial Case 48] Image Case 8 - Realization of Converting RGB Image to HSV Image Based on FPGA, Assisted Verification by MATLAB
- 5G China unicom repeater network management protocol real-time requirements
- 面试题精选:神奇的斐波那契数列
- RTSP协议的实现
- ansible-cmdb friendly display ansible collects host information
猜你喜欢
GIN中GET POST PUT DELETE请求
新起之秀 DPU,正在掀起数据中心变革!
注:检测到当前使用的ADB不是HBuilder内置或自定义ADB:PID为:9544进程名称为:adb.exe 路径为:c:\users\administrator\appdata\local\and
Redis源码剖析之字典(dict)
ViewPager fragments of nested data blank page abnormal problem analysis
novel research
Periodic sharing of Alibaba Da Tao system model governance
kustomize entry example and basic syntax instructions
保存Simulink仿真模型为图片或者PDF的方法
5G 联通网管设计思路
随机推荐
某高校的R语言数据分析期末作业
阿里大淘系模型治理阶段性分享
Data Mining-06
Flutter Getting Started and Advanced Tour (8) Button Widget
Flutter入门进阶之旅(八)Button Widget
Simple encapsulation of glide tool class
如何修改data work上jdbc驱动的版本
NFS 特别注意权限的问题
leetcode 20. Valid Parentheses 有效的括号(中等)
5G China unicom 直放站 网管协议 实时性要求
glibc 内存管理模型 释放 C库内存缓存
read stream special attention
ABP中的数据过滤器 (转载非原创)
FPGA-近日工作总结
30行代码实现微信朋友圈自动点赞
Dry+Bean+Dataset R语言数据分析,报告英文
Flutter入门进阶之旅(一)-初识Flutter
Do you know the difference between comments, keywords, and identifiers?
陈强教授《机器学习及R应用》课程 第十七章作业
注:检测到当前使用的ADB不是HBuilder内置或自定义ADB:PID为:9544进程名称为:adb.exe 路径为:c:\users\administrator\appdata\local\and