Skip to content

Instantly share code, notes, and snippets.

@amaya382
Created March 17, 2016 17:40
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 amaya382/11d0f86e43250dabe548 to your computer and use it in GitHub Desktop.
Save amaya382/11d0f86e43250dabe548 to your computer and use it in GitHub Desktop.
upsert
implicit class RichMutableMap[K, V](val m: scala.collection.mutable.Map[K, V]) extends AnyVal {
def upsert(key: K, insertValue: => V, updateValue: => V => V = null): scala.collection.mutable.Map[K, V] = {
if (m.contains(key) && updateValue != null)
m.update(key, updateValue(m(key)))
else
m.update(key, insertValue)
m
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment