Created
March 17, 2016 17:40
-
-
Save amaya382/11d0f86e43250dabe548 to your computer and use it in GitHub Desktop.
upsert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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