Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turning a list of strings into a single string in clojure

Tags:

clojure

For example, let's say I have a list

'("The" " " "Brown" " " "Cow")

I want to turn this into

"The Brown Cow" 

Is there a command in clojure that does this?

like image 268
JCD Avatar asked Mar 09 '16 06:03

JCD


1 Answers

I would rather use apply for that:

(apply str '("The" " " "Brown" " " "Cow"))

Since it calls the function just once, it is much more efficient for large collections:

(defn join-reduce [strings] (reduce str strings))
(defn join-apply [strings] (apply str strings))

user> (time (do (join-reduce (repeat 50000 "hello"))
                nil))
"Elapsed time: 4708.637673 msecs"
nil
user> (time (do (join-apply (repeat 50000 "hello"))
                nil))
"Elapsed time: 2.281443 msecs"
nil
like image 176
leetwinski Avatar answered Nov 13 '22 14:11

leetwinski