On Wednesday, March 30, 2011 7:29:42 PM UTC+2, Mike Meyer wrote:
>
> On Wed, 30 Mar 2011 10:21:41 -0700 (PDT)
> Jeff Rose <ros...@gmail.com> wrote:
>
> > Hola,
> >   I'm looking for a function that updates a map value by calling a
> > function on it.
> > 
> > (def foo {:a 1})
> > 
> > So instead of this:
> >   (assoc foo :a (inc (:a foo)))            ; => {:a 2}
> > 
> > Something like this:
> >   (map-fn foo :a inc)                        ; => {:a 2}
> > 
> > Does that exist somewhere, or should I be doing this in a different
> > way?  If not, shouldn't it be included?
>
> Possibly you want update-in:
>
> (update-in foo [:a] inc) ; -> {:a 2}
>
> I'm not sure why there's not an "update" that replaces the list of
> keys with a single key.
>
>      <mike
> -- 
> Mike Meyer <m...@mired.org> http://www.mired.org/consulting.html
> Independent Software developer/SCM consultant, email for more information.
>
> O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
>

What if I want to update all values in a map? Is there something faster/more 
idiomatic than this?

(reduce conj {} (map (fn [[k v]] [k (update-value-fn v)]) my-map))

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to