Hi all,

I work with a database and need a function, that will convert Clojure
sequence to a string with a comma-separated elements. E.g.,

  (comma-separated (list 1 2 3 4 5))  ==>  "1, 2, 3, 4, 5"

It must work with any data type - ints, strings, other lists, etc.
E.g. for a list of strings it must generate

  (comma-separated (list "1" "2" "3" "4" "5"))  ==>  "\"1\", \"2\",
\"3\", \"4\", \"5\""

I tried `cl-format` function from clojure.contrib.pprint package:

  (cl-format nil "~{~S~^, ~}" lst)

It works fine, but is too slow. Next, I tried such function:

  (defn comma-separated [s]
      (if (= (type (first s)) String)
         (chop (chop (chop (str "\" (apply str (interleave s
(repeatedly (fn [] "\", \"")))))))
         (chop (chop (apply str (interleave s (repeatedly (fn [] ",
")))))))))

This one works much faster (~20 times), but it is ugly and still
doesn't cover all cases.

So, what is the fastest way to generate such list?

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