So nested calls to update-in are needed in order to be able to provide a
specific default value, everytime just having an empty map created isn't
sufficient.

So instead of (update-in {} [:a :b] identity) which returns {:a {:b nil}} ,
you can break the key path at :a so that the default value if no :a is
specified by (fnil XXX default-value). And by specifying that XXX is
update-in, you can continue your "key path" where you stopped (after :a),
either using the found value at :a, either the provided default value :

(update-in {} [:a] (fnil update-in {:label "great label"}) [:b] identity)
=> {:a {:b nil, :label "great label"}}

or you can also decide that it's not a map which should be the default
value, imagine you want to modify a path [:a 2] :

(update-in {} [:a] (fnil update-in [:foo :bar :baz]) [2] str) => {:a [:foo
:bar ":baz"]}

2010/9/16 Btsai <benny.t...@gmail.com>

> My poor brain can't handle nested calls to update-in, so this is what
> I came up with:
>
> (defn add-meetings [data k meetings]
>  (cond
>   (nil? (data k)) (assoc data k {:title "title" :meetings meetings})
>   :else (update-in data [k :meetings] concat meetings)))
>
> On Sep 16, 8:53 am, Laurent PETIT <laurent.pe...@gmail.com> wrote:
> > 2010/9/16 Meikel Brandmeyer <m...@kotka.de>
> >
> > > Hi Laurent,
> >
> > > On 16 Sep., 15:54, Laurent PETIT <laurent.pe...@gmail.com> wrote:
> >
> > > > you don't like my one-liner ? :-)
> >
> > > I saw your message only after I sent mine. :)
> >
> > > > (update-in coll [k] (fnil update-in *default-value*) [:meetings]
> (comp
> > > vec
> > > > concat) data)
> >
> > > Hmm... (comp vec concat) == into?
> >
> > Yep.
> > so this is the killer one : :-)
> >
> > (update-in coll [k] (fnil update-in *default-value*) [:meetings] into
> data)
>
> --
> 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<clojure%2bunsubscr...@googlegroups.com>
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>

-- 
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