Compare commits
1 commit
master
...
split-sele
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
67ab15406e |
3 changed files with 30 additions and 10 deletions
|
|
@ -107,6 +107,9 @@
|
|||
(defmacro viewfn [& args]
|
||||
`(view (fn ~@args)))
|
||||
|
||||
(defn split [& selectors]
|
||||
(->SplitPath (->> selectors (map comp-paths*) doall)))
|
||||
|
||||
(defn selected?
|
||||
"Filters the current value based on whether a selector finds anything.
|
||||
e.g. (selected? :vals ALL even?) keeps the current element only if an
|
||||
|
|
|
|||
|
|
@ -270,5 +270,10 @@
|
|||
(-> structure view-fn next-fn)
|
||||
))
|
||||
|
||||
|
||||
|
||||
(deftype SplitPath [selectors]
|
||||
StructureValsPath
|
||||
(select-full* [this vals structure next-fn]
|
||||
(into [] (r/mapcat #(select-full* % vals structure next-fn) selectors)))
|
||||
(update-full* [this vals structure next-fn]
|
||||
(reduce (fn [structure s] (update-full* s vals structure next-fn)) structure selectors)
|
||||
))
|
||||
|
|
|
|||
|
|
@ -227,3 +227,15 @@
|
|||
[:a]
|
||||
[[1 3 5] [2] [7 11 4 2] [10 1] []]
|
||||
))))
|
||||
|
||||
(deftest split-test
|
||||
(let [data {:a [1 2 3 4] :b {:c :d}}]
|
||||
(is (= (select (split [:a ALL even?] [:b :c]) data)
|
||||
[2 4 :d]))
|
||||
;;TODO: this behavior is highly confusing... any way to have split selectors go first and THEN updates applied? ... only affects updates... what if it matches both selectors?
|
||||
;; maybe shouldn't allow splitting for ALL
|
||||
(is (= (update [:a ALL (split [even? (view -)] odd?)]
|
||||
inc
|
||||
data)
|
||||
{:a [2 0 4 -2] :b {:c :d}}
|
||||
))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue