Skip to content

Instantly share code, notes, and snippets.

@JonathanGTH
Created August 8, 2016 06:18
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 JonathanGTH/a29c196a4365555ae8b00d12821f021b to your computer and use it in GitHub Desktop.
Save JonathanGTH/a29c196a4365555ae8b00d12821f021b to your computer and use it in GitHub Desktop.
Flatten Arbitrary Arrays in Order
(ns citus-byte)
(defn flatten2
{:static true}
[x]
(letfn [(flat [coll]
(lazy-seq
(when-let [c (seq coll)]
(let [x (first c)]
(if (sequential? x)
(concat (flat x) (flat (rest c)))
(cons x (flat (rest c))))))))]
(if (sequential? x) (flat x) x)))
;; Testing as (flatten2 [[1,2,[3]],4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment