Fork me on GitHub
#beginners
<
2022-07-31
>
Kamuela16:07:27

If I'm trying to map over a range, and build up a single string with the result, is that the job of a reduce?

sheluchin16:07:16

Yes. Most generally reduce is for deriving a value from a sequence.

Martin Půda16:07:34

Can you write the exact problem and expected result?

Kamuela16:07:27

(apply str (map generate-string-with-number (range start end -1))) is what I ended up with

Kamuela16:07:18

generate-string-with-number 2 -> "I am string number 2."

Kamuela16:07:32

The aforementioned resulting in a single string

Martin Půda17:07:10

I think the best solution is ->> map , clojure.string/join :

(->> (range 10 5 -1)
     (map #(str "I am string number " % "."))
     (s/join " "))
You can do something like that with reduce, if you don't mind missing space between sentences (or there will be one unwanted space):
(->> (range 10 5 -1)
     (reduce #(str %1 "I am string number " %2 ".") ""))
And one more with clojure.pprint/cl-format:
(pp/cl-format nil "~{I am string number ~a.~^ ~}" (range 10 5 -1))