【问题标题】:How can I make this code more 'functional programming' style in Ramda.js / lodash如何在 Ramda.js / lodash 中使这段代码更具“函数式编程”风格
【发布时间】:2016-07-25 18:40:35
【问题描述】:

我有这段代码:

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

_.mapObjIndexed(fn, this.commandList);

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

【问题讨论】:

  • 您总是可以通过将它们连接在一起使其成为一行。

标签: javascript lodash ramda.js


【解决方案1】:

您似乎在寻找flip function / flip function

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

如果add 函数的.bind(this.commands) 是必要的,您可以使用@987654323@(this.commands, "add") 缩短它。

如果由于参数数量过多导致翻转无法正常工作,您可能需要输入_.ary(…, 2)_.binary(…)

【讨论】:

  • 谢谢。这完美无缺:_.mapObjIndexed(_.flip(_.bind(this.commands.add, this)), this.commandList)
  • @SuchMuchCode:我猜都是 Ramda?顺便说一句,当您绑定到 this 而不是 this.commands 时,我认为您不需要 bind
  • 我的错,我实际上绑定到this.commands。上面我打错了。是的,如果不是这样,我就不需要使用绑定。 :)
猜你喜欢
  • 1970-01-01
  • 2022-07-11
  • 1970-01-01
  • 2022-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-20
相关资源
最近更新 更多