Ternary conditional operator: Difference between revisions

Content deleted Content added
R: fix syntaxhighlight errors
Line 26:
The ternary operator can also be viewed as a binary map operation.
 
In R—and other languages with literal expression tuples—one can simulate the ternary operator with something like the R expression {{code|c(expr1,expr2)[1+condition]|r}} (this idiom is slightly more natural in languages with 0-origin subscripts)
Nested ternaries can be simulated as {{code|c(expr1,expr2,expr3)[which.first((c(cond1,cond2,TRUE))]|r}} where the function {{code|which.first}} returns the index of the first true value in the condition vector. Note that both of these map equivalents are binary operators, revealing that the ternary operator is ternary in syntax, rather than semantics. These constructions can be regarded as a weak form of [[currying]] based on data concatenation rather than function composition.
 
If the language provides a mechanism of [[futures and promises|futures or promises]], then short-circuit evaluation can sometimes also be simulated in the context of a binary map operation.