empty selectors and nil count as identity path now fixing #5, remove IDENTITY-PATH in favor of nil
This commit is contained in:
parent
228a949ac1
commit
50576e447b
3 changed files with 82 additions and 55 deletions
|
|
@ -124,8 +124,6 @@
|
||||||
|
|
||||||
(def FIRST (->FirstStructurePath))
|
(def FIRST (->FirstStructurePath))
|
||||||
|
|
||||||
(def IDENTITY-PATH (->IdentityPath))
|
|
||||||
|
|
||||||
(defn srange-dynamic [start-fn end-fn] (->SRangePath start-fn end-fn))
|
(defn srange-dynamic [start-fn end-fn] (->SRangePath start-fn end-fn))
|
||||||
|
|
||||||
(defn srange [start end] (srange-dynamic (fn [_] start) (fn [_] end)))
|
(defn srange [start end] (srange-dynamic (fn [_] start) (fn [_] end)))
|
||||||
|
|
|
||||||
|
|
@ -121,6 +121,9 @@
|
||||||
(or (fn? obj) (obj-extends? StructurePath obj)))
|
(or (fn? obj) (obj-extends? StructurePath obj)))
|
||||||
|
|
||||||
(extend-protocol CoerceTransformFunctions
|
(extend-protocol CoerceTransformFunctions
|
||||||
|
nil ; needs its own path because it doesn't count as an Object
|
||||||
|
(coerce-path [this]
|
||||||
|
(coerce-structure-path nil))
|
||||||
|
|
||||||
TransformFunctions
|
TransformFunctions
|
||||||
(coerce-path [this]
|
(coerce-path [this]
|
||||||
|
|
@ -145,30 +148,32 @@
|
||||||
))
|
))
|
||||||
|
|
||||||
(defn- combine-same-types [[^TransformFunctions f & _ :as all]]
|
(defn- combine-same-types [[^TransformFunctions f & _ :as all]]
|
||||||
(let [^ExecutorFunctions exs (.executors f)
|
(if (empty? all)
|
||||||
|
(coerce-path nil)
|
||||||
|
(let [^ExecutorFunctions exs (.executors f)
|
||||||
|
|
||||||
t (.type exs)
|
t (.type exs)
|
||||||
|
|
||||||
combiner
|
combiner
|
||||||
(if (= t :svalspath)
|
(if (= t :svalspath)
|
||||||
(fn [curr next]
|
(fn [curr next]
|
||||||
(fn [vals structure next-fn]
|
(fn [vals structure next-fn]
|
||||||
(curr vals structure
|
(curr vals structure
|
||||||
(fn [vals-next structure-next]
|
(fn [vals-next structure-next]
|
||||||
(next vals-next structure-next next-fn)
|
(next vals-next structure-next next-fn)
|
||||||
))))
|
))))
|
||||||
(fn [curr next]
|
(fn [curr next]
|
||||||
(fn [structure next-fn]
|
(fn [structure next-fn]
|
||||||
(curr structure (fn [structure] (next structure next-fn)))))
|
(curr structure (fn [structure] (next structure next-fn)))))
|
||||||
)]
|
)]
|
||||||
|
|
||||||
(reduce (fn [^TransformFunctions curr ^TransformFunctions next]
|
(reduce (fn [^TransformFunctions curr ^TransformFunctions next]
|
||||||
(->TransformFunctions
|
(->TransformFunctions
|
||||||
exs
|
exs
|
||||||
(combiner (.selector curr) (.selector next))
|
(combiner (.selector curr) (.selector next))
|
||||||
(combiner (.updater curr) (.updater next))
|
(combiner (.updater curr) (.updater next))
|
||||||
))
|
))
|
||||||
all)))
|
all))))
|
||||||
|
|
||||||
(defn coerce-structure-vals [^TransformFunctions tfns]
|
(defn coerce-structure-vals [^TransformFunctions tfns]
|
||||||
(if (= (extype tfns) :svalspath)
|
(if (= (extype tfns) :svalspath)
|
||||||
|
|
@ -448,16 +453,6 @@
|
||||||
(->> structure ((.view-fn this)) next-fn)
|
(->> structure ((.view-fn this)) next-fn)
|
||||||
))
|
))
|
||||||
|
|
||||||
(deftype IdentityPath [])
|
|
||||||
|
|
||||||
(extend-protocol StructurePath
|
|
||||||
IdentityPath
|
|
||||||
(select* [this structure next-fn]
|
|
||||||
(next-fn structure))
|
|
||||||
(update* [this structure next-fn]
|
|
||||||
(next-fn structure)
|
|
||||||
))
|
|
||||||
|
|
||||||
(deftype PutValCollector [val])
|
(deftype PutValCollector [val])
|
||||||
|
|
||||||
(extend-protocol Collector
|
(extend-protocol Collector
|
||||||
|
|
@ -466,3 +461,11 @@
|
||||||
(.val this)
|
(.val this)
|
||||||
))
|
))
|
||||||
|
|
||||||
|
|
||||||
|
(extend-protocol StructurePath
|
||||||
|
nil
|
||||||
|
(select* [this structure next-fn]
|
||||||
|
(next-fn structure))
|
||||||
|
(update* [this structure next-fn]
|
||||||
|
(next-fn structure)
|
||||||
|
))
|
||||||
|
|
|
||||||
|
|
@ -232,16 +232,42 @@
|
||||||
(for-all+
|
(for-all+
|
||||||
[i gen/int
|
[i gen/int
|
||||||
afn (gen/elements [inc dec])]
|
afn (gen/elements [inc dec])]
|
||||||
(and (= [i] (select IDENTITY-PATH i))
|
(and (= [i] (select nil i))
|
||||||
(= (afn i) (update IDENTITY-PATH afn i)))))
|
(= (afn i) (update nil afn i)))))
|
||||||
|
|
||||||
(defspec putval-test
|
(defspec putval-test
|
||||||
(for-all+
|
(for-all+
|
||||||
[kw gen/keyword
|
[kw gen/keyword
|
||||||
m (max-size 10 (gen-map-with-keys gen/keyword gen/int kw))
|
m (max-size 10 (gen-map-with-keys gen/keyword gen/int kw))
|
||||||
c gen/int]
|
c gen/int]
|
||||||
(= (update [(putval c) kw] + m)
|
(= (update [(putval c) kw] + m)
|
||||||
(update [kw (putval c)] + m)
|
(update [kw (putval c)] + m)
|
||||||
(assoc m kw (+ c (get m kw)))
|
(assoc m kw (+ c (get m kw)))
|
||||||
)))
|
)))
|
||||||
|
|
||||||
|
(defspec empty-selector-test
|
||||||
|
(for-all+
|
||||||
|
[v (gen/vector gen/int)]
|
||||||
|
(= [v]
|
||||||
|
(select [] v)
|
||||||
|
(select nil v)
|
||||||
|
(select (comp-paths) v)
|
||||||
|
(select (comp-paths nil) v)
|
||||||
|
(select [nil nil nil] v)
|
||||||
|
)))
|
||||||
|
|
||||||
|
(defspec empty-selector-update-test
|
||||||
|
(for-all+
|
||||||
|
[kw gen/keyword
|
||||||
|
m (max-size 10 (gen-map-with-keys gen/keyword gen/int kw))]
|
||||||
|
(and (= m
|
||||||
|
(update nil identity m)
|
||||||
|
(update [] identity m)
|
||||||
|
(update (comp-paths []) identity m)
|
||||||
|
(update (comp-paths nil nil) identity m)
|
||||||
|
)
|
||||||
|
(= (update kw inc m)
|
||||||
|
(update [nil kw] inc m)
|
||||||
|
(update (comp-paths kw nil) inc m)
|
||||||
|
(update (comp-paths nil kw nil) inc m)
|
||||||
|
))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue