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差别不大,正文如下 我们在编程中必然需要用到一些变量存储数据,供今后其他地方调用.而函数式编程有一个要领就是最好不要依赖外部变量(当然允许通过参数 ...
-
[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 ...
-
[Ramda] Curry and Uncurry Functions with Ramda
Most of the functions offered by the ramda library are curried by default. Functions you've created ...
随机推荐
-
Java多线程synchronized同步
非线程安全问题 “非线程安全”问题存在于“实例变量”中,如果是方法内部的私有变量,则不存在“非线程问题”.也即是说,方法中的变量永远是线程安全的. 如果多个线程共同访问1个对象中的实例变量,则可能线程 ...
-
C#如何在DataGridViewCell中自定义脚本编辑器
上一篇博文探讨了如何自定义DataGridViewColumn实现一个TreeViewColumn来在DataGridView控件中显示TreeView控件,其实我们还可以继续发挥想象,自定义其他的列 ...
-
JDBC连接MySQL数据库的方法和实例
import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java. ...
-
window.showModalDialog的传值和返回值
window.showModalDialog(URL,dialogArgments,features) 打开一个新窗口 URL为要将打开的网页地址. dialogArgments为设定好传递给新视窗网 ...
-
iReport —— 使用 JavaBean 作为数据源
在制作报表时,想直接使用Java代码提供数据. 网上找了一些文章,很多都是用Servlet做的.我不是想通过浏览器来观察它的输出.我想使用iReport的动态连接直接预览. 结合一些资料,加上自己的摸 ...
-
一次 php nusoap 调试过程
今天跟同事调用一个数据api ,用soap方式调用.本以为很简单的事情,却弄到了晚上. 因为有过调试经验,直接按照以往的过程直接部署,结果是错误. 1. 以为是调用方式错了,问了一下对接的同事,没问题 ...
-
cocos2d-x lua脚本开发 1
自从开始关注OpenResty之后,逐渐关注Lua语言,发现这个语言真真是容易让人喜爱的语言.偶然间发现了cocos2d-x,还支持lua,所以果断尝试一下. 这里是在cocos2d-x官方网站下载了 ...
-
codeforces#FF DIV2C题DZY Loves Sequences(DP)
题目地址:http://codeforces.com/contest/447/problem/C C. DZY Loves Sequences time limit per test 1 second ...
-
ORACLE数据库之PL/SQL触发器、rownum、动态SQL、数据库之视图与索引
WHEN子句说明触发约束条件.Condition为一个逻辑表达时,其中必须包含相关名称,而不能包含查询语句,也不能调用PL/SQL函数.WHEN子句指定的触发约束条件只能用在BEFORE和AFTER行 ...
-
异常处理:try - except 和 try finally。
异常处理:try-except语句 1) 此处:as reason为可选参数,reason是一个变量. 2) 使用try—except语句时,检测范围内出现错误,不会有红色的报错提 ...