I have the following code:
def myfunct(n: Int, steps: Int) = n match {
case 1 => steps
case (x) => if (x % 2 == 0) ...
Is there anyway to move the even number matching logic into the case ? Do I need a case class?
Such as:
def myfunct(n: Int, steps: Int) = n match {
case 1 => steps
case (even number??) => ...
Yes, it's called a guard:
def myfunct (n: Int, steps: Int) = n match {
case 1 => steps
case even if n % 2 == 0 => // stuff
case odd => // other stuff
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