Merge pull request #8 from mwfogleman/traverse-all
Add documentation for traverse-all
This commit is contained in:
commit
c8eb8ddcac
1 changed files with 45 additions and 0 deletions
|
|
@ -14,6 +14,7 @@
|
||||||
- [setval](#setval)
|
- [setval](#setval)
|
||||||
- [transform](#transform)
|
- [transform](#transform)
|
||||||
- [traverse](#traverse)
|
- [traverse](#traverse)
|
||||||
|
- [traverse-all](#traverse)
|
||||||
- [Path Macros](#path-macros)
|
- [Path Macros](#path-macros)
|
||||||
- [declarepath](#declarepath)
|
- [declarepath](#declarepath)
|
||||||
- [defprotocolpath](#defprotocolpath)
|
- [defprotocolpath](#defprotocolpath)
|
||||||
|
|
@ -265,6 +266,50 @@ This macro will do inline compilation and caching of the path.
|
||||||
;; returns object implementing clojure.lang.IReduce
|
;; 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.
|
||||||
|
|
||||||
|
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]}])
|
||||||
|
;; => 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
|
# Path Macros
|
||||||
|
|
||||||
## declarepath
|
## declarepath
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue