diff --git a/.github/workflows/clojure.yml b/.github/workflows/clojure.yml index 53bfe7d..1fbc1c2 100644 --- a/.github/workflows/clojure.yml +++ b/.github/workflows/clojure.yml @@ -12,14 +12,14 @@ jobs: test: strategy: matrix: - java_version: [corretto-11,corretto-21] + java_version: [corretto-11,corretto-25] test_clojure_alias: [clojure-1.10, clojure-1.11, clojure-1.12] runs-on: ubuntu-latest steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v6 - name: Tools setup - uses: jdx/mise-action@v2 + uses: jdx/mise-action@v4 with: install: true cache: true @@ -39,9 +39,9 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v6 - name: Tools setup - uses: jdx/mise-action@v2 + uses: jdx/mise-action@v4 with: install: true cache: true diff --git a/.mise.toml b/.mise.toml index ece162a..551df59 100644 --- a/.mise.toml +++ b/.mise.toml @@ -1,4 +1,4 @@ -[alias] +[tool_alias] clojure = "https://github.com/asdf-community/asdf-clojure.git" [tools] diff --git a/CHANGELOG.md b/CHANGELOG.md index 67b8614..6fb7455 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ This is a history of changes to gateless/walkr +# 0.2.0 +* Added support for walking multiple collections in tandem: `walk-reduce`, `postwalk-reduce`, and `prewalk-reduce` now accept optional trailing secondary collections that are walked alongside the primary form (paired by key for maps/records, by value/membership for sets, by index for vectors/lists/seqs) and passed as extra read-only arguments to the walk function. + # 0.1.0 * The initial release. diff --git a/README.md b/README.md index 7f174c2..aab71c0 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,23 @@ Here's a simple example. ;;; :other "HOTTER"}}]] ``` +You can also walk multiple collections in tandem. The first collection drives the traversal shape; each additional trailing collection is walked alongside it (paired by key for maps, by value/membership for sets, by index for vectors/lists/seqs), and its corresponding value is passed as extra, read-only context to your callback. Only the first collection is transformed and rebuilt. + +```clj +(w/postwalk-reduce + (fn [acc item & secondary-items] + (if (number? item) + [(conj acc (into [item] secondary-items)) item] + [acc item])) + [] + {:a 1 :b 2} + {:a 10 :b 20}) +;;; => +;;; [[[1 10] [2 20]] {:a 1 :b 2}] +``` + +Note: for sets, pairing is by value/membership rather than position — a set element is its own key, so the corresponding secondary value is that same element if it's also present in the secondary set, or nil otherwise. + See the existing tests for more examples. # _Building_ diff --git a/deps.edn b/deps.edn index 013ae67..f0e3451 100644 --- a/deps.edn +++ b/deps.edn @@ -5,7 +5,7 @@ :aliases {:clojure-1.10 {:extra-deps {org.clojure/clojure {:mvn/version "1.10.3"}}} :clojure-1.11 {:extra-deps {org.clojure/clojure {:mvn/version "1.11.4"}}} - :clojure-1.12 {:extra-deps {org.clojure/clojure {:mvn/version "1.12.0"}}} + :clojure-1.12 {:extra-deps {org.clojure/clojure {:mvn/version "1.12.5"}}} :dev {:extra-paths ["dev"] :extra-deps {reloaded.repl/reloaded.repl {:mvn/version "0.2.4"} diff --git a/src/walkr/core.clj b/src/walkr/core.clj index 048280a..c4e5cea 100644 --- a/src/walkr/core.clj +++ b/src/walkr/core.clj @@ -1,57 +1,153 @@ (ns walkr.core (:import [clojure.lang IMapEntry MapEntry IRecord])) +(defn- pad-secondary + "Returns sec as an infinite sequence for zipping against the primary: a + sequential sec is followed by endless nils past its own length; a + non-sequential (or nil) sec resolves to nil at every position." + [sec] + (if (sequential? sec) + (concat sec (repeat nil)) + (repeat nil))) + +(defn- positional-children + "Builds [primary secondary...] child tuples for a positionally-ordered + form (list, seq, vector), pairing form's items with each secondary in + more-forms by position. Walks form and every secondary together in + lock-step via map (each secondary padded to be infinite), rather than + seeking to each index, so a list/seq secondary costs O(n) overall + instead of O(n^2)." + [form more-forms] + (apply map + (fn [item & secs] (into [item] secs)) + form + (for [sec more-forms] (pad-secondary sec)))) + +(defn- map-entry-for + "Resolves sec's value for key k as a MapEntry, defaulting to nil when sec + isn't associative or doesn't contain k." + [k sec] + (MapEntry/create k + (when (and (associative? sec) + (contains? sec k)) + (get sec k)))) + +(defn- map-children + "Builds [primary secondary...] child tuples for a map-like form (maps and + records), pairing form's entries with each secondary by key rather than + position." + [form more-forms] + (for [[k v] form] + (into [(MapEntry/create k v)] + (for [sec more-forms] + (map-entry-for k sec))))) + +(defn- set-children + "Builds [primary secondary...] child tuples for a set form, pairing each + element with its match in each secondary set by value rather than by + position, since set iteration order isn't semantically meaningful and + can't be relied on to align two independently-built sets. Each + secondary's set-ness is resolved once here rather than once per element." + [form more-forms] + (let [secs (for [sec more-forms] + (when (set? sec) + sec))] + (for [e form] + (into [e] + (for [sec secs] + (when (and sec (contains? sec e)) + e)))))) + (defn- reduce-form - [build-fn inner outer acc form] - (let [[acc vec-form] (reduce (fn walk - [[acc-in form-in] item-in] - (let [[acc-out item-out] (inner acc-in item-in) - form-out (conj form-in item-out)] - [acc-out form-out])) + [build-fn inner outer acc form children more-forms] + (let [[acc vec-form] (reduce (fn do-walk + [[acc-in built] child] + (let [[acc-out item-out] (apply inner acc-in child)] + [acc-out (conj built item-out)])) [acc []] - form)] - (outer acc (with-meta - (build-fn vec-form) - (meta form))))) + children)] + (apply outer acc (with-meta (build-fn vec-form) (meta form)) more-forms))) (defn walk-reduce - "Traverses form, an arbitrary data structure while accumulating a result. - Inner and outer are 2 arity functions, returning a vector of [acc form]. - Applies inner to acc and each element of form, building up a data - structure of the same type, then applies outer to acc and the result. - Recognizes all Clojure data structures. Consumes seqs as with doall." - [inner outer acc form] + "Traverses form, an arbitrary data structure, while accumulating a result. + Inner and outer are functions of (acc, item, & secondary-items) returning + [acc item]. Recognizes all Clojure data structures. Consumes seqs as with + doall. + + Accepts optional trailing secondary collections (more-forms). form alone + drives traversal shape/order; each collection in more-forms is walked in + tandem, following the same path form's traversal takes, and its + corresponding item is passed as an extra trailing argument to inner/outer. + Only form's items are transformed and rebuilt; items from more-forms are + read-only context. + + Pairing: maps/records pair secondaries by key (missing/non-associative -> + nil); sets pair secondaries by value/membership rather than position, + since set iteration order is not semantically meaningful (missing/ + non-set -> nil); vectors/lists/seqs pair by index (out-of-range/ + non-sequential -> nil). With no secondary collections, behaves exactly + as before." + [inner outer acc form & more-forms] (cond (list? form) - (reduce-form (partial apply list) inner outer acc form) + (reduce-form (partial apply list) inner outer acc form + (positional-children form more-forms) more-forms) (instance? IMapEntry form) - (let [[acc key] (inner acc (key form)) - [acc val] (inner acc (val form))] - (outer acc (MapEntry/create key val))) + (let [key-seq (for [sec more-forms] + (when (instance? IMapEntry sec) + (key sec))) + val-seq (for [sec more-forms] + (when (instance? IMapEntry sec) + (val sec))) + [acc key-out] (apply inner acc (key form) key-seq) + [acc val-out] (apply inner acc (val form) val-seq)] + (apply outer acc (MapEntry/create key-out val-out) more-forms)) (seq? form) - (reduce-form seq inner outer acc (doall form)) + (let [form (doall form)] + (reduce-form seq inner outer acc form + (positional-children form more-forms) more-forms)) + + (or (instance? IRecord form) (map? form)) + (reduce-form (if (instance? IRecord form) + (partial into form) + (partial into (empty form))) + inner outer acc form + (map-children form more-forms) more-forms) - (instance? IRecord form) - (reduce-form (partial into form) inner outer acc form) + (set? form) + (reduce-form (partial into (empty form)) inner outer acc form + (set-children form more-forms) more-forms) (coll? form) - (reduce-form (partial into (empty form)) inner outer acc form) + (reduce-form (partial into (empty form)) inner outer acc form + (positional-children form more-forms) more-forms) :else - (outer acc form))) + (apply outer acc form more-forms))) (defn postwalk-reduce - "Performs a depth-first, post-order traversal of form. - Calls f with acc on each sub-form, f should return a vector of [new-acc new-form]. - Uses f's return acc and form in place of the original with each call. - Recognizes all Clojure data structures. Consumes seqs as with doall." - [f acc form] - (walk-reduce (partial postwalk-reduce f) f acc form)) + "Performs a depth-first, post-order traversal of form, calling f with acc + on each sub-form; f returns [new-acc new-form]. Recognizes all Clojure + data structures. Consumes seqs as with doall. + + Accepts optional trailing secondary collections (more-forms), walked in + tandem with form (see walk-reduce for pairing rules). f is called as + (apply f acc item secondary-item...) and still returns [new-acc new-item], + transforming only the primary item. With no secondary collections, + behaves exactly as before." + [f acc form & more-forms] + (apply walk-reduce (partial postwalk-reduce f) f acc form more-forms)) (defn prewalk-reduce - "Like postwalk-reduce, but does pre-order traversal." - [f acc form] - (let [[acc form] (f acc form)] - (walk-reduce (partial prewalk-reduce f) vector acc form))) + "Like postwalk-reduce, but does pre-order traversal. + + Accepts optional trailing secondary collections (more-forms), walked in + tandem with form (see walk-reduce for pairing rules). With no secondary + collections, behaves exactly as before." + [f acc form & more-forms] + (let [[acc form] (apply f acc form more-forms)] + (apply walk-reduce (partial prewalk-reduce f) + (fn [acc item & _secondary-items] [acc item]) + acc form more-forms))) diff --git a/test/walkr/core_test.clj b/test/walkr/core_test.clj index 950ebec..8bac560 100644 --- a/test/walkr/core_test.clj +++ b/test/walkr/core_test.clj @@ -110,3 +110,86 @@ [(conj acc form) form]) [] [1 2 {:a 3} (list 4 [5])])))) + +(deftest test-tandem-map-matching-keys + (testing "Two-map tandem walk pairs values by key, not iteration order" + (is (= #{[1 100] [2 200]} + (set (first (w/postwalk-reduce + (fn [acc item & secs] + (if (number? item) + [(conj acc (into [item] secs)) item] + [acc item])) + [] + {:a 1 :b 2} + {:b 200 :a 100})))))) + (testing "keys are still visited by f and paired trivially with themselves" + (is (= #{[:a :a] [:b :b]} + (set (first (w/postwalk-reduce + (fn [acc item & secs] + (if (keyword? item) + [(conj acc (into [item] secs)) item] + [acc item])) + [] + {:a 1 :b 2} + {:a 10 :b 20}))))))) + +(deftest test-tandem-map-missing-and-extra-keys + (testing "Secondary map missing a key yields nil; extra secondary keys are ignored" + (is (= #{[1 10] [2 nil]} + (set (first (w/postwalk-reduce + (fn [acc item & secs] + (if (number? item) + [(conj acc (into [item] secs)) item] + [acc item])) + [] + {:a 1 :b 2} + {:a 10 :c 99}))))))) + +(deftest test-tandem-vector-mismatched-lengths + (testing "Secondary vector shorter than primary yields nil for missing indexes" + (is (= [[1 10] [2 20] [3 nil]] + (first (w/postwalk-reduce + (fn [acc item & secs] + (if (number? item) + [(conj acc (into [item] secs)) item] + [acc item])) + [] + [1 2 3] + [10 20])))))) + +(deftest test-tandem-three-collections + (testing "N-ary tandem walk with three collections (not hardcoded binary)" + (is (= [[1 10 100] [2 20 200]] + (first (w/postwalk-reduce + (fn [acc item & secs] + (if (number? item) + [(conj acc (into [item] secs)) item] + [acc item])) + [] + [1 2] + [10 20] + [100 200])))))) + +(deftest test-tandem-nil-secondary + (testing "A wholly nil secondary collection resolves to nil at every position" + (is (= [[1 nil] [2 nil]] + (first (w/postwalk-reduce + (fn [acc item & secs] + (if (number? item) + [(conj acc (into [item] secs)) item] + [acc item])) + [] + [1 2] + nil)))))) + +(deftest test-tandem-set-membership + (testing "Sets pair by value/membership, not by (arbitrary) iteration order" + (is (= #{[1 1] [2 nil] [3 3]} + (set (first (w/postwalk-reduce + (fn [acc item & secs] + (if (number? item) + [(conj acc (into [item] secs)) item] + [acc item])) + [] + #{1 2 3} + #{3 1 99})))))))