I'm sorry, I've not followed this discussion - what is wrong with
user=>(apply str (interpose \, (list 1 2 3 4 5)))
"1,2,3,4,5"
the problem is strings where you want to preserve each string...you can
special case that and avoid the (apply str...) bit..
user=> (interpose \, (list "jim" "jon" "chris"))
("jim" \, "jon" \, "chris")...
once you have that pr-str is your friend...
=>(apply pr-str (interpose \, (list "jim" "jon" "chris")))
"\"jim\" \\, \"he\" \\, \"dan\""
Is this not exactly what you want and fast enough?
Jim
On 02/04/13 23:49, Max Penet wrote:
Using a protocol fn to do the encoding of the values according to the
rules you set per type then using clj.string/join should be quite fast
and not so horrible.
On Thursday, October 28, 2010 2:18:08 AM UTC+2, andrei wrote:
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
---
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.