Created
May 31, 2015 14:49
-
-
Save jarsen/1a4cfdf547a201243b15 to your computer and use it in GitHub Desktop.
Dictionary Update
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
extension Dictionary { | |
mutating func update(key: Key, initial: Value, update:Value->Value) { | |
if let x = self[key] { | |
self[key] = update(x) | |
} | |
else { | |
self[key] = initial | |
} | |
} | |
} | |
var foo = ["a":1, "b":2, "c":3] | |
foo.update("b", initial: 1) { x in x + 1 } | |
foo.update("d", initial: 1) { x in x + 1 } | |
foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment