And note that the pattern works at any level, and is easily readable, since
update-in "flattens" the arguments of the modifying function :

If you want to "touch" the path [:a :b :c :d] and provide specific default
values at each level if the key is not found, it's as "simple" as :

(update-in coll
  [:a] (fnil update-in default-for-first-level)
  [:b] (fnil update-in default-for-second-level)
  [:c] (fnil update-in default-for-third-level)
  [:d] (fnil update-in default-for-fourth-level)
  the-function-you-wanted-to-use-in-the-first-place
  arg2-for-the-function
  arg3-for-the-function ...)



2010/9/16 Laurent PETIT <laurent.pe...@gmail.com>

> 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