Clojure: Semi-Flattening a nested Sequence

后端 未结 5 730
走了就别回头了
走了就别回头了 2020-12-04 18:59

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 lik

5条回答
  •  我在风中等你
    2020-12-04 20:02

    Here's a function that will flatten down to the sequence level, regardless of uneven nesting:

    (fn flt [s] (mapcat #(if (every? coll? %) (flt %) (list %)) s))
    

    So if your original sequence was:

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

    You'd still get the same result:

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

提交回复
热议问题