Clojure: Semi-Flattening a nested Sequence

ChucK picture ChucK · Mar 8, 2011 · Viewed 11k times · Source

I have a list with embedded lists of vectors, which looks like:

(([1 2]) ([3 4] [5 6]) ([7 8]))

Which I know is not ideal to work with. I'd like to flatten this to ([1 2] [3 4] [5 6] [7 8]).

flatten doesn't work: it gives me (1 2 3 4 5 6 7 8).

How do I do this? I figure I need to create a new list based on the contents of each list item, not the items, and it's this part I can't find out how to do from the docs.

Answer

nickik picture nickik · Mar 8, 2011

If you only want to flatten it one level you can use concat

(apply concat '(([1 2]) ([3 4] [5 6]) ([7 8])))
=> ([1 2] [3 4] [5 6] [7 8])