Skip to content

Instantly share code, notes, and snippets.

@gfredericks
Created July 15, 2011 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gfredericks/1083792 to your computer and use it in GitHub Desktop.
Save gfredericks/1083792 to your computer and use it in GitHub Desktop.
; user=> (flat [[[[[4 55] 6] [6 6 [5 4]]]]])
; [4 55 6 6 6 5 4]
(defn flat
[coll]
(loop [res [], coll coll, stack ()]
(if (empty? coll)
(if (empty? stack)
res
(recur res (first stack) (rest stack)))
(let [[x & xs] coll]
(if (coll? x)
(recur res x (cons xs stack))
(recur (conj res x) xs stack))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment