change cond-path/if-path to take a selector for the conditional (works like selected?)
This commit is contained in:
parent
2299bcc58b
commit
e4a3275ff1
3 changed files with 36 additions and 3 deletions
|
|
@ -192,7 +192,7 @@
|
||||||
[& conds]
|
[& conds]
|
||||||
(->> conds
|
(->> conds
|
||||||
(partition 2)
|
(partition 2)
|
||||||
(map (fn [[c p]] [c (comp-paths* p)]))
|
(map (fn [[c p]] [(comp-paths* c) (comp-paths* p)]))
|
||||||
doall
|
doall
|
||||||
->ConditionalPath
|
->ConditionalPath
|
||||||
))
|
))
|
||||||
|
|
@ -201,4 +201,4 @@
|
||||||
"Like cond-path, but with if semantics."
|
"Like cond-path, but with if semantics."
|
||||||
([cond-fn if-path] (cond-path cond-fn if-path))
|
([cond-fn if-path] (cond-path cond-fn if-path))
|
||||||
([cond-fn if-path else-path]
|
([cond-fn if-path else-path]
|
||||||
(cond-path cond-fn if-path (fn [_] true) else-path)))
|
(cond-path cond-fn if-path nil else-path)))
|
||||||
|
|
|
||||||
|
|
@ -189,6 +189,9 @@
|
||||||
))))
|
))))
|
||||||
|
|
||||||
(extend-protocol StructureValsPathComposer
|
(extend-protocol StructureValsPathComposer
|
||||||
|
nil
|
||||||
|
(comp-paths* [sp]
|
||||||
|
(coerce-path sp))
|
||||||
Object
|
Object
|
||||||
(comp-paths* [sp]
|
(comp-paths* [sp]
|
||||||
(coerce-path sp))
|
(coerce-path sp))
|
||||||
|
|
@ -486,7 +489,10 @@
|
||||||
|
|
||||||
(defn- retrieve-selector [cond-pairs structure]
|
(defn- retrieve-selector [cond-pairs structure]
|
||||||
(->> cond-pairs
|
(->> cond-pairs
|
||||||
(drop-while (fn [[c-fn _]] (not (c-fn structure))))
|
(drop-while (fn [[c-selector _]]
|
||||||
|
(->> structure
|
||||||
|
(compiled-select* c-selector)
|
||||||
|
empty?)))
|
||||||
first
|
first
|
||||||
second
|
second
|
||||||
))
|
))
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
(:use [clojure.test]
|
(:use [clojure.test]
|
||||||
[clojure.test.check.clojure-test]
|
[clojure.test.check.clojure-test]
|
||||||
[com.rpl specter]
|
[com.rpl specter]
|
||||||
|
[com.rpl.specter protocols]
|
||||||
[com.rpl.specter test-helpers])
|
[com.rpl.specter test-helpers])
|
||||||
(:require [clojure.test.check
|
(:require [clojure.test.check
|
||||||
[generators :as gen]
|
[generators :as gen]
|
||||||
|
|
@ -235,6 +236,9 @@
|
||||||
(and (= [i] (select nil i))
|
(and (= [i] (select nil i))
|
||||||
(= (afn i) (update nil afn i)))))
|
(= (afn i) (update nil afn i)))))
|
||||||
|
|
||||||
|
(deftest nil-comp-test
|
||||||
|
(is (= [5] (select (comp-paths* nil) 5))))
|
||||||
|
|
||||||
(defspec putval-test
|
(defspec putval-test
|
||||||
(for-all+
|
(for-all+
|
||||||
[kw gen/keyword
|
[kw gen/keyword
|
||||||
|
|
@ -315,3 +319,26 @@
|
||||||
*
|
*
|
||||||
2)))
|
2)))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
(defspec cond-path-selector-test
|
||||||
|
(for-all+
|
||||||
|
[k1 (max-size 3 gen/keyword)
|
||||||
|
k2 (max-size 3 gen/keyword)
|
||||||
|
k3 (max-size 3 gen/keyword)
|
||||||
|
m (max-size 5
|
||||||
|
(gen-map-with-keys
|
||||||
|
gen/keyword
|
||||||
|
gen/int
|
||||||
|
k1
|
||||||
|
k2
|
||||||
|
k3))
|
||||||
|
pred (gen/elements [odd? even?])
|
||||||
|
]
|
||||||
|
(let [v1 (get m k1)
|
||||||
|
k (if (pred v1) k2 k3)]
|
||||||
|
(and
|
||||||
|
(= (update (if-path [k1 pred] k2 k3) inc m)
|
||||||
|
(update k inc m))
|
||||||
|
(= (select (if-path [k1 pred] k2 k3) m)
|
||||||
|
(select k m))
|
||||||
|
))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue