使用 Ramda 进行部分应用程序参数操作

Partial application argument manipulation with Ramda

这显然是行不通的——因为 always 不像在 whenPropTrueAlways 的顶级实现中那样用 whenTrueValue 调用。有没有办法像我正在尝试的那样操纵调用另一种方法?将参数替换为特定值,以及将函数应用于另一个参数?

export const whenPropEq = curry((key, predicateValue, whenTrueFn, data) =>
  when(
    propEq(key, predicateValue),
    whenTrueFn,
  )(data),
);

const whenPropTrueAlways = curry((key, whenTrueValue, data) =>
  whenPropEq(key, true, always(whenTrueValue), data),
);

// Doesn't work, for obvious reasons- just trying to illustrate the desire
const whenPropTrueAlways = whenPropEq(__, true, always(__), __);

在此先感谢您的帮助。

它不漂亮,但你可以这样写:

const whenPropTrue = useWith(flip(whenPropEq)(true), [identity, always, identity]);

useWith is one of the more unusual Ramda functions (along with its sister function converge.) 它可以帮助人们做一些否则很难做到的事情。但我常常发现,当很难让事情不计分时,这表明人们不应该费心去做。