Skip to content

Instantly share code, notes, and snippets.

@amalloy
Last active April 25, 2019 20:16
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 amalloy/8db634bcec85c33c0902e53581e90884 to your computer and use it in GitHub Desktop.
Save amalloy/8db634bcec85c33c0902e53581e90884 to your computer and use it in GitHub Desktop.
(defn stratify [coll]
(:ret ((fn step [coll]
(loop [ret [], coll coll]
(if (empty? coll)
{:ret ret :remainder nil}
(let [x (first coll)
[same different] (split-with #(= % x) coll)
so-far (into ret same)
pending (seq different)]
(cond (nil? pending) {:ret so-far :remainder nil}
(< (first different) x) {:ret so-far :remainder pending}
:else ;; nest another level
,,(let [{:keys [ret remainder]} (step pending)]
(recur (conj so-far ret) remainder)))))))
coll)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment