Last active
December 29, 2015 11:49
-
-
Save dabd/7666778 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
(ns testzip.core | |
(:require [clojure.zip :as z])) | |
(defn make-node [n children] | |
(cons (first n) children)) | |
(defn tree-zipper [v] | |
(z/zipper vector? next make-node v)) | |
(defn node-value | |
[node] | |
(if (vector? node) | |
(first node) | |
node)) | |
(defn tree-edit | |
[tree editor] | |
(loop [loc tree] | |
(if (z/end? loc) | |
(z/root loc) | |
(recur (z/next (editor loc)))))) | |
(defn test-tree-zipper | |
[] | |
(let [tree (tree-zipper [1 2 [3 4 5]])] | |
(tree-edit tree (fn [loc] | |
(z/edit loc (fn [n & args] | |
(if (z/branch? loc) | |
(z/make-node loc (if (and (number? (node-value n)) (odd? (node-value n))) | |
(conj (next n) (* 2 (node-value n))) | |
n) | |
(z/children loc)) | |
(if (odd? n) | |
(* 2 n) | |
n)))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment