(defn update-in*
  [m [k & ks] f & args]
  (let [k (if (and (instance? clojure.lang.Indexed m)
                   (integer? k)
                   (neg? k))
            (+ (count m) k)
            k)]
    (if ks
        (assoc m k (apply update-in* (get m k) ks f args))
      (assoc m k (apply f (get m k) args)))))


(update-in* [1 [1 2 [1 2 3]]] [-1 -1 -1] (constantly 5))

https://gist.github.com/wagjo/9813500


On Thu, Mar 27, 2014 at 6:17 PM, Mark Engelberg <mark.engelb...@gmail.com>wrote:

> Or you could write your own custom version of update-in that recognizes
> the keyword ::last to have a special meaning, so you could do:
>
> (update-in [1 2 ::last 1 ::last] ...)
>
>
> On Thu, Mar 27, 2014 at 8:57 AM, Jozef Wagner <jozef.wag...@gmail.com>wrote:
>
>> Calling count on vector is a constant time operation, so I think (dec
>> (count v)) is an acceptable and even idiomatic in this case. Make it a
>> function if it is too verbose for you.
>>
>> (let [assoc-last #(assoc % (dec (count %)) %2) ] (update-in [1 [1 2 [1 2
>> 3]]] [1 2] #(assoc-last % 5)))
>>
>> Jozef
>>
>>
>> On Thu, Mar 27, 2014 at 3:49 PM, Andy Smith <the4thamig...@googlemail.com
>> > wrote:
>>
>>> Hi,
>>>
>>> (1)
>>> I'm sure there must be a simpler way to update the last element of a
>>> vector other than dec and count :
>>>
>>> (let [xs [1 2 3]] (assoc xs (dec (count xs)) 4))
>>>
>>> This gets messy when updating vectors in nested data structures :
>>>
>>> (update-in [1 [1 2 [1 2 3]]] [1 2] #(assoc % (dec (count %)) 5))
>>>
>>> So, what am I doing wrong?
>>>
>>> P.S. I know I could reverse the order of the data in the lists (i.e.
>>> probably changing to lists rather than vectors for conj-ing purposes), but
>>> im interested in vectors specifically.
>>>
>>> Thanks
>>>
>>> Andy
>>>
>>> --
>>> 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
>>> ---
>>> You received this message because you are subscribed to the Google
>>> Groups "Clojure" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to clojure+unsubscr...@googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>  --
>> 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
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to clojure+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>  --
> 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
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to