Last active
December 15, 2015 22:29
-
-
Save ckirkendall/5333323 to your computer and use it in GitHub Desktop.
Two way relationship with immutable data structure.
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
(defprotocol XmlNode | |
(get-parent [_]) | |
(get-children [_])) | |
(def empty-node | |
(reify XmlNode | |
(get-parent [_] nil) | |
(get-children [_] '()))) | |
(defn- reasign [parent children] | |
(map #(reify XmlNode | |
(get-parent [_] parent) | |
(get-children [this] (reasign this (get-children %)))) | |
children)) | |
(defn add-child [parent child] | |
(reify XmlNode | |
(get-parent [_] (get-parent parent)) | |
(get-children [this] (reasign this (concat (get-children parent) [child]))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment