Last active
June 10, 2020 12:02
-
-
Save teesloane/0030eb118d80504c955ad71be16bf791 to your computer and use it in GitHub Desktop.
This file contains 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
(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