Skip to content

Instantly share code, notes, and snippets.

@teesloane
Last active June 10, 2020 12:02
Show Gist options
  • Save teesloane/0030eb118d80504c955ad71be16bf791 to your computer and use it in GitHub Desktop.
Save teesloane/0030eb118d80504c955ad71be16bf791 to your computer and use it in GitHub Desktop.
(def input
[{:level 1 :raw "foo"}
{:level 1 :raw "bar"}
{:level 2 :raw "yoa"}
{:level 2 :raw "yyyoa"}
{:level 3 :raw "foobarra"}
{:level 1 :raw "job"}])
(defn the-evil
[items prev-item out]
(if (empty? items)
out
(let [x (first items)
xs (rest items)]
(if (= prev-item nil)
(the-evil xs x (conj out [:li (x :raw)]))
(if (> (:level x) (prev-item :level))
(conj out [:ul (the-evil items nil [])])
(the-evil xs x (conj out [:li (x :raw)]))))))) ;; so close to getting this thing working. And I'm not even supposed to use this kind of recursion?
(the-evil input nil [])
;; output:
;; => [[:li "foo"]
;; [:li "bar"]
;; [:ul [[:li "yoa"] [:li "yyyoa"] [:ul [[:li "foobarra"] [:li "job"]]]]]]
;; expected output:
(comment
[[:li "foo"]
[:li "bar"]
[:ul
[:li "yoa"]
[:li "yyyoa"]
[:ul [:li "foobarra"]]]
[:li "job"]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment