Instead of apply you can use reduce like this:
user> (reduce #(str %1 " line=" %2) "" (range 1 5)) => " line=1 line=2 line=3 line=4"
The reduce function is a function that takes a function (let it be called if f ), the "initial value", and then a list of things that will be used as the second argument to e . He lazily calls f the starting value and the first element in the list, then calls f that it returns, and the second element in the list, and then calls f by what it returns, and the third element in the list, etc., while it I havenβt exhausted all the elements in the list (more precisely - since he is lazy, he will go through the entire list only if you ask this to ").
If you don't like the startup space, you can wrap it all in triml (you would have to do (use 'clojure.string) first). Or you could do (reduce #(str %1 "line=" %2 " ") (range 1 5)) that would put space at the end.
My experience is that at any time you can do something with apply , you can do it a bit elegantly with reduce . More importantly, my reduce alternative was always , as a rule, faster than my apply . Of course, I cannot guarantee that this will always be true, and I did not do speed tests for your specific problem.
Edit
I made some rough timings using my ( reduce ) version compared to the second JohnJ ( apply ) sentence and found that they were pretty similar to (range 1 100) , but in (range 1 500) apply version was at least 4 times faster.
source share