鉴于以下情况:
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发布于 2016-05-04 11:07:43
嗯,我的第一个想法是,在这里简单地明确是最好的:
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、converge或庇护所的组合器可能有一些方法可以使这一点不受影响,但它们最终可能不会像这样可读性那么强。
您可以在Ramda REPL上看到这一点。
https://stackoverflow.com/questions/37022965
复制相似问题