4

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 Answer 1

4

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"]]))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!! I knew there must have been an elegant solution I was missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.