Compare commits

...

1 commit

Author SHA1 Message Date
Nathan Marz
67ab15406e implemented split selector but semantics on update can be confusing (see test) 2015-04-22 10:55:36 -04:00
3 changed files with 30 additions and 10 deletions

View file

@ -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

View file

@ -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)
))

View file

@ -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}}
))))