Home > Mobile >  how to use a lookup to know the function to be executed?
how to use a lookup to know the function to be executed?

Time:06-17

I have a scala map as below

val funcMap: Map(String,String)

(country_code, formatting_function_name)
(AU,function001)
(BH,function006)
(CN,function007)
(IN,function001)

On a given dataframe (indf),

id,country
001,AU
002,BH
003,CN
004,IN

I need to perform a lookup using this map and execute the function as returned with "id" as argument to get a column named, say "mapped_id". eg. for AU -> mapped_id column should call function001("id")

indf.withcolumn("mapped_id",funcMap(col("country_code")))   //<- need to execute the returned function here eg: function001("id") to get the formatted value of id 

these functions may be defined in a functions.scala file with different formatting logics eg.

def function001(id:String): String = {
lpad(id,5,"0")
}

def function002(id:String): String = {
rpad(id,5,"0")
}

How do I call the function as per the name returned from the map?

CodePudding user response:

Having a Map from country_code -> formatting_function_name means you need another layer of converting a function name to a function call. You can avoid this because in Scala functions are first-class entities, which means they can be passed around to other functions as parameters, because functions are objects.

A straightforward solution is to change your Map value type parameter according to the type of the formatting function. Here's what I mean:

val funcMap: Map[String, String => String]

Now you can create the formatted String for your country_code using:

indf.withcolumn("mapped_id", funcMap("country_code")("mapped_id"))

Here's a quick test:

object Something extends App {

  val Id = "someId"

  val Au = "AU"
  val Bh = "BH"
  val Cn = "CN"
  val In = "IN"

  def function001(id: String): String = s"$id function001"
  def function002(id: String): String = s"$id function002"
  def function003(id: String): String = s"$id function003"
  def function004(id: String): String = s"$id function004"

  val funcMap = Map[String, String => String](
    Au -> function001,
    Bh -> function002,
    Cn -> function003,
    In -> function004
  )

  println(funcMap(Au)(Id))
}

Outputs:

someId function001
  • Related