Ramda ifElse 不为具有多个参数的函数执行
Ramda ifElse is not executed for function with multiple arguments
谁能给我解释一下这个行为,或者这是一个错误?
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');
你的第二个函数是柯里化二元函数。 ifElse
选择传递给它的三个函数 predicate
、ifTrue
和 ifFalse
中的最大元数。 R.T
的元数为 1,() => null
也是,但 secTest
的元数为 2,因此 unexpectedIfElse
的元数为 2。
当您使用 'no logging' 调用 unexpectedIfElse
时,您会返回一个等待(无用的)b
参数的函数。
有理由不喜欢这种额外的复杂性,但有时它非常有用,尤其是对于谓词。
您可以像这样调用它来解决您的问题
unexpectedIfElse('no logging', 'ignored');
或喜欢
unexpectedIfElse('no logging')('ignored')
谁能给我解释一下这个行为,或者这是一个错误?
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');
你的第二个函数是柯里化二元函数。 ifElse
选择传递给它的三个函数 predicate
、ifTrue
和 ifFalse
中的最大元数。 R.T
的元数为 1,() => null
也是,但 secTest
的元数为 2,因此 unexpectedIfElse
的元数为 2。
当您使用 'no logging' 调用 unexpectedIfElse
时,您会返回一个等待(无用的)b
参数的函数。
有理由不喜欢这种额外的复杂性,但有时它非常有用,尤其是对于谓词。
您可以像这样调用它来解决您的问题
unexpectedIfElse('no logging', 'ignored');
或喜欢
unexpectedIfElse('no logging')('ignored')