Skip to content

Instantly share code, notes, and snippets.

@behaghel
Created July 16, 2013 00:20
Show Gist options
  • Save behaghel/6004735 to your computer and use it in GitHub Desktop.
Save behaghel/6004735 to your computer and use it in GitHub Desktop.
implicit class RichMap[K, V](m: Map[K, V]) {
def updateValue(k: K)(f: Option[V] => V): Map[K, V] = m + (k -> f(m get k))
def incValue(k: K)(implicit ev: Numeric[V]): Map[K, V] =
updateValue(k){ o =>
val one = ev.fromInt(1)
o map { ev.plus(_, one) } getOrElse one
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment