sebi
sebi

Reputation: 1841

Sequence transformations in scala

In scala, is there a simple way of transforming this sequence

Seq(("a", 1), ("b", 2), ("a", 3), ("c", 4), ("b", 5))

into this Seq(("a", 4), ("b", 7), ("c", 4))?

Thanks

Upvotes: 1

Views: 479

Answers (2)

user-asterix
user-asterix

Reputation: 936

Here is another way by unzipping and using the second item in the tuple.

val sq = sqSeq(("a", 1), ("b", 2), ("a", 3), ("c", 4), ("b", 5))
sq.groupBy(_._1)
  .transform {(k,lt) => lt.unzip._2.sum}
  .toSeq

The above code details:

scala> sq.groupBy(_._1)
res01: scala.collection.immutable.Map[String,Seq[(String, Int)]] = Map(b -> List((b,2), (b,5)), a -> List((a,1), (a,3)), c -> List((c,4)))

scala> sq.groupBy(_._1).transform {(k,lt) => lt.unzip._2.sum}
res02: scala.collection.immutable.Map[String,Int] = Map(b -> 7, a -> 4, c -> 4)


scala> sq.groupBy(_._1).transform {(k,lt) => lt.unzip._2.sum}.toSeq
res03: Seq[(String, Int)] = ArrayBuffer((b,7), (a,4), (c,4))

Upvotes: 0

Michael Zajac
Michael Zajac

Reputation: 55569

I'm not sure if you meant to have Strings in the second ordinate of the tuple. Assuming Seq[(String, Int)], you can use groupBy to group the elements by the first ordinate:

Seq(("a", 1), ("b", 2), ("a", 3), ("c", 4), ("b", 5))
   .groupBy(_._1)
   .mapValues(_.map(_._2).sum)
   .toSeq

Otherwise, you'll next an extra .toInt

Upvotes: 5

Related Questions