Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ternary Operator Similar To ?:

Tags:

scala

I am trying to avoid constructs like this:

val result = this.getClass.getSimpleName if (result.endsWith("$")) result.init else result 

Ok, in this example the then and else branch are simple, but you can image complex ones. I built the following:

object TernaryOp {   class Ternary[T](t: T) {     def is[R](bte: BranchThenElse[T,R]) = if (bte.branch(t)) bte.then(t) else bte.elze(t)   }   class Branch[T](branch: T => Boolean) {     def ?[R] (then: T => R) = new BranchThen(branch,then)   }   class BranchThen[T,R](val branch: T => Boolean, val then: T => R)   class Elze[T,R](elze: T => R) {     def :: (bt: BranchThen[T,R]) = new BranchThenElse(bt.branch,bt.then,elze)   }   class BranchThenElse[T,R](val branch: T => Boolean, val then: T => R, val elze: T => R)   implicit def any2Ternary[T](t: T) = new Ternary(t)   implicit def fct2Branch[T](branch: T => Boolean) = new Branch(branch)   implicit def fct2Elze[T,R](elze: T => R) = new Elze(elze) } 

Defined that, I can replace the above simple example with:

this.getClass.getSimpleName is {s: String => s.endsWith("$")} ? {s: String => s.init} :: {s: String => s} 

But how can I get rid of the s: String =>? I want something like that:

this.getClass.getSimpleName is {_.endsWith("$")} ? {_.init} :: {identity} 

I guess the compiler needs the extra stuff to infer types.

like image 758
Peter Schmitz Avatar asked Feb 09 '11 16:02

Peter Schmitz


People also ask

Is ?: ternary operator?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

Which is called ternary operator ?: && ===?

The conditional operator (? :) is a ternary operator (it takes three operands).

How is ternary operator similar to if else?

The conditional operator – also known as the ternary operator – is an alternative form of the if/else statement that helps you to write conditional code blocks in a more concise way. First, you need to write a conditional expression that evaluates into either true or false .

Is there an equivalent of C's ?: ternary operator in Python?

Yes, it was added in version 2.5.


2 Answers

From Tony Morris' Lambda Blog:

I hear this question a lot. Yes it does. Instead of c ? p : q, it is written if(c) p else q.

This may not be preferable. Perhaps you’d like to write it using the same syntax as Java. Sadly, you can’t. This is because : is not a valid identifier. Fear not, | is! Would you settle for this?

c ? p | q 

Then you’ll need the following code. Notice the call-by-name (=>) annotations on the arguments. This evaluation strategy is required to correctly rewrite Java’s ternary operator. This cannot be done in Java itself.

case class Bool(b: Boolean) {      def ?[X](t: => X) = new {     def |(f: => X) = if(b) t else f      }  }  object Bool {      implicit def BooleanBool(b: Boolean) = Bool(b)  } 

Here is an example using the new operator that we just defined:

object T {   val condition = true    import Bool._    // yay!      val x = condition ? "yes" | "no" } 

Have fun ;)

like image 171
Landei Avatar answered Oct 05 '22 18:10

Landei


We can combine How to define a ternary operator in Scala which preserves leading tokens? with the answer to Is Option wrapping a value a good pattern? to get

scala>   "Hi".getClass.getSimpleName |> {x => x.endsWith("$") ? x.init | x} res0: String = String  scala> List.getClass.getSimpleName |> {x => x.endsWith("$") ? x.init | x} res1: String = List 

Is this adequate for your needs?

like image 33
Rex Kerr Avatar answered Oct 05 '22 18:10

Rex Kerr