implemented paramscollector and converted putval to use it
This commit is contained in:
parent
983bf84495
commit
bcfcd02f80
3 changed files with 48 additions and 17 deletions
|
|
@ -58,8 +58,8 @@
|
||||||
[selector structure]
|
[selector structure]
|
||||||
(compiled-select-first (i/comp-paths* selector) structure))
|
(compiled-select-first (i/comp-paths* selector) structure))
|
||||||
|
|
||||||
;; Transformfunctions
|
|
||||||
|
|
||||||
|
;; Transform functions
|
||||||
|
|
||||||
(def ^{:doc "Version of transform that takes in a selector pre-compiled with comp-paths"}
|
(def ^{:doc "Version of transform that takes in a selector pre-compiled with comp-paths"}
|
||||||
compiled-transform i/compiled-transform*)
|
compiled-transform i/compiled-transform*)
|
||||||
|
|
@ -114,19 +114,22 @@
|
||||||
|
|
||||||
(defmacro paramspath [params & impls]
|
(defmacro paramspath [params & impls]
|
||||||
(let [num-params (count params)
|
(let [num-params (count params)
|
||||||
retrieve-params (->> params
|
retrieve-params (i/make-param-retrievers params)]
|
||||||
(map-indexed
|
|
||||||
(fn [i p]
|
|
||||||
[p `(aget ~i/PARAMS-SYM
|
|
||||||
(+ ~i/PARAMS-IDX-SYM ~i))]
|
|
||||||
))
|
|
||||||
(apply concat))]
|
|
||||||
(i/paramspath* retrieve-params num-params impls)
|
(i/paramspath* retrieve-params num-params impls)
|
||||||
))
|
))
|
||||||
|
|
||||||
|
(defmacro paramscollector [params impl]
|
||||||
|
(let [num-params (count params)
|
||||||
|
retrieve-params (i/make-param-retrievers params)]
|
||||||
|
(i/paramscollector* retrieve-params num-params impl)
|
||||||
|
))
|
||||||
|
|
||||||
(defmacro defparamspath [name & body]
|
(defmacro defparamspath [name & body]
|
||||||
`(def ~name (paramspath ~@body)))
|
`(def ~name (paramspath ~@body)))
|
||||||
|
|
||||||
|
(defmacro defparamscollector [name & body]
|
||||||
|
`(def ~name (paramscollector ~@body)))
|
||||||
|
|
||||||
(defmacro fixed-pathed-path [bindings & impls]
|
(defmacro fixed-pathed-path [bindings & impls]
|
||||||
(let [bindings (partition 2 bindings)
|
(let [bindings (partition 2 bindings)
|
||||||
paths (mapv second bindings)
|
paths (mapv second bindings)
|
||||||
|
|
@ -276,16 +279,17 @@
|
||||||
(defn collect-one [& selector]
|
(defn collect-one [& selector]
|
||||||
(i/->SelectCollector select-one (i/comp-paths* selector)))
|
(i/->SelectCollector select-one (i/comp-paths* selector)))
|
||||||
|
|
||||||
(defn putval
|
|
||||||
"Adds an external value to the collected vals. Useful when additional arguments
|
|
||||||
are required to the transform function that would otherwise require partial
|
|
||||||
application or a wrapper function.
|
|
||||||
|
|
||||||
e.g., incrementing val at path [:a :b] by 3:
|
;;TODO: add this comment:
|
||||||
(transform [:a :b (putval 3)] + some-map)"
|
; "Adds an external value to the collected vals. Useful when additional arguments
|
||||||
[val]
|
; are required to the transform function that would otherwise require partial
|
||||||
(i/->PutValCollector val))
|
; application or a wrapper function.
|
||||||
|
|
||||||
|
; e.g., incrementing val at path [:a :b] by 3:
|
||||||
|
; (transform [:a :b (putval 3)] + some-map)"
|
||||||
|
(defparamscollector putval [val]
|
||||||
|
(collect* [this structure]
|
||||||
|
val ))
|
||||||
|
|
||||||
;;TODO: test nothing matches case
|
;;TODO: test nothing matches case
|
||||||
(defn cond-path
|
(defn cond-path
|
||||||
|
|
|
||||||
|
|
@ -410,6 +410,34 @@
|
||||||
ret#
|
ret#
|
||||||
))))
|
))))
|
||||||
|
|
||||||
|
|
||||||
|
(defn paramscollector* [bindings num-params [_ [_ structure-sym] & body]]
|
||||||
|
`(let [collector# (fn [~PARAMS-SYM ~PARAMS-IDX-SYM vals# ~structure-sym next-fn#]
|
||||||
|
(let [~@bindings ~@[] ; to avoid syntax highlighting issues
|
||||||
|
c# (do ~@body)]
|
||||||
|
(next-fn#
|
||||||
|
~PARAMS-SYM
|
||||||
|
(+ ~PARAMS-IDX-SYM ~num-params)
|
||||||
|
(conj vals# c#)
|
||||||
|
~structure-sym)
|
||||||
|
))]
|
||||||
|
(->ParamsNeededPath
|
||||||
|
(->TransformFunctions
|
||||||
|
RichPathExecutor
|
||||||
|
collector#
|
||||||
|
collector# )
|
||||||
|
~num-params
|
||||||
|
)))
|
||||||
|
|
||||||
|
(defn make-param-retrievers [params]
|
||||||
|
(->> params
|
||||||
|
(map-indexed
|
||||||
|
(fn [i p]
|
||||||
|
[p `(aget ~PARAMS-SYM
|
||||||
|
(+ ~PARAMS-IDX-SYM ~i))]
|
||||||
|
))
|
||||||
|
(apply concat)))
|
||||||
|
|
||||||
;; cell implementation idea taken from prismatic schema library
|
;; cell implementation idea taken from prismatic schema library
|
||||||
(defprotocol PMutableCell
|
(defprotocol PMutableCell
|
||||||
#?(:clj (get_cell [cell]))
|
#?(:clj (get_cell [cell]))
|
||||||
|
|
|
||||||
|
|
@ -6,4 +6,3 @@
|
||||||
|
|
||||||
(defprotocol Collector
|
(defprotocol Collector
|
||||||
(collect-val [this structure]))
|
(collect-val [this structure]))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue