gpt4 book ai didi

javascript - 我怎样才能使这段代码在 Ramda.js/lodash 中更像 'functional programming' 风格

转载 作者:行者123 更新时间:2023-11-30 16:09:56 25 4
gpt4 key购买 nike

我有这段代码:

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);

如果 .bind(this.commands)对于 add功能是必需的,您可以使用 <a href="https://lodash.com/docs#bindKey" rel="noreferrer noopener nofollow">_.bindKey</a>(this.commands, "add") 缩短它.

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

关于javascript - 我怎样才能使这段代码在 Ramda.js/lodash 中更像 'functional programming' 风格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36432868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com