Hi.
Is there an idiomatic way to have update-in create a vector when the
supplied keys do not already exist? (Or maybe I should use something other
than update-in?)
For example...
This gives me a sequence of things in the reverse of the order I want:
(update-in {} [:foo :bar] conj :a)
;; => {:foo {:bar (:a)}}
(update-in *1 [:foo :bar] conj :b)
;; => {:foo {:bar (:b :a)}}
This does what I want, but at the expense of creating a new vector each
time:
(update-in {} [:foo :bar] (comp vec conj) :a)
;; => {:foo {:bar [:a]}}
(update-in *1 [:foo :bar] (comp vec conj) :b)
;; => {:foo {:bar [:a :b]}}
(I know about laziness and structure sharing, and that this expense may not
be large.)
The following works, but at the expense of a new function that the reader
has to understand:
(defn conj-vec
"Like conj, but returns a vector if coll is empty.
Useful in conjunction with update-in."
[coll x]
(if (empty? coll) [x] (conj coll x)))
(update-in {} [:foo :bar] conj-vec :a)
;; => {:foo {:bar [:a]}}
(update-in *1 [:foo :bar] conj-vec :b)
;; => {:foo {:bar [:a :b]}}
Is there a better way?
--
--
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/groups/opt_out.