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. C# new用法总结

    有一道这样的题:写出c#中new关键字的三种用法,思前想后挖空心思也只想出了两种用法,回来查了下msdn,还真是有第三种用法: 用于在泛型声明中约束可能用作类型参数的参数的类型,这是在Framewor ...

  2. [Papers]NSE, $\p_3u$, multiplier spaces [Guo-Gala, ANAP, 2013]

    $$\bex \p_3\bbu\in L^\frac{2}{1-r}(0,T;\dot X_r(\bbR^3)),\quad 0\leq r\leq 1. \eex$$

  3. 1048 图的宽度优先遍历序列 c语言

    描述 图(graph)是数据结构 G=(V,E),其中V是G中结点的有限非空集合,结点的偶对称为边(edge):E是G中边的有限集合.设V={0,1,2,……,n-1},图中的结点又称为顶点(vert ...

  4. Metaspace 之二--Java 8的元空间(metaspace)、metaspace监控方法

    很多开发者都在其系统中见过“java.lang.OutOfMemoryError: PermGen space”这一问题.这往往是由类加载器相关的内存泄漏以及新类加载器的创建导致的,通常出现于代码热部 ...

  5. JAVA和C/C++之间的相互调用。

    在一些Android应用的开发中,需要通过JNI和 Android NDK工具实现JAVA和C/C++之间的相互调用. Java Native Interface (JNI)标准是java平台的一部分 ...

  6. HDU-1584 蜘蛛牌(dfs)

    可以多看看. 蜘蛛牌 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  7. XCode修改工程名注意

    以下文字转载过来,在使用的过程中遇到几个问题 1.需要在 Build phases 里面,检查下 Link Binary With Libraries 以及Compline Sources 2.Bul ...

  8. 基于MFC和opencv的FFT

    在网上折腾了一阵子,终于把这个程序写好了,程序是基于MFC的,图像显示的部分和获取图像的像素点是用到了opencv的一些函数,不过FFT算法没有用opencv的(呵呵,老师不让),网上的二维的FFT程 ...

  9. UNIX系统文件

    密码文件 密码文件又称用户数据库,一般为/etc/passwd,对应的结构为struct passwd,该文件内容大体如下: 描述 passwd字段 用户名 char* pw_name 加密密码 ch ...

  10. bzoj 2186 [Sdoi2008]沙拉公主的困惑(欧拉函数,逆元)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2186 [题意] 若干个询问,求1..n!中与m!互质的个数. [思路] 首先有gcd( ...