在 Scala 的模式匹配系统中使用比较运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1585395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using comparison operators in Scala's pattern matching system
提问by BefittingTheorem
Is it possible to match on a comparison using the pattern matching system in Scala? For example:
是否可以使用 Scala 中的模式匹配系统进行比较?例如:
a match {
case 10 => println("ten")
case _ > 10 => println("greater than ten")
case _ => println("less than ten")
}
The second case statement is illegal, but I would like to be able to specify "when a is greater than".
第二个 case 语句是非法的,但我希望能够指定“当 a 大于”时。
回答by Ben James
You can add a guard, i.e. an ifand a boolean expression after the pattern:
您可以if在模式后添加一个保护,即 an和一个布尔表达式:
a match {
case 10 => println("ten")
case x if x > 10 => println("greater than ten")
case _ => println("less than ten")
}
Edit: Note that this is more than superficially different to putting an ifafterthe =>, because a pattern won'tmatch if the guard is not true.
编辑:请注意,这是比肤浅到把一个不同if之后的=>,因为一个模式不会如果保护是不正确的匹配。
回答by seh
As a non-answer to the question's spirit, which asked how to incorporate predicates into a match clause, in this case the predicate can be factored out before the match:
作为对问题精神的非回答,该问题询问如何将谓词合并到匹配子句中,在这种情况下,谓词可以在 之前分解match:
def assess(n: Int) {
println(
n compare 10 match {
case 0 => "ten"
case 1 => "greater than ten"
case -1 => "less than ten"
})
}
Now, the documentation for scala.math.Ordering.compare(T, T)promises only that the non-equal outcomes will be greater thanor less than zero. Java's Comparable#compareTo(T)is specified similarly to Scala's. It happens to be conventional to use 1 and -1 for the positive and negative values, respectively, as Scala's current implementationdoes, but one can't make such an assumption without some risk of the implementation changing out from underneath.
现在,文档scala.math.Ordering.compare(T, T)只承诺不相等的结果将大于或小于零。Java 的Comparable#compareTo(T)指定类似于 Scala 的。就像 Scala 的当前实现那样,分别使用 1 和 -1 分别表示正值和负值是惯例,但是如果不存在实现从底层改变的风险,就不能做出这样的假设。
回答by vergenzt
A solution that in my opinion is much more readable than adding guards:
在我看来,一个比添加警卫更具可读性的解决方案:
(n compare 10).signum match {
case -1 => "less than ten"
case 0 => "ten"
case 1 => "greater than ten"
}
Notes:
笔记:
Ordered.comparereturns a negative integer if this is less than that, positive if greater, and0if equal.Int.signumcompresses the output fromcompareto-1for a negative number (less than 10),1for positive (greater than 10), or0for zero (equal to 10).
Ordered.compare如果小于该值,则返回负整数,如果大于则返回正整数,0如果相等则返回正整数 。Int.signum压缩从输出compare到-1用于负数(小于10),1用于正(大于10),或0零(等于10)。
回答by Sergii Zhuravskyi
While all the above and bellow answers perfectly answer the original question, some additional information can be found in the documentation https://docs.scala-lang.org/tour/pattern-matching.html, they didn't fit in my case but because this stackoverflow answer is the first suggestion in Google I would like to post my answer which is a corner case of the question above.
My question is:
虽然以上和下面的所有答案都完美地回答了原始问题,但可以在文档https://docs.scala-lang.org/tour/pattern-matching.html 中找到一些其他信息,但它们不适合我的情况但是因为这个 stackoverflow 答案是 Google 中的第一个建议,所以我想发布我的答案,这是上述问题的一个极端情况。
我的问题是:
- How to use a guard in match expression with an argument of a function?
- 如何在带有函数参数的匹配表达式中使用保护?
Which can be paraphrased:
可以改写为:
- How to use an if statement in match expression with an argument of a function?
- 如何在带有函数参数的匹配表达式中使用 if 语句?
The answer is the code example below:
答案是下面的代码示例:
def drop[A](l: List[A], n: Int): List[A] = l match {
case Nil => sys.error("drop on empty list")
case xs if n <= 0 => xs
case _ :: xs => drop(xs, n-1)
}
link to scala fiddle : https://scalafiddle.io/sf/G37THif/2as you can see the case xs if n <= 0 => xsstatement is able to use n(argument of a function) with the guard(if) statement.
scala fiddle 的链接:https: //scalafiddle.io/sf/G37THif/2正如您所见,该case xs if n <= 0 => xs语句能够将 n(函数的参数)与 guard(if) 语句一起使用。
I hope this helps someone like me.
我希望这可以帮助像我这样的人。

