Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I match multiple arguments?

I have a function:

def func(a: int, b: int, c: double): int 

And I want to match various possible scenarios

  1. Wherever c is 0, return b-a
  2. Wherever c > 9, return 0
  3. Wherever a=b return 0

And so on, before doing some more complex logic if none of the above are satisfied.

Do I have to match c separately first, or can I match on a,b,c, like _,_,0?

like image 884
Phil H Avatar asked Mar 22 '11 14:03

Phil H


People also ask

Can you do INDEX match with 3 criteria?

To extract data with different criteria or conditions in Microsoft Excel, the combination of INDEX and MATCH functions is best suited so far. In this article, you'll get to learn how you can use these INDEX and MATCH functions together with 3 different criteria in Excel with proper illustrations.


2 Answers

You can pattern match all described cases like this:

def func(a: Int, b: Int, c: Double) = (a, b, c) match {     case (a, b, 0) => b - a     case (a, b, c) if c > 9 || a == b => 0     case _ => 1 // add your logic here } 
like image 143
tenshi Avatar answered Sep 21 '22 17:09

tenshi


Following on from my comments to Easy Angel's answer, I still feel this

if (c == 0)    b -a else if (c > 9)    0 else if (a == b)    0 else     1 // your logic here 

is clearer. Basically because there isn't really any pattern to match here.

like image 29
The Archetypal Paul Avatar answered Sep 21 '22 17:09

The Archetypal Paul