简体   繁体   English

如何在Ramda.js / lodash中使此代码更具“功能性编程”风格

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

I have this bit of code: 我有这段代码:

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

_.mapObjIndexed(fn, this.commandList);

I feel like there's a way to improve this bit of code and make it just one line. 我觉得有一种方法可以改进这段代码,使其仅一行。 I tried many different ways, but I'm new to ramda.js and I might be missing some function, that would make this easier and simpler. 我尝试了许多不同的方法,但是我对ramda.js ,我可能会缺少一些功能,这会使此操作变得更加容易和简单。

You seem to be looking for the flip function / flip function : 您似乎在寻找flip功能 / flip功能

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

If a .bind(this.commands) for the add function is necessary, you can shorten that using _.bindKey (this.commands, "add") . 如果需要add函数的.bind(this.commands) ,则可以使用_.bindKey (this.commands, "add")来缩短它。

If the flipping doesn't work correctly because of the number of arguments, you might needt to throw in _.ary(…, 2) or _.binary(…) . 如果由于参数数量而导致翻转无法正常进行,则可能不需要输入_.ary(…, 2)_.binary(…)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM