Skip to content

Instantly share code, notes, and snippets.

@ckirkendall
Last active December 15, 2015 22:29
Show Gist options
  • Save ckirkendall/5333323 to your computer and use it in GitHub Desktop.
Save ckirkendall/5333323 to your computer and use it in GitHub Desktop.
Two way relationship with immutable data structure.
(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