41

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.

David J.
  • 31,569
  • 22
  • 122
  • 174
Theo
  • 131,503
  • 21
  • 160
  • 205
  • 2
    See a related stackoverflow question: [Can a range be matched in Scala?](http://stackoverflow.com/questions/1346127/can-a-range-be-matched-in-scala) – David J. Sep 26 '11 at 01:35

4 Answers4

71

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"
}
Yardena
  • 2,837
  • 20
  • 17
  • This is one of those cases where Scala pattern matching provides absolutely no benefit over a plain old if-else chain. Also, using a Range doesn't provide any benefit over a plain-old `0 <= n && n < 5` term. – Christian Schlichtherle Apr 18 '20 at 14:48
  • I think this is also way more expensive computationally. generating a sequence to do set membership when all you meant was a < x && x < b ... – WamBamBoozle Dec 17 '20 at 19:52
16

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

gens
  • 972
  • 11
  • 22
  • I much prefer this way of writing such conditions. One detail: the parentheses are unnecessary; `case i if i >= 0 && i < 5` suffices (use whatever you prefer). – Jonik Nov 27 '18 at 14:41
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

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)

Randall Schulz
  • 26,420
  • 4
  • 61
  • 81
4

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!

user unknown
  • 35,537
  • 11
  • 75
  • 121