Skip to content

Instantly share code, notes, and snippets.

@dabd
Last active December 29, 2015 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dabd/7666778 to your computer and use it in GitHub Desktop.
Save dabd/7666778 to your computer and use it in GitHub Desktop.
(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