Ramda curry/uncurry ES6 默认参数语法问题
Ramda curry/uncurry issue with ES6 default parameter syntax
在以下示例中,当存在默认参数时,我无法使 Ramda.js uncurry 函数与 ES6 箭头函数一起使用:
const tt = (x) => y => x + y;
const tt1 = (x = 5) => y => x + y;
const uncurry = R.uncurryN(2);
console.log( uncurry(tt)(1,2) );
console.log( uncurry(tt1)(1,2) );
tt
和tt1
这两个函数应该是一样的,除了一个默认参数。但是输出不同(babel-node 6.24.1):
3
6
好像默认语法是用uncurrying来赋值的。
我是不是漏掉了什么?这是有意为之还是错误?
澄清一下:
如果没有 curry/uncurry,两个函数(有或没有默认值)在完全提供参数时的行为方式相同。 tt(1)(2)
和 tt1(1)(2)
的计算结果都是 3
。为什么更改调用约定后行为会有所不同?
顺便说一下,我正在关注一些 react/redux 示例 () 并测试是否可以将一些 React 函数调用转换为 Ramda 样式。这里讨论的问题显然是一个障碍。
虽然这不是有意为之,但很难找到真正有用的解决方案。
错误在this line执行中:
endIdx = currentDepth === depth ? arguments.length : idx + value.length;
如果换成
endIdx = currentDepth === depth ? arguments.length : idx + 1;
你的两个函数将工作相同。那将打破这种用法:
const tt2 = (x, y) => (z) => x + y + z;
uncurry(tt2)(1, 2, 3); //=> NaN
(在当前的实现中,这 returns 6
。)
这可能不是那么重要。尝试将 uncurryN
用于未完全柯里化的函数似乎是一件奇怪的事情。
但总的来说,我不知道这有什么帮助。默认参数可能难以使用。具有默认值的参数不会影响函数的 length
:
length
is a property of a function object, and indicates how many arguments the function expects, i.e. the number of formal parameters. This number excludes the rest parameter and only includes parameters before the first one with a default value. -- MDN
装饰器函数无法使用默认值,因此 uncurryN
之类的东西会丢失默认值。这使得在一个具有默认值的参数之后使用任何参数变得很尴尬。
当然,您仍然可以这样做:
((x, y) => tt1(x)(y))(1, 2); //=> 3
((x, y) => tt1(x)(y))(undefined, 2); //=> 7
传递给 uncurry(tt1)
的 undefined
也可以。但是传递一个明确的 undefined
感觉有点奇怪。
欢迎 raise an issue Ramda 讨论这个问题。
您可以在 Ramda REPL.
上尝试这些想法
在以下示例中,当存在默认参数时,我无法使 Ramda.js uncurry 函数与 ES6 箭头函数一起使用:
const tt = (x) => y => x + y;
const tt1 = (x = 5) => y => x + y;
const uncurry = R.uncurryN(2);
console.log( uncurry(tt)(1,2) );
console.log( uncurry(tt1)(1,2) );
tt
和tt1
这两个函数应该是一样的,除了一个默认参数。但是输出不同(babel-node 6.24.1):
3
6
好像默认语法是用uncurrying来赋值的。
我是不是漏掉了什么?这是有意为之还是错误?
澄清一下:
如果没有 curry/uncurry,两个函数(有或没有默认值)在完全提供参数时的行为方式相同。 tt(1)(2)
和 tt1(1)(2)
的计算结果都是 3
。为什么更改调用约定后行为会有所不同?
顺便说一下,我正在关注一些 react/redux 示例 (
虽然这不是有意为之,但很难找到真正有用的解决方案。
错误在this line执行中:
endIdx = currentDepth === depth ? arguments.length : idx + value.length;
如果换成
endIdx = currentDepth === depth ? arguments.length : idx + 1;
你的两个函数将工作相同。那将打破这种用法:
const tt2 = (x, y) => (z) => x + y + z;
uncurry(tt2)(1, 2, 3); //=> NaN
(在当前的实现中,这 returns 6
。)
这可能不是那么重要。尝试将 uncurryN
用于未完全柯里化的函数似乎是一件奇怪的事情。
但总的来说,我不知道这有什么帮助。默认参数可能难以使用。具有默认值的参数不会影响函数的 length
:
length
is a property of a function object, and indicates how many arguments the function expects, i.e. the number of formal parameters. This number excludes the rest parameter and only includes parameters before the first one with a default value. -- MDN
装饰器函数无法使用默认值,因此 uncurryN
之类的东西会丢失默认值。这使得在一个具有默认值的参数之后使用任何参数变得很尴尬。
当然,您仍然可以这样做:
((x, y) => tt1(x)(y))(1, 2); //=> 3
((x, y) => tt1(x)(y))(undefined, 2); //=> 7
传递给 uncurry(tt1)
的 undefined
也可以。但是传递一个明确的 undefined
感觉有点奇怪。
欢迎 raise an issue Ramda 讨论这个问题。
您可以在 Ramda REPL.
上尝试这些想法