JavaScript和ES6中用于咖喱函数的函数应用程序 [英] Function application for curried functions in JavaScript and ES6

查看:70
本文介绍了JavaScript和ES6中用于咖喱函数的函数应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢ECMAScript 6允许您编写如下的咖喱函数:

I love that ECMAScript 6 allows you to write curried functions like this:

var add = x => y => z => x + y + z;

但是,我讨厌我们需要在咖喱函数的每个参数中加上括号:

However, I hate that we need to parenthesize every argument of a curried function:

add(2)(3)(5);

我希望能够一次将咖喱函数应用于多个参数:

I want to be able to apply curried functions to multiple arguments at once:

add(2, 3, 5);

我该怎么办?我不在乎性能.

What should I do? I don't care about performance.

推荐答案

在Javascript中,咖喱和咖喱函数的应用是有争议的问题.简而言之,有两种相反的观点,我将简要说明两者.

Currying and the application of curried functions are controversial issues in Javascript. In simple terms, there are two opposing views, which I illustrate both briefly.

-仅在必要时使用单独的咖喱功能

从其他语言或范例适应概念在原则上是一件好事.但是,这种改编应该使用目标语言的基本手段来完成.对javascript来说,这意味着什么?

The adaptation of concepts from other languages or paradigms is in principle a good thing. This adaptation though, should be done with the elementary means of the target language. What does that mean for currying in javascript?

  • 咖喱函数被称为一元函数序列: add3(1)(2)(3);//6
  • 自己的函数通过箭头手动管理. const add3 = x =>y =>z =>x + y + z;
  • 第三方功能或方法由单独的咖喱功能咖喱

-默认情况下使用单独的咖喱实现方式

建议的 $ / uncurry 函数存在问题:

There's a problem with the proposed $/uncurry function:

const $ = (func, ...args) => args.reduce((f, x) => f(x), func);
const sum = x => y => z => x + y + z;

$(sum, 1, 2, 3); // 6
$(sum, 1, 2)(3); // 6
$(sum, 1)(2, 3); // z => x + y + z

通过这种方式,未经处理的函数只能使用无限数量的参数来应用一次.任何后续呼叫都必须设为一元.该功能完全符合其承诺.但是,它不允许应用咖喱函数,例如JavaScript开发人员习惯的.当前大多数咖喱实施方式都更加灵活.这是扩展的实现:

In this way uncurried functions can only once be applied with an unlimited number of arguments. Any subsequent calls must be made unary. The function does exactly what it promises. However, it does not allow application of curried functions, such as JavaScript developers are used to. Most of the current curry implementations are more flexible. Here's an extended implementation:

const uncurry = f => (...args) => args.reduce(
  (g, x) => (g = g(x), typeof g === "function" && g.length === 1
   ? uncurry(g) 
   : g), f
);

const sum = uncurry(x => y => z => x + y + z);

sum(1, 2, 3); // 6
sum(1, 2)(3); // 6
sum(1)(2, 3); // 6

如果您喜欢自动取消递归,则此实现有效:一旦未管理的函数本身生成了一个已管理的函数作为返回值,则此被返回的函数也将自动取消管理.如果您希望获得更多控制权,则以下实现可能更合适.

This implementation works, if you like auto-uncurrying: Once a uncurried function itself produces a curried function as a return value, this returned function is automatically uncurried. If you prefer more control, the following implementation might be more appropriate.

最终的即时应用

const partial = arity => f => function _(...args) {
  return args.length < arity
   ? (...args_) => _(...args.concat(args_))
   : f(args);
};

const uncurry = arity => f => partial(arity)(args => args.reduce((g, x) => g(x), f));
const sum = uncurry(3)(x => y => z => x + y + z);

sum(1, 2, 3); // 6
sum(1, 2)(3); // 6
sum(1)(2, 3); // 6

这个很小的arity参数为我们带来了所需的控制.我认为这是值得的.

This tiny arity parameter brings us the desired control. I think it's worth it.

其余部分的咖喱解决方案

我们如何处理超出我们控制范围的功能,因此没有进行手动处理?

What do we do with functions that are beyond our control and hence haven't been manually curried?

const curryN = uncurry(2)(arity => f => partial(arity)(args => f(...args)));
const add = curryN(2, (x, y) => x + y);
const add2 = add(2);

add2(4); // 6

幸运的是,我们能够重用 partial 并保持 curryN 简洁.使用此解决方案,还可以处理可变参数函数或带有可选参数的函数.

Fortunately, we were able to reuse partial and keep curryN concise. With this solution also variadic functions or such with optional parameters can be curried.

奖金:功能化"和欺骗性方法"

要使用咖喱方法,我们需要在一个显式参数中转换此讨厌的,隐式的 this 属性.事实证明,我们可以再次使用 partial 再次实现适当的实现:

To curry methods, we need to transform this nasty, implicit this property in an explicit parameter. It turns out that we can reuse partial for an adequate implementation once again:

const apply = uncurry(2)(arity => key => {
  return arity
   ? partial(arity + 1)(args => args[arity][key](...args.slice(0, arity)))
   : o => o[key]();
});

apply(0, "toLowerCase")("A|B|C"); // "a|b|c"
apply(0, "toLowerCase", "A|B|C"); // "a|b|c"

apply(1, "split")("|")("A|B|C"); // ["A", "B", "C"]
apply(1, "split")("|", "A|B|C"); // ["A", "B", "C"]
apply(1, "split", "|", "A|B|C"); // ["A", "B", "C"]

apply(2, "includes")("A")(0)("A|B|C"); // true
apply(2, "includes", "A", 0, "A|B|C"); // true

在此博客文章中,详细讨论了currying.

In this blog post currying is discussed in detail.

这篇关于JavaScript和ES6中用于咖喱函数的函数应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆