[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 ...
随机推荐
- AE与AO的区别
在ArcGis9.0之前,ArcObject还不是一个独立的产品,一直捆绑在Desktop产品中,只要你购买了desktop产品中的一个,你就可 以使用arcboject开发.从ArcGis9.0开始 ...
- LXD 2.0 系列(二):安装与配置
导读 简单来说,LXD是一个守护进程,为LXC容器的管理提供一组REST API.主要目标是提供一种类虚拟机的用户体验,是一种第三方的容器管理工具.下面呢,我们来介绍LXD 2.0 的安装与配置 安装 ...
- hdu 4720 计算几何简单题
昨天用vim练了一道大水题,今天特地找了道稍难一点的题.不过也不是很难,简单的计算几何而已.练习用vim编码,用gdb调试,结果居然1A了,没调试...囧... 做法很简单,无非就是两种情况:①三个巫 ...
- T-SQL 数据库笔试题
1.说明:创建数据库 Create DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建备份数 ...
- ASP.NET QueryString乱码解决问题
正常的情况下,现在asp.net的网站很多都直接使用UTF8来进行页面编码的,这与Javascript.缺省网站的编码是相同的,但是也有相当一部分采用GB2312. 对于GB2312的网站如果直接用j ...
- MFC特定函数的应用20160720(SystemParametersInfo,GetWindowRect,WriteProfileString,GetSystemMetrics)
1.SystemParametersInfo函数可以获取和设置数量众多的windows系统参数 MFC中可以用 SystemParametersInfo(……) 函数来获取和设置系统信息,如下面例子所 ...
- 基于Hbase数据的Mapreduce程序环境开发
一.实验目标 编写Mapreduce程序,以Hbase表数据为Map输入源,计算结果输出到HDFS或者Hbase表中. 在非CDH5的Hadoop集群环境中,将编写好的Mapreduce程序整个工程打 ...
- Apache Maven 入门
Apache Maven 入门篇 ( 上 ) Apache Maven 入门篇 ( 下 ) ~$mvn archetype:generate -DgroupId=com.mycompany.hello ...
- Crontab设置定时任务
Crontab文件格式 文件格式: minute hour day month weekday username command 格式解析 字段 描述 minute 分,值为0-59 hour 小 ...
- Sql建表语句
create table dbo.[Finance_CityInfo] ([CityId] int identity(1,1) not null , [City] nvarchar(20) not n ...