On Feb 19, 6:23 am, Chouser <chou...@gmail.com> wrote:

> In Clojure, if you could use conj on a vector but instead use
> concat on a list, you'll end up with code that is both
> non-idiomatic and runs slower than necessary.

I found the exercise of doing the equivalent with Clojure vectors
pretty challenging.  Given the following use case for CL append:

CL-USER> (append '(a) '() '(b (c)) '(d))
(A B (C) D)

The best I could do with Clojure vectors is this:

(defn join-vecs [v1 v2]
  (let [v2len (count v2)]
    (cond
     (zero? v2len) v1
     (= v2len 1) (conj v1 (first v2))
     true (recur (conj v1 (first v2)) (rest v2)))))

(defn append [& vecs] (reduce join-vecs [] vecs))

user> (append '[a] '[] '[b [c]] '[d])
[a b [c] d]

Is there a better / more idiomatic / more efficient way?

   Thanks,
   Mike

P.S.  I recently got the MEAP of "The Joy of Clojure" and, aside from
a few rough edges in the copy editing, think the content is
excellent!  Looking forward to learning more.

-- 
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

Reply via email to