[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: '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.__'.

posted @   Zhentiw  阅读(379)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2015-09-02 [React] React Fundamentals: with-addons - ReactLink
2015-09-02 [JavaScript] Array.prototype.reduce in JavaScript by example
2015-09-02 [CSS] @keyframes
点击右上角即可分享
微信分享提示