On Thu, Dec 16, 2010 at 1:33 AM, Sunil S Nandihalli
<[email protected]> wrote:
> Hello everybody,
> I would like to write regular code something like
> [1 2 ~@(map some-fn some-coll) 4 5]
> is this possible?
(vec (concat [1 2] (map some-fn some-coll) [4 5]))
will do it.
A tidier form would be:
user=> (vecify 1 2 (map #(* % %) (range 3)) 4 5)
[1 2 0 1 4 4 5]
which this enables:
(defn vecify [& stuff]
(loop [v [] s (seq stuff)]
(if s
(let [f (first s)]
(if (coll? f)
(recur (into v f) (next s))
(recur (conj v f) (next s))))
v)))
with the caveat that it assumes you want to splice any coll in the
arglist. If you want to include a coll in the arglist as a single
element of the output vector you can wrap it in []. A more DSLey
variant:
(defn vecify [& stuff]
(loop [v [] s (seq stuff) splice false]
(if s
(let [f (first s)]
(if (coll? f)
(if splice
(recur (into v f) (next s) false)
(recur (conj v f) (next s) false))
(if (= f :splice)
(recur v (next s) true)
(recur (conj v f) (next s) false))))
v)))
user=> (vecify 1 2 :splice (map #(* % %) (range 3)) :k [0 8] 4 5)
[1 2 0 1 4 :k [0 8] 4 5]
This one requires you to use :splice [:splice] instead of just :splice
in the arglist to get the literal keyword :splice in the vector.
Otherwise :splice splices the following coll in (and is ignored
entirely if what follows isn't a coll); otherwise the coll is inserted
as a single element. This comes closest to unquote-splicing's
behavior.
--
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