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
可能有一些方法可以使用 useWith
or converge
or Sanctuary's S
combinator 使这个点免费,但它们最终可能不像这个那样可读。
您可以在 Ramda REPL.
上看到实际效果
鉴于以下情况:
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
可能有一些方法可以使用 useWith
or converge
or Sanctuary's S
combinator 使这个点免费,但它们最终可能不像这个那样可读。
您可以在 Ramda REPL.
上看到实际效果