简体   繁体   English

ramda.js中的函数组成

[英]function composition in ramda.js

Given the following: 给定以下内容:

var xs = [{a: 1}, {a: 2}, {a: 3}];
R.findIndex(R.propEq('a', 2))(xs); //=> 1
R.findIndex(R.propEq('a', 4))(xs); //=> -1

How do I create a new function that does not bind propEq immediately. 如何创建不立即绑定propEq的新函数。 I thought curry might do it. 我以为curry可以做到。

var myfn = R.findIndex(R.propEq);
myfn('a', '2')(xs); // => 1

I tried curry, but I don't have it quite correct. 我试过咖喱,但是我不太正确。

var myfn = R.findIndex(R.curry(R.propEq)); // functional programming is rusty - this is not currect

Well, my first thought is that simply being explicit would be best here: 好吧,我的第一个想法是,简单地在这里表现最好:

const findByProp = curry((key, val, xs) => findIndex(propEq(key, val), xs));

const xs = [{a: 1, b: 10}, {a: 2, b: 20}, {a: 3, b: 30}];

findByProp('a', 1, xs);  //=> 0
findByProp('a', 2)(xs);  //=> 1
findByProp('b')(30)(xs); //=> 2

There might be some way to make this points-free using useWith or converge or Sanctuary's S combinator , but they would probably in the end not be as readable as this. 可能有一些方法可以使用useWithconverge或Sanctuary的S组合器来实现无useWith ,但最终它们可能不那么可读。

You can see this in action on the Ramda REPL . 您可以在Ramda REPL上看到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM