Chris Edwards
Chris Edwards

Reputation: 1558

Clojure concat 2d elements to the same String

I have data in the following format:

[[123][124][125][126][127]]

And I ultimately want to produce a String which will look like

123,
124,
125,
126,
127

So I need to iterate the vector, apply ",\n" to the elements and then remove the ",\n" from the last one (or dont even add it in the first place)

I have tried the following (data is the [[123][124][125][126][127]] vector):

(mapv (fn [inner] (mapv #(str  % ",\n") inner)) data)

Which produces:

[[123,
] [124,
] [125,
] [126,
] [127,
]]

I dont really want to do a replace statement to remove [] from the String, but then again I'm stumped on how to convert the data to a better format before mapping.

Any ideas?

Upvotes: 0

Views: 53

Answers (2)

slipset
slipset

Reputation: 3078

user>(def data [[123][124][125][126][127]])
;; => #'user/data
user> (clojure.string/join ",\n" (flatten data))
;; => "123,
124,
125,
126,
127"

Upvotes: 4

Mosho
Mosho

Reputation: 7078

try this:

(clojure.string/join ",\n" (mapv #(clojure.string/join ",\n" %) data))

Upvotes: 0

Related Questions