Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Ternary conditional operator
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Equivalence to map== 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.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)