三元运算符类似于 ?:
我试图避免这样的构造:
val result = this.getClass.getSimpleName
if (result.endsWith("$")) result.init else result
好的,在这个例子中,then
和 else
分支很简单,但你可以想象复杂的分支。 我构建了以下内容:
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)
}
定义了这一点,我可以将上面的简单示例替换为:
this.getClass.getSimpleName is {s: String => s.endsWith("$")} ? {s: String => s.init} :: {s: String => s}
但是如何摆脱 s: String =>
?我想要这样的东西:
this.getClass.getSimpleName is {_.endsWith("$")} ? {_.init} :: {identity}
我想编译器需要额外的东西来推断类型。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
来自 Tony Morris 的 Lambda 博客:
From Tony Morris' Lambda Blog:
我们可以将 如何在 Scala 中定义保留前导标记的三元运算符? 与 选项包装值是一个好的模式吗? 获取
这是否足以满足您的需求?
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
Is this adequate for your needs?
Rex Kerr的答案用基本的Scala表达:
虽然我不确定哪一部分您想要优化的 if-else 结构。
Rex Kerr’s answer expressed in basic Scala:
although I’m not sure what part of the if–else construct you want to optimise.
由于 Scala 中的 if-else 结构返回一个值,因此您可以使用此
更多信息: https://alvinalexander.com/scala/scala-if-then-ternary-operator-cookbook-examples
Since if-else constructions in Scala return a value, you can use this
More info: https://alvinalexander.com/scala/scala-if-then-ternary-operator-cookbook-examples
由于 : 本身不是一个有效的运算符,除非您同意始终使用反引号
:
转义它,因此您可以使用另一个字符,例如“|”正如上面的答案之一。但是有山羊胡子的猫王怎么样?::当然,如果你的值是列表,这又不起作用,因为它们本身有 :: 运算符。
Since : by itself won't be a valid operator unless you are ok with always escaping it with back ticks
:
, you could go with another character, e.g. "|" as in one of the answers above. But how about elvis with a goatee ?::Of course this again won't work if you values are lists, since they have :: operator themselves.