在Scala中如何对范围进行模式匹配?

41

在Ruby中,我可以这样写:

case n
when 0...5  then "less than five"
when 5...10 then "less than ten"
else "a lot"
end

如何在Scala中实现此功能?

编辑:最好以更优雅的方式实现,而不是使用if


2
请参考相关的stackoverflow问题:Scala中是否可以匹配范围? - David J.
4个回答

71

在模式匹配中,可以使用守卫来表达:

n match {
  case it if 0 until 5 contains it  => "less than five"
  case it if 5 until 10 contains it => "less than ten"
  case _ => "a lot"
}

这是其中一种情况,Scala模式匹配与普通的if-else链相比没有任何优势。此外,使用范围(Range)也不比普通的0 <= n && n < 5条件语句更有优势。 - Christian Schlichtherle
我认为这在计算上也更加昂贵。当你的意思只是 a < x && x < b 时,生成一个序列来执行集合成员检查... - WamBamBoozle

17

与@Yardena的回答类似,但使用基本比较:

n match {
    case i if (i >= 0 && i < 5) => "less than five"
    case i if (i >= 5 && i < 10) => "less than ten"
    case _ => "a lot"
}

同样适用于浮点数n


我更喜欢这种编写条件的方式。一个细节:括号是不必要的;case i if i >= 0 && i < 5 就足够了(使用您喜欢的任何方式)。 - Jonik

15
class Contains(r: Range) { def unapply(i: Int): Boolean = r contains i }

val C1 = new Contains(3 to 10)
val C2 = new Contains(20 to 30)

scala> 5 match { case C1() => println("C1"); case C2() => println("C2"); case _ => println("none") }
C1

scala> 23 match { case C1() => println("C1"); case C2() => println("C2"); case _ => println("none") }
C2

scala> 45 match { case C1() => println("C1"); case C2() => println("C2"); case _ => println("none") }
none

请注意,Contains实例应使用首字母大写命名。如果不这样做,你需要用反引号注明名称(这里很难,除非有我不知道的转义字符)


4

对于相等大小的范围,您可以使用老派的数学方法:

val a = 11 
(a/10) match {                      
    case 0 => println (a + " in 0-9")  
    case 1 => println (a + " in 10-19") } 

11 in 10-19

是的,我知道:“不要无谓地分割!” 但是:Divide et impera!


(注:这句话为拉丁语,意为“分而治之”)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接