I was curious that are there any ternary operator being used in programming language except ?:
operator. And could found only 2 from wikipedia
Is it only operator we have been used? Are there any more than these?
I was curious that are there any ternary operator being used in programming language except ?:
operator. And could found only 2 from wikipedia
Is it only operator we have been used? Are there any more than these?
Another useful class of ternary operator, especially in functional languages, is the "element update" operation. For example, OCaml expressions have three kinds of update syntax:
a.b<-c
means the record a
where field b
has value c
a.(b)<-c
means the array a
where index b
has value c
a.[b]<-c
means the string a
where index b
has value c
Note that these are not "update" in the sense of "assignment" or "modification"; the original object is unchanged, and a new object is yielded that has the stated properties. Consequently, these operations cannot be regarded as a simple composition of two binary operators.
Similarly, the Isabelle theorem prover has:
a(|b := c|)
meaning the record a
where field b
has value c
Yet another sort of ternary operator is array slice, for example in Python we have:
a[b:c]
meaning an array whose first element is a[b]
and last element is a[c-1]
In fact, Python has a quaternary form of slice:
a[b:c:d]
meaning an array whose elements are a[b + n*d]
where n
ranges from 0 to the largest value such that b + n*d < c
Although quite obscure, bash
has several forms of variable expansion (apparently borrowed from ksh
) that are ternary:
${var:pos:len}
is a maximum of len
characters from $var
, starting at pos
${var/Pattern/Replacement}
is $var
except the first substring within it that matches Pattern
is replaced with Replacement
${var//Pattern/Replacement}
is the same except all matches are replaced${var/#Pattern/Replacement}
is like the first case except Pattern
has to match a prefix of $var
${var/%Pattern/Replacement}
is like the previous except for matching a suffixThese are borderline in my opinion, being close to ordinary functions that happen to accept three arguments, written in the sometimes baroque style of shell syntax. But, I include them as they are entirely made of non-letter symbols.
In mathematics, an important ternary relation is congruence modulo:
a ≡ b (mod c)
is true iff a
and b
both belong to the same equivalence class in c
I'm not aware of any programming language that has this, but programming languages often borrow mathematical notation, so it's possible it exists in an obscure language. (Of course, most programming languages have mod
as a binary operator, allowing the above to be expressed as (a mod c) == (b mod c)
.) Furthermore, unlike the bash
variable substitution syntax, if this were introduced in some language, it would not be specific to that language since it is established notation elsewhere, making it more similar to ?:
in ubiquity.
There are some categories of operator I've chosen to exclude from the category of "ternary" operator:
a(b,c)
) that could apply to any number of operators.f(a,b,c)
) that accept three arguments, as there are too many of them for any to be interesting in this context.SUM
(Σ) or let
that function as a binding introduction of a new variable, since IMO a ternary operator ought to act on three already-existing things.sed
that happen to accept three arguments, as these really are like the named function case, and the language just has a very terse naming convention.Well it’s not a ternary operator per-say but I do think the three way comparison operator is highly underrated.
The ternary operator
is appropriate when a computation has to take place, even if I cannot use the effect inside of an if
/else
statement or switch
statement Consequently, 0
or the DEFAULT VALUE is treated as a DEFAULT VALUE when I try the computation.
The if
/else
or switch
statements require me to enumerate every case that can take place and are only helpful if the ability to discriminate between a numeric value and a branching choice can help. In some cases, it is clear why it won't help if a condition test exists, simply because I am either too early or too late to do something about the condition, even though some other function is unable to test for the given condition.
The ternary operator
forces a computation to have the effect that can pass through the code that follows it. Other types of condition tests resort to using, for instance, the &&
and ||
operators which can't guarantee a pass through.