From 07b69cc9a63ce386269655fc03b5a6820d22afd8 Mon Sep 17 00:00:00 2001 From: Marat Surmashev Date: Wed, 12 Jun 2024 00:14:14 +0200 Subject: [PATCH] Revert "Move to custom walk2" This reverts commit 8d42621e1d08af36a103942e338030cfb581221f. --- src/zen/store.clj | 4 +- src/zen/walk2.clj | 137 ---------------------------------------- test/zen/walk2_test.clj | 59 ----------------- 3 files changed, 2 insertions(+), 198 deletions(-) delete mode 100644 src/zen/walk2.clj delete mode 100644 test/zen/walk2_test.clj diff --git a/src/zen/store.clj b/src/zen/store.clj index 7b06634..6a90c90 100644 --- a/src/zen/store.clj +++ b/src/zen/store.clj @@ -3,7 +3,7 @@ [clojure.edn] [clojure.java.io :as io] [clojure.string :as str] - [zen.walk2] + [clojure.walk :as walk] [edamame.core] [zen.utils] [zen.v2-validation :as v2])) @@ -48,7 +48,7 @@ ;; disables sym expansion in zen/list (expr types) (if (list? resource) resource - (zen.walk2/walk (partial walk-resource walk-fn) walk-fn resource))) + (walk/walk (partial walk-resource walk-fn) walk-fn resource))) (defn validate-symbol diff --git a/src/zen/walk2.clj b/src/zen/walk2.clj deleted file mode 100644 index 95c5e8d..0000000 --- a/src/zen/walk2.clj +++ /dev/null @@ -1,137 +0,0 @@ -(ns zen.walk2) - -(defprotocol ^{:added "1.6"} Walkable - (^{:added "1.6"} walkt [coll f] - "If coll is a collection, applies f to each element of the collection - and returns a collection of the results, of the same type and order - as coll. If coll is not a collection, returns it unchanged. \"Same - type\" means a type with the same behavior. For example, a hash-map - may be returned as an array-map, but a a sorted-map will be returned - as a sorted-map with the same comparator.")) - -;; From http://clojure.org/protocols : -;; -;; "You can implement a protocol on an interface ... if one interface -;; is derived from the other, the more derived is used, else which one -;; is used is unspecified." - -(extend-protocol Walkable - nil - (walkt [coll f] nil) - java.lang.Object ; default: not a collection - (walkt [x f] x) - clojure.lang.IMapEntry - (walkt [coll f] - (clojure.lang.MapEntry. (f (.key coll)) (f (.val coll)))) - clojure.lang.ISeq ; generic sequence fallback - (walkt [coll f] - (map f coll)) - clojure.lang.PersistentList ; special case to preserve type - (walkt [coll f] - (apply list (map f coll))) - clojure.lang.PersistentList$EmptyList ; special case to preserve type - (walkt [coll f] '()) - clojure.lang.IRecord ; any defrecord - (walkt [coll f] - (reduce (fn [r x] (conj r (f x))) coll coll))) - -(defn- walkt-transient [coll f] - ;; Note: `transient` discards metadata as of Clojure 1.6.0 - (persistent! - (reduce (fn [r x] (conj! r (f x))) (transient (empty coll)) coll))) - -;; Persistent collections that support transients -(doseq [type [clojure.lang.PersistentArrayMap - clojure.lang.PersistentHashMap - clojure.lang.PersistentHashSet - clojure.lang.PersistentVector]] - (extend type Walkable {:walkt walkt-transient})) - -(defn- walkt-default [coll f] - (reduce (fn [r x] (conj r (f x))) (empty coll) coll)) - -;; Persistent collections that don't support transients -(doseq [type [clojure.lang.PersistentQueue - clojure.lang.PersistentStructMap - clojure.lang.PersistentTreeMap - clojure.lang.PersistentTreeSet]] - (extend type Walkable {:walkt walkt-default})) - -(defn walk - "Traverses form, an arbitrary data structure. inner and outer are - functions. Applies inner to each element of form, building up a - data structure of the same type, then applies outer to the result. - Recognizes all Clojure data structures. Consumes seqs as with doall." - {:added "1.1"} - [inner outer form] - (outer (walkt form inner))) - -(defn postwalk - "Performs a depth-first, post-order traversal of form. Calls f on - each sub-form, uses f's return value in place of the original. - Recognizes all Clojure data structures. Consumes seqs as with doall." - {:added "1.1"} - [f form] - (walk (partial postwalk f) f form)) - -(defn prewalk - "Like postwalk, but does pre-order traversal." - {:added "1.1"} - [f form] - (walk (partial prewalk f) identity (f form))) - - -(defn postwalk-demo - "Demonstrates the behavior of postwalk by printing each form as it is - walked. Returns form." - {:added "1.1"} - [form] - (postwalk (fn [x] (print "Walked: ") (prn x) x) form)) - -(defn prewalk-demo - "Demonstrates the behavior of prewalk by printing each form as it is - walked. Returns form." - {:added "1.1"} - [form] - (prewalk (fn [x] (print "Walked: ") (prn x) x) form)) - -(defn keywordize-keys - "Recursively transforms all map keys from strings to keywords." - {:added "1.1"} - [m] - (let [f (fn [[k v]] (if (string? k) [(keyword k) v] [k v]))] - ;; only apply to maps - (postwalk (fn [x] (if (map? x) (into {} (map f x)) x)) m))) - -(defn stringify-keys - "Recursively transforms all map keys from keywords to strings." - {:added "1.1"} - [m] - (let [f (fn [[k v]] (if (keyword? k) [(name k) v] [k v]))] - ;; only apply to maps - (postwalk (fn [x] (if (map? x) (into {} (map f x)) x)) m))) - -(defn prewalk-replace - "Recursively transforms form by replacing keys in smap with their - values. Like clojure/replace but works on any data structure. Does - replacement at the root of the tree first." - {:added "1.1"} - [smap form] - (prewalk (fn [x] (if (contains? smap x) (smap x) x)) form)) - -(defn postwalk-replace - "Recursively transforms form by replacing keys in smap with their - values. Like clojure/replace but works on any data structure. Does - replacement at the leaves of the tree first." - {:added "1.1"} - [smap form] - (postwalk (fn [x] (if (contains? smap x) (smap x) x)) form)) - -(defn macroexpand-all - "Recursively performs all possible macroexpansions in form. For - development use only: This function does not perfectly emulate - macroexpansion by the Clojure compiler because it doesn't understand - special forms like 'let'." - {:added "1.1"} - [form] - (prewalk (fn [x] (if (seq? x) (macroexpand x) x)) form)) diff --git a/test/zen/walk2_test.clj b/test/zen/walk2_test.clj deleted file mode 100644 index a4d14cd..0000000 --- a/test/zen/walk2_test.clj +++ /dev/null @@ -1,59 +0,0 @@ -(ns zen.walk2-test - (:require [zen.walk2 :as w] - [clojure.test :refer (deftest is)])) - -(deftest t-prewalk-replace - (is (= (w/prewalk-replace {:a :b} [:a {:a :a} (list 3 :c :a)]) - [:b {:b :b} (list 3 :c :b)]))) - -(deftest t-postwalk-replace - (is (= (w/postwalk-replace {:a :b} [:a {:a :a} (list 3 :c :a)]) - [:b {:b :b} (list 3 :c :b)]))) - -(deftest t-stringify-keys - (is (= (w/stringify-keys {:a 1, nil {:b 2 :c 3}, :d 4}) - {"a" 1, nil {"b" 2 "c" 3}, "d" 4}))) - -(deftest t-prewalk-order - (is (= (let [a (atom [])] - (w/prewalk (fn [form] (swap! a conj form) form) - [1 2 {:a 3} (list 4 [5])]) - @a) - [[1 2 {:a 3} (list 4 [5])] - 1 2 {:a 3} [:a 3] :a 3 (list 4 [5]) - 4 [5] 5]))) - -(deftest t-postwalk-order - (is (= (let [a (atom [])] - (w/postwalk (fn [form] (swap! a conj form) form) - [1 2 {:a 3} (list 4 [5])]) - @a) - [1 2 - :a 3 [:a 3] {:a 3} - 4 5 [5] (list 4 [5]) - [1 2 {:a 3} (list 4 [5])]]))) - -(defrecord Foo [a b c]) - -(deftest walk - "Checks that walk returns the correct result and type of collection" - (let [colls ['(1 2 3) - [1 2 3] - #{1 2 3} - (sorted-set-by > 1 2 3) - {:a 1, :b 2, :c 3} - (sorted-map-by > 1 10, 2 20, 3 30) - (->Foo 1 2 3) - (map->Foo {:a 1 :b 2 :c 3 :extra 4})]] - (doseq [c colls] - (let [walked (w/walk identity identity c)] - (is (= c walked)) - (is (= (type c) (type walked))) - (if (map? c) - (is (= (w/walk #(update-in % [1] inc) #(reduce + (vals %)) c) - (reduce + (map (comp inc val) c)))) - (is (= (w/walk inc #(reduce + %) c) - (reduce + (map inc c))))) - (when (or (instance? clojure.lang.PersistentTreeMap c) - (instance? clojure.lang.PersistentTreeSet c)) - (is (= (.comparator c) (.comparator walked))))))))