0

I am using Scala reflection and toolbox to evaluate dynamic function as a string. And I am trying to evaluate it with data of type List[HashMap[String, Double]]

But it is giving error

Can't unquote List[scala.collection.immutable.HashMap[String,Double]], consider 
using ...
val dataAfterFunctionApplied = tb.eval(q"$functionSymbol.function($data)")

The code which I am using is as below.

package test

import scala.collection
import scala.collection.immutable.HashMap
import scala.reflect.runtime.universe.{Quasiquote, runtimeMirror}
import scala.tools.reflect.ToolBox

object ReflectionTest {
  def main(args: Array[String]): Unit = {
    val mirror = runtimeMirror(getClass.getClassLoader)
    val tb = ToolBox(mirror).mkToolBox()

    val data = List(
      HashMap("a" -> 1.0, "b" -> 267.0, "c" -> 26.0, "d" -> 2.0),
      HashMap("a" -> 1.0, "b" -> 2678.0, "c" -> 40.0, "d" -> 2.0),
      HashMap("a" -> 4.0, "b" -> 267.0, "c" -> 26.0, "d" -> 2.0),
      HashMap("a" -> 1.0, "b" -> 2678.0, "c" -> 90.0, "d" -> 17.0)
    )

    println("Data before function applied on it")
    println(data.mkString(","))

    val function = "def function(result: List[HashMap[String, Double]])={result.map(x=>(x('a'.toString)+x('b'.toString)))}"

    val functionWrapper = "object FunctionWrapper { " + function + "}"
    val functionSymbol=tb.define(tb.parse(functionWrapper).asInstanceOf[tb.u.ImplDef])

    val dataAfterFunctionApplied = tb.eval(q"$functionSymbol.function($data)")

    println("Data after function applied on it")
  }
}

If I am using Map (datatype) instead of HashMap then it is working, but the time consuming while parsing and evaluation is too much. So, I am trying to do it with HashMap.

But Unfortunately getting this error.

It is working with Map, that's ok. But the performance issue is coming because of toolbox parsing and evaluation from string to code. Any efficient way to parse string to code in scala??

johanandren
  • 11,249
  • 1
  • 25
  • 30
Tarun Khaneja
  • 451
  • 11
  • 23
  • You can use `List[Map[String,Double]](HashMap(...), ...)` or an explicit type on `data` to get a `List[Map]` containing `HashMap`s, but I don't see why you expect it would affect performance. – Alexey Romanov Oct 23 '18 at 14:25
  • Because if i will do operations like groupBy, in case of huge data, map will be slow because indexing on keys will not be there... but in hashmap all keys are hashed, so groupby will be faster – Tarun Khaneja Oct 23 '18 at 15:02
  • 1
    `Map`s having 4 or fewer elements use specialized implementations for efficiency (`Map.Map4`, `Map.Map3`, etc.). `Map`s that contain more than 4 elements use `HashMap` as the implementation. So there should be no performance improvement by specifying `HashMap` instead of `Map`. – Mike Allen Oct 23 '18 at 16:39
  • @AlexeyRomanov Yes , you are write performance issue is not becuase of Map. But it is because of toolbox which is parsing and evaluation string as code – Tarun Khaneja Oct 24 '18 at 05:35
  • @MikeAllen Thanks! I tested my code again, actually performance issue is coming because of toolbox parsing and evaluation of string to code. Any Idea to optimise it? – Tarun Khaneja Oct 24 '18 at 05:39

0 Answers0