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 <[email protected]>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 [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 > --- > 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 [email protected]. > 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 [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 --- 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 [email protected]. For more options, visit https://groups.google.com/d/optout.
