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. 七中滤波方法测试matlab实现

    http://blog.163.com/xiaheng0804@126/blog/static/1205282120132129471816/ 创建两个混合信号,便于更好测试滤波器效果.同时用七中滤波 ...

  2. codeforces 691F Couple Cover 暴力

    分析:开一个300w的数组,统计,然后nlogn统计每个值在在序对第一个出现有多少种情况 时间复杂度:O(nlogn) n在3e6数量级 #include<cstdio> #include ...

  3. OpenGL超级宝典第5版&&缓冲区

    缓冲区有很多用途:可以保存顶点数据,像素数据,纹理数据,着色器处理的输入,不同着色器阶段的输出. 缓冲区保存在GPU内存中,提供高速有效的访问.   像素缓冲区对象: GLuint pixBuffer ...

  4. [CODEVS2603]公路修建

    题目描述 某国有n个城市,它们互相之间没有公路相通,因此交通十分不便.为解决这一“行路难”的问题,政府决定修建公路.修建公路的任务由各城市共同完成.    修建工程分若干轮完成.在每一轮中,每个城市选 ...

  5. Motan:目录结构

    motan是由maven管理的,在最外层的pom.xml中可以看出这个项目有多个模块组成. <modules> <module>motan-core</module> ...

  6. [HIve - LanguageManual] LateralView

    Lateral View Syntax Description Example Multiple Lateral Views Outer Lateral Views Lateral View Synt ...

  7. WinForm编程时窗体设计器中ComboBox控件大小的设置

    问题描述: 在VS中的窗体设计器中拖放一个ComboBox控件后想调整控件的大小.发现在控件上用鼠标只能拖动宽度(Width)无法拖动(Height). 解决过程: 1.控件无法拖动,就在属性窗口中设 ...

  8. JavaFX 2 Dialogs

    http://edu.makery.ch/blog/2012/10/30/javafx-2-dialogs/ ———————————————————————————————————————————— ...

  9. JavaIO流(02)RandomAccessFile类详解

    RandomAccessFile类 该类主要是对文件内容进行操作,可以随机的读取一个文件中指定位置的数据: 但是如果想实现这样的功能,则每个数据的长度应该保持一致:   构造方法:   接受File类 ...

  10. HDU 1312 http://acm.hdu.edu.cn/showproblem.php?pid=1312

    #include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #de ...