简体   繁体   中英

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. I thought curry might do it.

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.

You can see this in action on the Ramda REPL .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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