繁体   English   中英

ramda.js中的函数组成

[英]function composition in ramda.js

给定以下内容:

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

如何创建不立即绑定propEq的新函数。 我以为curry可以做到。

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

我试过咖喱,但是我不太正确。

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

好吧,我的第一个想法是,简单地在这里表现最好:

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

可能有一些方法可以使用useWithconverge或Sanctuary的S组合器来实现无useWith ,但最终它们可能不那么可读。

您可以在Ramda REPL上看到这一点。

暂无
暂无

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

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