From d0ff9bbc35055c94fb66fae0290cfcac3acaa7bc Mon Sep 17 00:00:00 2001 From: nathanmarz Date: Thu, 1 Jun 2017 02:38:20 -0400 Subject: [PATCH] reimplement codewalker so it works with NONE --- CHANGES.md | 3 +++ src/clj/com/rpl/specter.cljc | 30 +++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 371c55c..988b29e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,6 +3,9 @@ * Added `pred=`, `pred<`, `pred>`, `pred<=`, `pred>=` for filtering using common comparisons * Add `map-key` navigator * Add `set-elem` navigator +* Add `ALL-WITH-META` navigator +* `walker` and `codewalker` can now be used with `NONE` to remove elements +* Extend `ALL` to work on records (navigate to key/value pairs) * Workaround ClojureScript bug that emits warnings for vars named the same as a private var in cljs.core (in this case `NONE`, added as private var to cljs.core with 1.9.562) * For ALL transforms on maps, interpret transformed key/value pair of size < 2 as removal * Bug fix: Fix incorrect inline compilation when a dynamic function invocation is nested in a data structure within a parameter to a navigator builder diff --git a/src/clj/com/rpl/specter.cljc b/src/clj/com/rpl/specter.cljc index 81f5c2d..21a9763 100644 --- a/src/clj/com/rpl/specter.cljc +++ b/src/clj/com/rpl/specter.cljc @@ -652,6 +652,19 @@ (transform* [this structure next-fn] (n/all-transform structure next-fn))) +(defnav + ^{:doc "Same as ALL, except maintains metadata on the structure."} + ALL-WITH-META + [] + (select* [this structure next-fn] + (n/all-select structure next-fn)) + (transform* [this structure next-fn] + (let [m (meta structure) + res (n/all-transform structure next-fn)] + (if (some? res) + (with-meta res m) + )))) + (defnav ^{:doc "Navigate to each value of the map. This is more efficient than navigating via [ALL LAST]"} @@ -827,15 +840,6 @@ (merge (reduce dissoc structure m-keys) newmap)))) -(defnav - ^{:doc "Like `walker` but maintains metadata of any forms traversed."} - codewalker - [afn] - (select* [this structure next-fn] - (i/walk-select afn next-fn structure)) - (transform* [this structure next-fn] - (i/codewalk-until afn next-fn structure))) - (defdynamicnav subselect "Navigates to a sequence that contains the results of (select ...), but is a view to the original structure that can be transformed. @@ -1286,3 +1290,11 @@ (cond-path (pred apred) STAY coll? [ALL p] ))) + +(def + ^{:doc "Like `walker` but maintains metadata of any forms traversed."} + codewalker + (recursive-path [apred] p + (cond-path (pred apred) STAY + coll? [ALL-WITH-META p] + )))