Created
February 26, 2013 21:58
-
-
Save joelittlejohn/5042641 to your computer and use it in GitHub Desktop.
BrisFunctional zipper
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
(ns brisfunctional-zipper.core) | |
(def sample | |
[:+ [:* 5 6] [:* 7 4] [:+ 1 2]]) | |
(defn loc [l r c p] | |
{:left l | |
:right r | |
:cur c | |
:path p}) | |
(defn edit [t] | |
(loc nil (rest t) (first t) nil)) | |
(defn left [{:keys [left right cur path]}] | |
(loc (rest left) (cons cur right) (first left) path)) | |
(defn right [{:keys [left right cur path]}] | |
(loc (cons cur left) (rest right) (first right) path)) | |
(defn down [{:keys [left right cur path]}] | |
(loc nil (rest cur) (first cur) (cons [left right] path))) | |
(defn up [{:keys [left right cur path]}] | |
(let [[pleft pright] (first path) | |
newcur (concat (reverse left) (cons cur right))] | |
(loc pleft pright newcur (rest path)))) | |
(defn update [f {:keys [left right cur path]}] | |
(loc left right (f cur) path)) | |
(defn top [{:keys [left right cur path] :as node}] | |
(if (seq path) | |
(recur (up node)) | |
node)) | |
(defn out [loc] | |
(-> loc top up :cur)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The only note I'll make is that we really should have defined edit as follows:
(loc nil nil t nil)
... which makes 'out' look a little more sane (no superfluous 'up').