I'm trying to flatten a Map's contents to a List of Strings.
So this collection :
val d: scala.collection.immutable.Map[String, scala.collection.immutable.Seq[Any]] =
Map("b" -> List(Array(1.0, 2.0), 5.333333333333333),
"d" -> List(Array(3.0, 3.0), 8.0))
Should be converted to a List[String]
type of two elements :
b,1,2,5.3
d 3,3,8.0
To achieve this im attempting to decompose the map and flatten its keys using :
d.map(m =>
m match {
case(k , v) => {
(k , v.flatten)
}
})
But I receive error :
Multiple markers at this line - No implicit view available from Any => scala.collection.GenTraversableOnce[B]. -
not enough arguments for method flatten: (implicit asTraversable: Any =>
scala.collection.GenTraversableOnce[B])scala.collection.immutable.Seq[B]. Unspecified value parameter
asTraversable.
How can collection scala.collection.immutable.Map[String, scala.collection.immutable.Seq[Any]]
be converted to List[String]
?
Update :
This works :
val map: Map[String, scala.collection.immutable.Seq[Any]] =
Map(
"bsds" -> List(Array(1.0, 2.0), 5.333333333333333),
"dsdfsd" -> List(Array(3.0, 3.0), 8.0)
) //> map : Map[String,scala.collection.immutable.Seq[Any]] = Map(bsds -> List(Ar
//| ray(1.0, 2.0), 5.333333333333333), dsdfsd -> List(Array(3.0, 3.0), 8.0))
val flatten = map.map {
case (k : String , v) =>
val expanded = v map {
case arr: Array[_] => Seq(arr: _*)
case el : Double => Seq(el)
}
(k , (expanded.flatten))
} //> flatten : scala.collection.immutable.Map[String,scala.collection.immutable.
//| Seq[Any]] = Map(bsds -> List(1.0, 2.0, 5.333333333333333), dsdfsd -> List(3.
//| 0, 3.0, 8.0))
val ll = flatten.map(m => List(m._1) ++ m._2) //> ll : scala.collection.immutable.Iterable[List[Any]] = List(List(bsds, 1.0,
//| 2.0, 5.333333333333333), List(dsdfsd, 3.0, 3.0, 8.0))
ll.map(m => m.mkString("|")) //> res5: scala.collection.immutable.Iterable[String] = List(bsds|1.0|2.0|5.3333
//| 33333333333, dsdfsd|3.0|3.0|8.0)