I would like to do the following pattern matching :
minReachableInt match {
   case None | Some(n) if n <= 0 =>
     println("All positive numbers can be reached")
   case _ =>
     println("Not all positive numbers can be reached")
}
Of course, it does not compile, because n is not matched in None. But as I don't need it in the subsequent code, how can I achieve my result without duplicating the code, in the most beautiful way you could imagine ?
There are limits to what you can do with pattern matching syntax, so don't try to use it to express all your logic.
This problem could be expressed using filter:
minReachableInt filter (_ <= 0) match {
  case None =>
    println("All positive numbers can be reached")
  case _ =>
    println("Not all positive numbers can be reached")
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With