what is the idiomatic way to write this clojure snippet to concat collection with a string?

StackOverflow https://stackoverflow.com/questions/19085488

  •  29-06-2022
  •  | 
  •  

Question

Its best to explain what I am trying using an example.

Given a collection ["apple" "orange" "banana"] and concatenation string "," function should produce "apple,orange,banana"

Is this the idiomatic way to write this function?

user=> (defn my-concat[x st]
   (str (first x) (apply str (map #(str st %) (rest x)))))

user=> (my-concat "abcd" "!")
"a!b!c!d"
Was it helpful?

Solution

You can use the interpose function if you want a sequence, or clojure.string/join if you just want the string result.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top