4
votes

I have a vector of the form [ ["H"] ["B"] ["ER"] ["W"] ] and I want a vector of the form [ ["H"] ["B"] ["E"] ["R"] ["W"] ] with the E and the R naturally separated.

I'm quite familiar with map (and reduce) and have been using them a lot but for some reason I can't think of a way to do this easily using map.

Can map produce two elements or more for each input it receives from a sequence? If so how?

1

1 Answers

4
votes

mapcat is what you’re looking for.

With mapcat you return a collection for each input element. The collections are concatenated into the result. For example:

(vec
  (mapcat #(map (comp vector str) (first %))
          [["H"] ["B"] ["ER"] ["W"]]))