Last active
February 17, 2019 23:17
-
-
Save dnaeon/5e4f02a18a6f5e48bdb5925a60c48be8 to your computer and use it in GitHub Desktop.
clojure-make-nested-map
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
;; | |
;; Making a deeply nested map by following parent-child relations between items | |
;; | |
(defn make-items [n] | |
(concat [{:id 0 :name "item-root"}] | |
(for [x (range 1 n)] {:id x :parent (dec x) :item (format "item-%d" x)}))) | |
(defn item-children [item coll] | |
(if-let [children (-> (filter #(= (:parent %) (:id item)) coll) seq)] | |
(map #(assoc item :children (item-children % coll)) children) | |
item)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example with just a few items.
Now with more items.