[CLJ-1251] The update function: like update-in but allows multiple (shallow) updates at once. Created: 03/Sep/13 Updated: 14/Feb/14
|Affects Version/s:||Release 1.6|
|Reporter:||Michael O. Church||Assignee:||Unassigned|
|Patch:||Code and Test|
Like update-in, update allows functions to be applied to specified keys in an associative data structure. It does not support updates deeper than 1 level, and does not require a seq or vector of keys to be specified.
Unlike update-in, update allows multiple fields to be updated in one call, e.g.:
(def heal-spell [creature] (update creature :HP #(+ % 100) :MP #(- % 8) :spells-cast inc))
This is usually one of the first things I find myself adding to the language on my own projects, so I thought it'd be worth submitting. Apologies if this has already been discussed before.
|Comment by Alex Miller [ 06/Sep/13 9:56 AM ]|
I like this - kind of halfway between assoc and update-in.
|Comment by Michael O. Church [ 07/Sep/13 12:41 PM ]|
It's very useful. I assumed that its non-inclusion was for a reason (hence was hesitant to submit the patch) but it comes in handy a lot. One project I'd like to do with some free time is a library for turn-based strategy games, which use update frequently to express game-state changes.
The downside of this change is that 'update is probably a defined function in a good number of modules written by other people. IMO the strongest reason not to include it is that it's such a common name; but the benefits (in my view) outweigh the downsides.
|Comment by Andy Fingerhut [ 14/Feb/14 11:50 AM ]|
Patch update.patch dated Sep 3 2013 no longer applies cleanly to latest Clojure master as of Feb 14 2014. It did on Feb 7 2014. I haven't checked in detail, but this is probably simply due to some tests recently added to a test file that require updating some diff context lines.