score:1

Accepted answer

The problem is that padTo actually fills the list up to a given size. So the first time it works with 4 elements padded, but the next time you'll have to add the actual length of the curent list - hence:

def decompress(tList: List[Tuple2[Int,Any]]): List[Any] = {
  val newList = tList.foldLeft(List[Any]())((b,a) => {
   b.padTo(a._1+b.length, a._2)
 }) 
 newList
}

score:0

This works:

scala> testList.foldLeft(List[Char]()){ case (xs, (count, elem)) => xs ++ List(elem).padTo(count, elem)}
res7: List[Char] = List(a, a, a, a, b, c, c, a, a, d, e, e, e, e)

The problem actually is that when you say b.padTo(padCount, padElement) you use always the same list (b) to fill up the elements. Because the first tuple data generate the most elements nothing is added in the next step of foldLeft. If you change the second tuple data you will see a change:

scala> val testList = List(Tuple2(3, 'a'), Tuple2(4, 'b'))
testList: List[(Int, Char)] = List((3,a), (4,b))

scala> testList.foldLeft(List[Char]()){ case (xs, (count, elem)) => xs.padTo(count, elem)}
res11: List[Char] = List(a, a, a, b)

Instead of foldLeft you can also use flatMap to generate the elements:

scala> testList flatMap { case (count, elem) => List(elem).padTo(count, elem) }
res8: List[Char] = List(a, a, a, a, b, c, c, a, a, d, e, e, e, e)

By the way, Tuple(3, 'a') can be written (3, 'a') or 3 -> 'a'

Note that padTo doesn't work as expected when you have data with a count of <= 0:

scala> List(0 -> 'a') flatMap { case (count, elem) => List(elem).padTo(count, elem) }
res31: List[Char] = List(a)

Thus use the solution mentioned by Garret Hall:

def decompress[A](xs: Seq[(Int, A)]) =
  xs flatMap { case (count, elem) => Seq.fill(count)(elem) }

scala> decompress(List(2 -> 'a', 3 -> 'b', 2 -> 'c', 0 -> 'd'))
res34: Seq[Char] = List(a, a, b, b, b, c, c)

scala> decompress(List(2 -> 0, 3 -> 1, 2 -> 2))
res35: Seq[Int] = List(0, 0, 1, 1, 1, 2, 2)

Using a generic type signature should be referred in order to return always correct type.

score:1

You could do your decompress like this:

val list = List(Tuple2(4, 'a'), Tuple2(1, 'b'), Tuple2(2, 'c'), Tuple2(2, 'a'), Tuple2(1, 'd'), Tuple2(4, 'e'))
list.flatMap{case (times, value) => Seq.fill(times)(value)}

Related Query

More Query from same tag