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

发布于 2024-09-07 17:35:43 字数 217 浏览 3 评论 0原文

在 Ruby 中我可以这样写:

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

How do I do this in Scala?

编辑:最好我想比使用 if 做得更优雅。

In Ruby I can write this:

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

How do I do this in Scala?

Edit: preferably I'd like to do it more elegantly than using if.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故事与诗 2024-09-14 17:35:43

内部模式匹配可以用守卫来表示:

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"
}

Inside pattern match it can be expressed with guards:

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"
}
赤濁 2024-09-14 17:35:43

与@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

Similar to @Yardena's answer, but using basic comparisons:

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"
}

Also works for floating point n

节枝 2024-09-14 17:35:43
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 实例的命名应使用首字母大写。如果不这样做,则需要在反引号中给出名称(这里很困难,除非有我不知道的转义)

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

Note that Contains instances should be named with initial caps. If you don't, you'll need to give the name in back-quotes (difficult here, unless there's an escape I don't know)

淑女气质 2024-09-14 17:35:43

对于相同大小的范围,您可以用老式数学来完成:

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

11 in 10-19

是的,我知道:“没有必要不要除!”
但是:分而治之!

For Ranges of equal size, you can do it with old-school math:

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

11 in 10-19

Yes, I know: "Don't divide without neccessity!"
But: Divide et impera!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文