2

I wrote the code above to define the type of String based on some rules.

def dataType (input:String) : String = input match {
  case input if input.startsWith("Q")   => "StringType";
  case input if (input.startsWith("8") && !(input.contains("F"))) => "IntegerType"
  case input if (input.startsWith("8") && (input.contains("F"))) => "FloatType"
  case _                             => "UnknowType";
}

This code works well , but I want to optimize it by avoiding the use of If satements. I want it to be based on pattern matching only without any use of if statements. I tried to modify it this way , but it gives me bad results :

def dataType (input:String) : String = input match {
  case "startsWith('Q')"  => "StringType"
  case "startsWith('8') && !(contains('F')))" => "IntegerType"
  case "startsWith('8') && (contains('F')))" => "FloatType"
  case _                             => "UnknowType";
}

it always gives me the UnknownType result

Any help with this please

Best Regards

3
  • hello this is completely different , so I am not asking an already answered question Commented May 1, 2018 at 13:35
  • 2
    Use pattern matching: stackoverflow.com/questions/4636610/… Commented May 1, 2018 at 13:37
  • Please do not vandalize your posts. If you believe your question is not useful or is no longer useful, it should be deleted instead of editing out all of the data that actually makes it a question. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 3.0 license). By SE policy, any vandalism will be reverted. Commented May 3, 2018 at 8:27

1 Answer 1

4

Since you are checking for the initial letter and boolean for containing F, you can create Tuple2[Char, Boolean] of those cases and use it in you match case as following

def dataType (input:String) : String = (input.charAt(0), input.contains('F')) match {

  case ('8', true) => "FloatType"
  case ('Q', _)  => "StringType"
  case ('8', false) => "IntegerType"
  case _ => "UnknowType"
}

And you should be fine

Sign up to request clarification or add additional context in comments.

5 Comments

I didn't know that we can do pattern matching over a tuple, this helps me a lot since I have to handle several conditions in different elements. Thanks a lot for your valuable answer
I have one more query for integer type I have to distinguish two subtypes based on the data length , I already have written a function to calculate data length
Hello Ramesh , I asked a new question which takes your answer as an input, may you pkease check it : stackoverflow.com/questions/50120107/…
This is undesirable because you always compute the contains. Better to just case 8 => if (contains) one else other. Sometimes it is nice to pattern match tuples and see a nice grid in the cases.
Yes I agree with you @som-snytt but the OP wanted to do it without if expressions :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.