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

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • hello this is completely different , so I am not asking an already answered question –  May 01 '18 at 13:35
  • 2
    Use pattern matching: https://stackoverflow.com/questions/4636610/how-to-pattern-match-using-regular-expression-in-scala?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – RoberMP May 01 '18 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. – Filnor May 03 '18 at 08:27

1 Answers1

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

Ramesh Maharjan
  • 41,071
  • 6
  • 69
  • 97
  • 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 –  May 01 '18 at 13:57
  • 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 –  May 01 '18 at 14:10
  • Hello Ramesh , I asked a new question which takes your answer as an input, may you pkease check it : https://stackoverflow.com/questions/50120107/recursive-calculation-over-lines-in-scala –  May 01 '18 at 16:25
  • 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. – som-snytt May 01 '18 at 19:11
  • Yes I agree with you @som-snytt but the OP wanted to do it without if expressions :) – Ramesh Maharjan May 02 '18 at 04:04