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 run the code below giving me the desired result.
Is there a smarter way to do this? Possibly a better way through the map function?
var multiset : Seq[T] = Seq[T]()
var variables : Seq[T] = data.map(x => x._1).toSeq
var variableCounts : Seq[Int] = data.map(x => x._2).toSeq
for(x <- 0 until variables.length){
for(y <- 0 until variableCounts(x))
multiset = multiset :+ variables(x)
}
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.I Hope it helps!!!
If you want to avoid tuple._1 and tuple._1 you can use the following approach.