0

I am working on a project where i supposed to work with conversion based on the key. Since there are lot of key are there I was supposed to write a case for each key and logic for that code. But this approach leads to increase in lines of code.

So I was required to change the code by using map(key ->fun(length ,description, convertionMatchCase)

val h = scala.collection.mutable.Map(
  "01" -> tagfunction(2, "Tag Length", "ToInt"),
  "02" -> tagfunction(1, "Firmware version", "ToInt"),
  "03" -> tagfunction(15, "IMEI", "ToASCII"),
  "04" -> tagfunction(2, "Device ID", "ToInt"),
  "10" -> tagfunction(2, "No of Archive records", "ToInt"),
  "20" -> tagfunction(4, "Date and time", "ToInt"),
  //"30" -> tagfunction(9, "Coordinates", "ToASCII"),
  //"33" -> tagfunction(4, "Speed and direction", "ToASCII"),
  "34" -> tagfunction(2, "Height", "ToSignedShort"),
  "35" -> tagfunction(1, "HDOP", "ToInt"),
  "40" -> tagfunction(2, "Status of device", "ToInt"),
  "41" -> tagfunction(2, "Supply Voltage", "ToInt"),
  "42" -> tagfunction(2, "Battery Voltage", "ToInt"),
  "43" -> tagfunction(1, "Temperature of tracking device", "ToSignedByte"),
)

The problem is that function's inside the map are executing automatically(even when i'm using lazy after calling a single map(key) every thing was executing)

Could any one please suggest me some solution. Thanks you

1
  • 1
    Please share the tagfunction signature and how you use your Map, it's not clear. Commented Aug 8, 2022 at 17:30

2 Answers 2

1

First of all try to avoid using mutable Map's in favor of immutable ones as it is a more idiomatic approach in Scala.

Secondly - your Map is not a map from key to function, it is a map from key to a value. Possibly you are looking for lambda expressions:

val h = Map(
  "01" -> (() => tagfunction(2, "Tag Length", "ToInt")),
  "02" -> (() => tagfunction(1, "Firmware version", "ToInt")),
  ...
)

This will allow to get invokable function by key, though it is not very clear why do you need to invoke it every time (cause usually having functions with side effects is also considered non-idiomatic approach for Scala, at least by some =).

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

2 Comments

Thanks for the answer and I tried calling h("01")( ) which worked perfectly for my problem. Thank you Guru Stron.
@Nagababu was glad to help! If any of the answers works for you please mark any as an accepted one (checkmark next to the answer).
1

I am not sure why number of lines matters in the first place ... but here is how you can minimize it:

    val map = Map( // do not use mutable!
       "foo" -> (1, "bar"),
       "baz" -> (2, "bat"), 
       "bam" -> (3, "baz")
    )
    
    def convert(key: String) = tagfunction.tupled(map(key))

The last line might need a bit of an explanation. tagfunction.tupled converts a function of two arguments Int, String into a function with a single argument, which is a tuple (Int, String). Because tuples are stored in the map, you get one with map(key), and can send it directly into the function.

A longer way to write it would be:

   val (n,s) = map(key)
   tagfunction(n,s)

But since you are after minimizing the number of lines, I thought, I'd show you that little trick.

1 Comment

Thanks you Dima, for the trick followed by explanation

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.