Skip to content

Instantly share code, notes, and snippets.

@pulsation
Last active December 22, 2015 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pulsation/6524135 to your computer and use it in GitHub Desktop.
Save pulsation/6524135 to your computer and use it in GitHub Desktop.
Accumulate hash values using Scala
val myHashList = List("at work" -> 7, "at home" -> 5, "at david's" -> 3)
val myNewValue = "at home" -> 1
myHashList :+ myNewValue groupBy (_._1) map {case (k, v) => (k, v.map(_._2).sum)} toList
// List(at home -> 6, at work -> 7, at david's -> 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment