A Java event logging (analytics) library has a method that takes an event name and then varargs list of Strings like this:
Analytics.event("my event name", "key1", "value1", "key2", "value2" ...)
I've collected my event parameters in to a Map like
Map("key1" -> "value1", "key2" -> "value2" ...)
Now there must be a way to flatten the Map
to list where keys and values alternate and then feed it to the event method. I've had several guesses, like transforming the Map
to list a List
of Tuple
s, but calling .flatten on that says
No implicit view available from (java.lang.String, java.lang.String) => scala.collection.TraversableOnce[B]
What am I missing here?
You can use the : _*
operator:
def event(name: String, values: String*) {/*...*/}
val params = Map("key1" -> "value1", "key2" -> "value2")
val paramsArr = params flatMap {case(k, v) => List(k, v)} toArray
event("name", paramsArr: _*)
Not sure whether this works with Java varargs as well (let us know!)
You can use flatMap to map every key/value pair to a list with two elements and then concatenate these lists.
scala> Map("k1" -> "v1", "k2" -> "v2")
res0: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(k1 -> v1, k2 -> v2)
scala> res0.flatMap { case (k,v) => k::v::Nil }
res1: scala.collection.immutable.Iterable[java.lang.String] = List(k1, v1, k2, v2)
Once you have that, you can just pass the list to the event method:
Analytics.event("my event name", res1:_*)
val m = Map ("key1" -> "value1", "key2" -> "value2", "k3"-> "v3")
val li = m.map ((e)=> List (e._1, e._2)).flatten.toSeq
event ("name", li: _*)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With