【问题标题】:Ramda ifElse is not executed for function with multiple argumentsRamda ifElse 不会为具有多个参数的函数执行
【发布时间】:2017-12-04 11:06:44
【问题描述】:

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

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

【问题讨论】:

    标签: javascript ramda.js


    【解决方案1】:

    您的第二个函数是柯里化二元函数。 ifElse 选择传递给它的三个函数中的最大数量,predicateifTrueifFalseR.T 有 arity 1,() => null 也有,但 secTest 有 2,所以 unexpectedIfElse 也有 2。

    当您使用“无日志记录”调用 unexpectedIfElse 时,您会返回一个等待(无用的)b 参数的函数。

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

    你可以通过调用它来解决你的问题

    unexpectedIfElse('no logging', 'ignored');
    

    或喜欢

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      相关资源
      最近更新 更多