scala - Ternary Operator Similar To ?: -


i trying avoid constructs this:

val result = this.getclass.getsimplename if (result.endswith("$")) result.init else result 

ok, in example then , else branch simple, can image complex ones. built 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, can replace above simple example with:

this.getclass.getsimplename {s: string => s.endswith("$")} ? {s: string => s.init} :: {s: string => s} 

but how can rid of s: string =>? want that:

this.getclass.getsimplename {_.endswith("$")} ? {_.init} :: {identity} 

i guess compiler needs stuff infer types.

we can combine how define ternary operator in scala preserves leading tokens? answer is option wrapping value pattern? 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 adequate needs?


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -