Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregate list values in Scala

Starting with a list of objects containing two parameters notional and currency, how can I aggregate the total notional per currency?

Given:

case class Trade(name: String, amount: Int, currency: String)

val trades = List(
  Trade("T150310", 10000000, "GBP"),
  Trade("T150311", 10000000, "JPY"),
  Trade("T150312", 10000000, "USD"),
  Trade("T150313", 100, "JPY"),
  Trade("T150314", 1000, "GBP"),
  Trade("T150315", 10000, "USD")
)

How can I get:

Map(JPY -> 10000100, USD -> 10010000, GBP -> 10001000)
like image 520
parkr Avatar asked Jun 21 '09 13:06

parkr


2 Answers

Starting Scala 2.13, most collections are provided with the groupMapReduce method which is (as its name suggests) an equivalent (more efficient) of a groupBy followed by mapValues and a reduce step:

trades.groupMapReduce(_.currency)(_.amount)(_ + _)
// immutable.Map[String,Int] = Map(JPY -> 10000100, USD -> 10010000, GBP -> 10001000)

This:

  • groups elements based on their currency (group part of groupMapReduce)

  • maps grouped values to their amount (map part of groupMapReduce)

  • reduces values (_ + _) by summing them (reduce part of groupMapReduce).

This is an equivalent version performed in one pass through the List of:

trades.groupBy(_.currency).mapValues(_.map(_.amount).reduce(_+_))
like image 160
Xavier Guihot Avatar answered Oct 03 '22 15:10

Xavier Guihot


If you use trunk the machinery is already there. groupBy is defined on Traversable and sum can be applied directly to the list, you don't have to write a fold.

scala> trades groupBy (_.currency) map { case (k,v) => k -> (v map (_.amount) sum) }
res1: Iterable[(String, Int)] = List((GBP,10001000), (JPY,10000100), (USD,10010000))
like image 38
psp Avatar answered Oct 03 '22 16:10

psp