3 ms·
My favorite (although not the most efficient) Clojure version of this is: (defn flat [s] (if (sequential? s) (apply concat (map flat s))
by fogus 16y ago
My favorite (although not the most efficient) Clojure version of this is:
(defn flat [s]
(if (sequential? s)
(apply concat (map flat s))
(list s)))
(flat '((((1 2 3)) (4 5)) (((((((6 7 [8 9])))))) a b c) {:a 1 :b 2}))
;=> (1 2 3 4 5 6 7 8 9 a b c {:a 1, :b 2})
- lispm 16y agoCommon Lisp has MAPCAN: (defun flatten (list) (mapcan (lambda (item) (if (listp item) (flatten item) (list item))) list))