i completely agree about adding update to core. i've added it my utils
library and i use it all the time.
here's a slightly different implementation of update that takes a variable
number of args (like update-in).
(defn update
"Update value in map where f is a function that takes the old value and
the
supplied args and returns the new value. Leave map unchanged if value is
unchanged."
[map key f & args]
(let [old (get map key)
new (apply f old args)]
(if (= old new) map (assoc map key new))))
it also leaves the map unchanged if the value is unchanged, though this is
different from update-in, so it
may or may not be a good addition. it is useful for leaving a map unmodified
in the case of of nils. e.g:
(update {} :foo #(when % (inc %))) ;; {}
(update {:foo 2} :foo #(when % (inc %))) ;; {:foo 3}
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en