Skip to content

Commit

Permalink
v0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
conjunctive committed Apr 17, 2022
0 parents commit fbf60e7
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.cpcache
.nrepl-port
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Conjunctive and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 5 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
* Zipper-Encoding
Various utilities for working with zippers.

** License
This project is licensed under the MIT License.
1 change: 1 addition & 0 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:paths ["src"]}
80 changes: 80 additions & 0 deletions src/zipper_encoding/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
(ns zipper-encoding.core
"Utilities for working with zippers."
(:require [clojure.spec.alpha :as s]
[clojure.zip :as z]))

(defn leaf?
"Is the `zipper` node at the current location of a leaf?"
{:inline (fn [zipper] `(not (z/branch? ~zipper)))}
[zipper]
(not (z/branch? zipper)))

(defn next-leaf
"Move to the next location of a leaf node in the hierarchy, depth-first.
Do nothing if the current location represents the end of a depth-first walk."
[zipper]
(when-not (z/end? zipper)
(let [next-zipper (z/next zipper)]
(if (z/branch? next-zipper)
(recur next-zipper)
next-zipper))))

(defn validate-tree-with
"Breadth-first validation of a `zipper` tree,
checking that each node is `valid?` under the predicate."
([valid? zipper]
(let [zippers (into (clojure.lang.PersistentQueue/EMPTY) [zipper])]
(validate-tree-with valid? zippers [])))
([valid? zippers visited]
(if-let [zipper (peek zippers)]
(let [node (z/node zipper)]
(if (valid? node)
(recur valid?
(into (pop zippers)
(some->> zipper
(z/down)
(iterate z/right)
(take-while identity)))
(conj visited node))
{:valid? false
:node node
:visited visited}))
{:valid? true
:node nil
:visited visited})))

(defn validate-tree-with-spec
"Breadth-first validation of a `zipper` tree,
checking that each node is valid for `spec`."
[spec zipper]
(validate-tree-with (fn valid? [node]
(s/valid? spec node))
zipper))

(defn all-children-valid?
"Is the `zipper` node at the current location of a branch node,
where all of the child nodes are deemed `valid?`?"
[valid? zipper]
(and (z/branch? zipper)
(every? valid?
(z/children zipper))))

(defn encode-tree-with
"Depth-first encoding of a tree."
[encoded? encoder zipper]
(letfn [(f [zipper]
(let [node (z/node zipper)]
(if (encoded? node)
;; Node already fully encoded.
node
;; If it is a leaf, or all of its children are encoded.
(if (or (all-children-valid? encoded? zipper)
(leaf? zipper))
;; Encode then go up a "level".
(let [encoded (z/edit zipper encoder)]
(recur (or (z/right encoded)
(z/up encoded)
encoded)))
;; Keep moving downwards until you find a leaf.
(recur (next-leaf zipper))))))]
(f zipper)))

0 comments on commit fbf60e7

Please sign in to comment.