-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fbf60e7
Showing
5 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.cpcache | ||
.nrepl-port |
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
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. |
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
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. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{:paths ["src"]} |
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
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))) |