From e8ebc79c479f0452e2e3a54b8c558e0189313005 Mon Sep 17 00:00:00 2001 From: Michael Fogleman Date: Thu, 21 Sep 2017 08:45:33 -0400 Subject: [PATCH 1/5] Initial documentation of traverse-all. --- List-of-Macros.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/List-of-Macros.md b/List-of-Macros.md index f3d0abe..a347a64 100644 --- a/List-of-Macros.md +++ b/List-of-Macros.md @@ -265,6 +265,14 @@ This macro will do inline compilation and caching of the path. ;; returns object implementing clojure.lang.IReduce ``` +## traverse-all + +_Added in 1.0.0_ + +`(traverse-all apath)` + +Returns a transducer that traverses over each element with the given path. + # Path Macros ## declarepath From 85e15d00a811ef07e648a3ddc2165ef4ca6671b8 Mon Sep 17 00:00:00 2001 From: Michael Fogleman Date: Thu, 21 Sep 2017 08:47:20 -0400 Subject: [PATCH 2/5] Add blog example. --- List-of-Macros.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/List-of-Macros.md b/List-of-Macros.md index a347a64..d472b19 100644 --- a/List-of-Macros.md +++ b/List-of-Macros.md @@ -273,6 +273,23 @@ _Added in 1.0.0_ Returns a transducer that traverses over each element with the given path. +Many common transducer use cases can be expressed more elegantly with traverse-all: + +```clojure +;; Using Vanilla Clojure +(transduce + (comp (map :a) (mapcat identity) (filter odd?)) + + + [{:a [1 2]} {:a [3]} {:a [4 5]}]) +;; => 9 + +;; The same logic expressed with Specter +(transduce + (traverse-all [:a ALL odd?]) + + + [{:a [1 2]} {:a [3]} {:a [4 5]}]) +``` + # Path Macros ## declarepath From ce6fc2a3d73dcc8bb353882a88ffd3e86893cdfc Mon Sep 17 00:00:00 2001 From: Michael Fogleman Date: Thu, 21 Sep 2017 08:48:20 -0400 Subject: [PATCH 3/5] Add toc entry. --- List-of-Macros.md | 1 + 1 file changed, 1 insertion(+) diff --git a/List-of-Macros.md b/List-of-Macros.md index d472b19..ce72399 100644 --- a/List-of-Macros.md +++ b/List-of-Macros.md @@ -14,6 +14,7 @@ - [setval](#setval) - [transform](#transform) - [traverse](#traverse) + - [traverse-all](#traverse) - [Path Macros](#path-macros) - [declarepath](#declarepath) - [defprotocolpath](#defprotocolpath) From 61b59472d89d46dfbaa1a9b2bc9483411c1dcd87 Mon Sep 17 00:00:00 2001 From: Michael Fogleman Date: Thu, 21 Sep 2017 08:50:16 -0400 Subject: [PATCH 4/5] Add return value. --- List-of-Macros.md | 1 + 1 file changed, 1 insertion(+) diff --git a/List-of-Macros.md b/List-of-Macros.md index ce72399..642c5cd 100644 --- a/List-of-Macros.md +++ b/List-of-Macros.md @@ -289,6 +289,7 @@ Many common transducer use cases can be expressed more elegantly with traverse-a (traverse-all [:a ALL odd?]) + [{:a [1 2]} {:a [3]} {:a [4 5]}]) +;; => 9 ``` # Path Macros From 95db7b58732cf904f1ceef24cb1460d26d1d59ef Mon Sep 17 00:00:00 2001 From: Michael Fogleman Date: Thu, 21 Sep 2017 08:52:18 -0400 Subject: [PATCH 5/5] Add test examples. --- List-of-Macros.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/List-of-Macros.md b/List-of-Macros.md index 642c5cd..4fa2e0c 100644 --- a/List-of-Macros.md +++ b/List-of-Macros.md @@ -292,6 +292,24 @@ Many common transducer use cases can be expressed more elegantly with traverse-a ;; => 9 ``` +Here are some more examples of using traverse-all: + +```clojure +=> (into [] (traverse-all :a) [{:a 1} {:a 2}]) +[1 2] +=> (transduce (traverse-all [ALL :a]) + + + 0 + [[{:a 1} {:a 2}] [{:a 3}]]) +6 +=> (transduce (comp (mapcat identity) + (traverse-all :a)) + (completing (fn [r i] (if (= i 4) (reduced r) (+ r i)))) + 0 + [[{:a 1}] [{:a 2}] [{:a 4}] [{:a 5}]]) +3 +``` + # Path Macros ## declarepath