functional programming - Scala combine Seq of Timestamps -
hello i'm learning scala , have seq of object of following case class:
case class historycounter(time: datetime, counter:int)
now combine value of counter
based on timestamp time
. sum of counters time
on same day or same week.
how can this? 1 of collection functions fold or map capable of doing this?
if had list
of these case classes called list
, 1 way so:
val counts = list.groupby(_.time).map{ case (k, v) => (k, v.map(_.counter).sum) }
or more succinctly:
list.groupby(_.time).mapvalues(_.map(_.counter).sum)
Comments
Post a Comment