[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: ‘[email protected]‘,
      age: 42
    }
  },
  {
    title: ‘If You Please‘,
    url: ‘http://www.geocities.com/milq‘,
    author: {
      name: ‘Caspar Milquetoast‘,
      email: ‘[email protected]‘,
      age: 34
    }
  }
];
const ages = R.compose(
  R.map(get(‘age‘)),
  R.map(get(‘author‘))
);

//ORconst 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.__‘.

时间: 2024-10-09 03:02:19

[Ramda] Compose and Curry的相关文章

快速写curry函数和compose函数

我一直都把编程作为一项极其富有创造性和乐趣的工作,其意义就在于我们可以接触各种迷人而富有远见的编程思想,站在巨人的肩膀上眺望未来. 作为一名懒癌晚期编程工作者,任何可以让我少写代码的编程思想对我来说都是一种鸦片,代码本身就应当是简洁而美的,一个人写出来的代码,就是他的整个思想世界. 作为一名菜鸟,最近学习到函数式编程,立即就被这种声明示的编程模式吸引,实在没有更直白的编程方式了,这就是一个小孩或者天才应当使用的编程模式,像我这种半只脚步入中年的程序员,只能喟然嗟叹. 函数式编程应当有三个基石:纯

JS函数式编程【译】4.2 函数组合

?? Functional Programming in Javascript 主目录第四章 在Javascript中实现函数式编程的技术 函数组合 终于,我们到了函数组合. 在函数式编程中,我们希望一切都是函数,尤其希望是一元函数,如果可能的话.如果可以把所有的函数转换为一元函数, 将发生神奇的事情. 一元函数是只接受单个输入的函数.函数如果有多个输入就是多元的,不过我们一般把接受两个输入的叫二元函数, 把接受三个输入的叫三元函数. 有的函数接受的输入的数量并不确定,我们称它为可变的. 操作函

函数式编程简介-附入门方法

WHAT? 什么是函数式编程? 函数式编程是一种编程范式. 编程范式又是什么? 编程范式是一种解决问题的思路. 我们熟悉的命令式编程把程序看作一系列改变状态的指令:而函数式编程把程序看作一系列数学函数映射的组合. 编程范式和编程语言无关,任何编程语言都可以按照函数式的思维来组织代码. i++; // 命令式 关心指令步骤 [i].map(x => x + 1); // 函数式 关心映射关系 WHY? 函数式有什么好处? 易写易读 聚焦重要逻辑,摆脱例如循环之类的底层工作 易复用 面向对象可复用的

[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 obj[prop]; }) var people = [ {name: "Wan"}, {name: "Zhentian"} ]; var res = R.compose( get('name'), log, R.head )(people); console.log

[Ramda] Basic Curry with Ramda

var _ = R; /***************************************** C U R R Y I N G E X A M P L E ******************************************/ // We've got a nice multiply function. // It takes two arguments. console.log( _.multiply(3, 4) ); // But it has been secr

[Ramda] Eliminate Function Arguments (Point-Free Style) with Ramda's Converge

When doing comparisons inside of functions, you end of relying heavily on the argument passed into the function. Ramda's converge allows you to do comparisons in a Point-Free style allowing you more flexibility with composing and constructing functio

[Ramda] Convert Object Methods into Composable Functions with Ramda

In this lesson, we'll look at how we can use Ramda's invoker and constructNfunctions to take methods of an object and turn them into reusable utility functions that are curried and accept their object as the last argument. We'll convert a dot-chained

[Transducer + Ramda] Write highly performance / functional code by using transducer-js and ramda.js libs

Tansducer-js lib A high performance Transducers implementation for JavaScript. Transducers are composable algorithmic transformations. They are independent from the context of their input and output sources and specify only the essence of the transfo

[Javascript Crocks] Compose Functions for Reusability with the Maybe Type

We can dot-chain our way to great success with an instance of Maybe, but there are probably cases where you need to apply the same series of transformations against different Maybes. In this lesson, we’ll look at some point-free versions of some comm