I am trying to turn a Map("a" -> 2, "b" -> 1) into seq("a","a","b") through the map function, Currently I am trying to ru
I am not sure exactly what the shape of your data is. That part is a little bit unclear from the question.
Map('a' -> 3, 'b' -> 1)
Is indeed a Map. Whereas
('a' -> 3, 'b' -> 1)
desugars in a Tuple
(('a', 3), ('b', 1))
If it is the former case you can fold like this
val m : Map[String, Int] = Map("a" -> 2, "b" -> 1)
val res = m.foldLeft(List[String]())((a, b) => a ++ List.fill(b._2)(b._1))
here res
will be List('a', 'a', 'b')
What is going on here is that we start with an empty accumulator, iterate through the key-value pairs of the Map, create a list that repeats the given key, value times and concat it to the accumulator
The latter is unfortunately a little harder without using something like Shapeless
since in Scala 2 the type information will get lost when converting from a Tuple. You need to do some type juggling with the annotations
val ml = ("a" -> 2, "b" -> 1).productIterator
ml.foldLeft(List[String]())((a, b) => b match{
case (k: String, v : Int) => a ++ List.fill(v)(k)
})
you can do something like this:
Use fill method of GenTraversableFactory
def fill[A](n: Int)(elem: => A): CC[A]
from the definition of fill we can see that it takes an integer and an element. Integer tell how many times we need to fill the given element.
object Demo extends App {
val x = Map("a" -> 2, "b" -> 1)
val p: Seq[String] = x.flatMap { tuple =>
List.fill(tuple._2)(tuple._1)
}.toSeq
print(p)
//output: List(a, a, b)
}
I Hope it helps!!!
If you want to avoid tuple._1 and tuple._1 you can use the following approach.
object Demo extends App {
val x = Map("a" -> 2, "b" -> 1)
val p: Seq[String] = x.flatMap { case (key, value) =>
List.fill(value)(key)
}.toSeq
print(p)
//output: List(a, a, b)
}