sectornitad
sectornitad

Reputation: 981

Concatenate a list of vectors of strings in clojure

If I have:

(["aa=AA"] ["&"] ["bb=BB"] ["&"] ["cc=CC"])

how can I get:

"aa=AA&bb=BB&cc=CC"

is there a concatenate function available?

Upvotes: 0

Views: 556

Answers (3)

leetwinski
leetwinski

Reputation: 17859

just for fun: you can also use map function's behaviour for this

user> (defn conc [items] (first (apply map str items)))
#'user/conc

user> (conc '(["aa=AA"] ["&"] ["bb=BB"] ["&"] ["cc=CC"]))
"aa=AA&bb=BB&cc=CC"

Upvotes: 1

Piotrek Bzdyl
Piotrek Bzdyl

Reputation: 13175

concat will "flatten" your nested sequence at one level:

(apply concat '(["aa=AA"] ["&"] ["bb=BB"] ["&"] ["cc=CC"]))
;; => ("aa=AA" "&" "bb=BB" "&" "cc=CC")

Then you can use str to concatenate the strings from the sequence:

(apply str '("aa=AA" "&" "bb=BB" "&" "cc=CC"))
;; => "aa=AA&bb=BB&cc=CC"

Combined into a function:

(defn concat-str [s]
  (->> s
    (apply concat)
    (apply str)))

(concat-str '(["aa=AA"] ["&"] ["bb=BB"] ["&"] ["cc=CC"]))
;; => "aa=AA&bb=BB&cc=CC"

Upvotes: 3

OlegTheCat
OlegTheCat

Reputation: 4513

You may implement it as follows:

(def concatenate (comp (partial apply str) flatten))

and then:

user> (concatenate '(["aa=AA"] ["&"] ["bb=BB"] ["&"] ["cc=CC"]))
aa=AA&bb=BB&cc=CC

Upvotes: 1

Related Questions