Last active
April 25, 2019 20:16
-
-
Save amalloy/8db634bcec85c33c0902e53581e90884 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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