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的更多相关文章

  1. [Ramda] Compose lenses

    We can compose lenses to get value: const addrs = [{street: '99 Walnut Dr.', zip: '04821'}, {street: ...

  2. [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 ...

  3. javascript函数式编程(一)

    一.引言 javascript函数式编程在最近两年来频繁的出现在大众的视野,越来越多的框架(react,angular,vue等)标榜自己使用了函数式编程的特性,好像一旦跟函数式编程沾边,就很高大上一 ...

  4. js函数式编程(二)-柯里化

    这节开始讲的例子都使用简单的TS来写,尽量做到和es6差别不大,正文如下 我们在编程中必然需要用到一些变量存储数据,供今后其他地方调用.而函数式编程有一个要领就是最好不要依赖外部变量(当然允许通过参数 ...

  5. JavaScript函数式编程之函子

    函子(Functor) 函子是一个特殊的容器,通过一个普通对象来实现,该对象具有map方法,map方法可以运行一个函数对值进行处理(变形关系),容器包含值和值变形关系(这个变形关系就是函数).函数式编 ...

  6. [Ramda] Curry, Compose and Pipe examples

    const curry = R.curry((fns, ary) => R.ap(fns, ary)); ), R.add()]); ,,]); console.log(res); //[2, ...

  7. [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 ...

  8. js函数式编程curry与compose实现

    //自行实现以下curry函数和compose //curry function curry(fn) { return function aa (...arg) { if (arg.length &g ...

  9. [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 ...

随机推荐

  1. IOS AsyncSocket

    导入AsyncSocket.h  AsyncSocket.m   AsyncUdpSocket.h   AsyncUdpSocket.m   以及  CFNetWork.framework async ...

  2. <译>Selenium Python Bindings 4 - Locating Eelements

    有各种不同的策略来定位页面中的元素.你可以使用最合适定位方式用于你的用例.Selenium提供了以下方法来定位页面中的元素: find_element_by_id find_element_by_na ...

  3. Bug疑难杂症 - java.lang.NoSuchFieldError: udhLen

    通过友盟后台记录到一个诡异的错误: HUAWEI C8825D 4.0.4 百度.谷歌都未能找到一丝结果更别提解决方案. java.lang.NoSuchFieldError: udhLen at a ...

  4. html 5新特性 --用SVG绘制的微信logo

    一个简单的SVG绘制图片的小案例. 效果图: 代码如下: <style> * { ; ; } body { background-color: #d5d3d4; } .container ...

  5. XNA Game Studio4.0 Programming 随便读,随便记。

    一.精灵和2D图形 1.什么是2D ? 2D可以理解为 two-Dimentionanl  , 2-dimentional 的缩写. 意就是两维的,比如 数学中的 直角坐标系 所能描述的就是一个2D的 ...

  6. [学习笔记] Web设计过程中该做和不该做的

    原文网址: http://www.javascriptstyle.com/the-dos-and-donts-of-web-design -该做的: QR代码QR代码即快速响应代码,这是矩阵条形码的一 ...

  7. Ubuntu中、英文环境设置

    改变ubuntu的中英文显示需要修改文件/etc/default/locale,具体设置过程为: 1.打开/etc/default/locale文件 #sudo vim /etc/default/lo ...

  8. work5

    这一次写的内容是黄金豆小游戏,由于现在偏重写服务器端.对于算法层面其实涉及不多,更多偏于工程上的架构. 总而言之本次作业的服务器核心是用web.py所写,而且为了方便其他用户写客户端,架构非常简单. ...

  9. 【转】使用JavaScriptCore在JS和OC间通信

    http://www.cocoachina.com/ios/20160623/16796.html iOS 开发中,我们时不时的需要加载一些 Web 页面,一些需求使用 Web 页面来实现可以更可控, ...

  10. UVALive 7278 Game of Cards (sg函数)

    Game of Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/G Description Alice and Bob ...