如何在 Ramda.js / lodash 中使此代码更具 'functional programming' 风格

How can I make this code more 'functional programming' style in Ramda.js / lodash

我有这段代码:

var fn = function(fn, name) {
   this.commands.add(name, fn);
}.bind(this);

_.mapObjIndexed(fn, this.commandList);

我觉得有一种方法可以改进这段代码并将其简化为一行。我尝试了很多不同的方法,但我是 ramda.js 的新手,我可能会遗漏一些功能,这将使这变得更容易和更简单。

您似乎在寻找 flip function / flip function:

_.mapObjIndexed(_.flip(this.commands.add), this.commandList);

如果 add 函数需要 .bind(this.commands),您可以使用 <a href="https://lodash.com/docs#bindKey" rel="nofollow">_.bindKey</a>(this.commands, "add" 来缩短它).

如果由于参数数量原因翻转不能正常工作,您可能需要输入 _.ary(…, 2) or _.binary(…)