对于具有多个参数的函数,不执行Ramda ifElse

时间:2017-12-04 11:06:45

标签: javascript ramda.js

有人可以解释一下这种行为,还是一个错误?

const firstTest = (a) => console.log(a, 'this will be executed');
const secTest = (a, b) => console.log(a, 'this will not be executed');

const firstIfElse = R.ifElse(R.T, firstTest, () => null);
const unexpectedIfElse = R.ifElse(R.T, secTest, () => null);

firstIfElse('logging appears as expected');
unexpectedIfElse('no logging');

example in REPL

1 个答案:

答案 0 :(得分:2)

你的第二个功能是一个咖喱二进制函数。 ifElse选择传递给它的三个函数的最大元数predicateifTrueifFalseR.T有arity 1,() => null也有,secTest有2,所以unexpectedIfElse也有2。

当您使用“no logging”调用unexpectedIfElse时,您将返回一个等待(无用的)b参数的函数。

有理由不喜欢这种额外的复杂性,但有时它非常有用,特别是对于谓词。

您可以通过调用

来解决问题
unexpectedIfElse('no logging', 'ignored');

或喜欢

unexpectedIfElse('no logging')('ignored')