Both of the below functions return the same output, but the first one uses reduce and a pair of quote marks to achieve the same thing. I played with this code and if you delete the ""
marks, the first character in the sequence is not converted from it's integer value. The second one seems clear to me, but I feel that the first way is superior, because it packages all of the desired effects into a single function literal. Could somebody unpack the anonymous function for me to explain what variables %
and %2
are referring to and tell me why we need the ""
only for the first integer (115
) in the vector?
(reduce #(str % (char %2)) ""
[115 101 99 114 101 116 32 109 101 115 115 97 103 101 115])
(apply str (map #(char %) [115 101 99 114 101 116 32 109 101 115 115 97 103 101 115]))
The first function was taken from the Clojure Cookbook discussion on int
to str
conversion, here.