Hi,

2013/4/5 Simon Katz <nomisk...@gmail.com>:
> 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)}}

I think you want fnil:

;; Clojure 1.5.1

=> (update-in {} [:foo :bar] conj :b)
{:foo {:bar (:b)}}

=> (update-in {} [:foo :bar] (fnil conj []) :b)
{:foo {:bar [:b]}}

=> (require 'clojure.repl)
nil
=> (clojure.repl/doc fnil)
-------------------------
clojure.core/fnil
([f x] [f x y] [f x y z])
  Takes a function f, and returns a function that calls f, replacing
  a nil first argument to f with the supplied value x. Higher arity
  versions can replace arguments in the second and third
  positions (y, z). Note that the function f can take any number of
  arguments, not just the one(s) being nil-patched.
nil



>
> 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 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/groups/opt_out.
>
>

-- 
-- 
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/groups/opt_out.


Reply via email to