[Ramda] Compose and Curry
Curry:
The idea of Curry is to spreate the data from the function. Using Curry to define the function logic and later pass the data into the function logic.
Example1:
- const get = R.curry(function(prop, obj){
- return obj[prop];
- });
- const obj1 = {
- foo: 'bar'
- }
- console.log(get('foo')); //function (t){return n.apply(this,arguments)}
- console.log(get('foo')(obj1)); //bar
The function 'get' just care about get the value from the object, doesn't care about what data it deal with. Make it more reuseable.
Example 2:
- const ary1 = [
- {
- name: 'foo'
- },
- {
- name: 'bar'
- }
- ];
- const names = R.map(get('name'));
- console.log(names(ary1)); //["foo", "bar"]
Combine different functions to make it more prowerful. Here we create a function called 'names', later you pass in the data, it will return back the names for you.
So far, you should think what 'curry' does is just define the function logic. For example, we wirte a 'Calculate average value' function.
We can define the logic first: "1. We need sum value, 2. we need the size value, 3. Sum divide size":
- const nums = [15, 16, 5];
- const avgLogic = R.curry(function(divide, sum, size, nums){
- return divide( sum(nums), size(nums) );
- })
- const avgCal = avgLogic(R.divide, R.sum, R.length);
- const avgNum = avgCal(nums);
- console.log(avgNum);
Compose:
The idea of compose is to chain function together. R.compose run from 'rgiht' --> 'left'.
So the previous result will be used for the next function. Those function combine together to make a more prowerful and reuseable function.
- const articles = [
- {
- title: 'Everything Sucks',
- url: 'http://do.wn/sucks.html',
- author: {
- name: 'Debbie Downer',
- email: 'debbie@do.wn',
- age: 42
- }
- },
- {
- title: 'If You Please',
- url: 'http://www.geocities.com/milq',
- author: {
- name: 'Caspar Milquetoast',
- email: 'hello@me.com',
- age: 34
- }
- }
- ];
- const ages = R.compose(
- R.map(get('age')),
- R.map(get('author'))
- );
- //OR
const ages = R.map(
R.compose(
get('age'),
get('author')
)
);- console.log(ages(articles)); // [42, 34]
Exmaple 2:
- const words = "Hello world, what a great day!";
- const lengths = R.compose(
- R.map(R.length),
- R.split(' ')
- );
- console.log(lengths(words)); //[5, 6, 4, 1, 5, 4]
Currently All the example2 list above using curry one way or another. The pattern is always like:
- var foo = bar('baz');
- var res = foo(data); // ...
- //or
- var res = bar('baz')(data);
The 'data' always come at the end, but not necessary it should be like this every time.
R.__ : the placeholder for the curry data
- const lenSubTow = R.compose(
- R.map(R.subtract(R.__, 2)),
- R.map(R.length),
- R.split(' ')
- );
- console.log(lenSubTow(words)); //[3, 4, 2, -1, 3, 2]
SO the result comes from 'R.map(R.length)' will be passed to 'R.__'.
[Ramda] Compose and Curry的更多相关文章
- [Ramda] Compose lenses
We can compose lenses to get value: const addrs = [{street: '99 Walnut Dr.', zip: '04821'}, {street: ...
- [Ramda] Pick and Omit Properties from Objects Using Ramda
Sometimes you just need a subset of an object. In this lesson, we'll cover how you can accomplish th ...
- javascript函数式编程(一)
一.引言 javascript函数式编程在最近两年来频繁的出现在大众的视野,越来越多的框架(react,angular,vue等)标榜自己使用了函数式编程的特性,好像一旦跟函数式编程沾边,就很高大上一 ...
- js函数式编程(二)-柯里化
这节开始讲的例子都使用简单的TS来写,尽量做到和es6差别不大,正文如下 我们在编程中必然需要用到一些变量存储数据,供今后其他地方调用.而函数式编程有一个要领就是最好不要依赖外部变量(当然允许通过参数 ...
- JavaScript函数式编程之函子
函子(Functor) 函子是一个特殊的容器,通过一个普通对象来实现,该对象具有map方法,map方法可以运行一个函数对值进行处理(变形关系),容器包含值和值变形关系(这个变形关系就是函数).函数式编 ...
- [Ramda] Curry, Compose and Pipe examples
const curry = R.curry((fns, ary) => R.ap(fns, ary)); ), R.add()]); ,,]); console.log(res); //[2, ...
- [Ramda] Simple log function for debugging Compose function
const log = function(x){ console.log(x); return x; } const get = R.curry(function(prop, obj){ return ...
- js函数式编程curry与compose实现
//自行实现以下curry函数和compose //curry function curry(fn) { return function aa (...arg) { if (arg.length &g ...
- [Ramda] Simple log function for debugging Compose function / Using R.tap for logging
const log = function(x){ console.log(x); return x; } const get = R.curry(function(prop, obj){ return ...
随机推荐
- Chrome 实用调试技巧
Chrome 实用调试技巧 2016-07-23 如今Chrome浏览器无疑是最受前端青睐的工具,原因除了界面简洁.大量的应用插件,良好的代码规范支持.强大的V8解释器之外,还因为Chrome开发者工 ...
- POJ 1573 Robot Motion
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12978 Accepted: 6290 Des ...
- (原创)LAMP教程5-配置VirtualBox虚拟机中centos6.4的网卡
(原创)LAMP教程5-配置VirtualBox虚拟机中centos6.4的网卡 是的,今天我们要讲的是如何配置VirtualBox虚拟机中centos6.4的网卡,毕竟我们是要做网站开发的,没有网络 ...
- IOS 时间 日历 处理集合
1.获得当前时间 从1970开始的秒数 NSTimeInterval time = [[NSDate date[ timeIntervalSince1970]]; NSString * str = [ ...
- Selenium IDE验证点
Selenium IDE验证点 我们还开发了测试用例需要检查一个Web页面的属性.这需要维护和验证命令.有两种方法可以验证点到任何脚本 插入记录模式中的任何验证点单击“右键”元素,并选择“Show a ...
- 【转】发布python的包至pypi服务器
[原文链接]http://yejinxin.github.io/distribute-python-packages-to-pypi-server/ 使用pip或easy_install可以管理和安装 ...
- flex 图片旋转(解决公转和自转问题)
在Flex中图片的旋转是既有公转和自转的.这样在图片旋转的时候就有一定小麻烦: 为了更好地说明问题,先引入两个概念:“自转”和“公转”.想象一下,地球在绕着太阳公转的同时,它自己也在自转.Flash应 ...
- asp.net MVC 安全性[笔记]
1. 跨站脚本(XSS) 1.1 介绍 1.1.1 被动注入,利用输入html,javascript 等信息伪造链接,图片等使用提交信息,调转页面等 1.1.2 主动注入,黑客主动参与攻击,不会傻等倒 ...
- openstack【Kilo】汇总:包括20英文文档、各个组件新增功能及Kilo版部署
OpenStack Kilo版本发布 20英文文档OpenStack Kilo版本文档汇总:各个操作系统安装部署.配置文档.用户指南等文档 Kilo版部署 openstack[Kilo]入门 [准备篇 ...
- homework-08-作业2
1. 了解Lambda的用法 计算“Hello World!”中 a.字母‘e’的个数 b. 字母‘l’的个数 代码: void calcEL() { char s[100] = "Hell ...