empty selectors and nil count as identity path now fixing #5, remove IDENTITY-PATH in favor of nil

This commit is contained in:
Nathan Marz 2015-05-31 08:50:00 -04:00
parent 228a949ac1
commit 50576e447b
3 changed files with 82 additions and 55 deletions

View file

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

View file

@ -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,6 +148,8 @@
)) ))
(defn- combine-same-types [[^TransformFunctions f & _ :as all]] (defn- combine-same-types [[^TransformFunctions f & _ :as all]]
(if (empty? all)
(coerce-path nil)
(let [^ExecutorFunctions exs (.executors f) (let [^ExecutorFunctions exs (.executors f)
t (.type exs) t (.type exs)
@ -168,7 +173,7 @@
(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)
))

View file

@ -232,8 +232,8 @@
(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+
@ -245,3 +245,29 @@
(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)
))))