简体   繁体   English

Ramda ifElse未针对具有多个参数的函数执行

[英]Ramda ifElse is not executed for function with multiple arguments

Can somebody explain me this behavior, or is it a bug? 有人可以向我解释这种行为,还是一种错误?

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 REPL中的示例

Your second function is a curried binary function. 您的第二个功能是咖喱的二进制功能。 ifElse chooses the maximum arity of the three functions passed to it, predicate , ifTrue , and ifFalse . ifElse在传递给它的三个函数predicateifTrueifFalse选择最大ifFalse RT has arity 1, as does () => null , but secTest has arity 2, so unexpectedIfElse also has arity 2. RT具有1的secTest() => null也是如此,但是secTest具有2的secTest ,因此unexpectedIfElse secTest也具有2的secTest

When you call unexpectedIfElse with 'no logging', you get back a function waiting for the (useless) b parameter. 当您以“不记录”调用unexpectedIfElse时,您将获得一个等待(无用) b参数的函数。

There are reasons not to like this additional complexity, but there are times when it's very useful, especially for the predicate. 有理由不喜欢这种额外的复杂性,但是有时候它非常有用,尤其是对于谓词。

You can fix your issue by calling it like 您可以通过以下方式解决问题:

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

or like 或喜欢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM