implemented multi-transform and terminal
This commit is contained in:
parent
9ff9ef6650
commit
858b0b488d
3 changed files with 59 additions and 7 deletions
|
|
@ -60,7 +60,7 @@
|
||||||
(compiled-select (i/comp-paths* path)
|
(compiled-select (i/comp-paths* path)
|
||||||
structure))
|
structure))
|
||||||
|
|
||||||
(def ^{:doc "Version of select-one that takes in a path pre-compiled with comp-paths"}
|
(def ^{:doc "Version of select-one that takes in a path precompiled with comp-paths"}
|
||||||
compiled-select-one i/compiled-select-one*)
|
compiled-select-one i/compiled-select-one*)
|
||||||
|
|
||||||
(defn select-one*
|
(defn select-one*
|
||||||
|
|
@ -68,7 +68,7 @@
|
||||||
[path structure]
|
[path structure]
|
||||||
(compiled-select-one (i/comp-paths* path) structure))
|
(compiled-select-one (i/comp-paths* path) structure))
|
||||||
|
|
||||||
(def ^{:doc "Version of select-one! that takes in a path pre-compiled with comp-paths"}
|
(def ^{:doc "Version of select-one! that takes in a path precompiled with comp-paths"}
|
||||||
compiled-select-one! i/compiled-select-one!*)
|
compiled-select-one! i/compiled-select-one!*)
|
||||||
|
|
||||||
(defn select-one!*
|
(defn select-one!*
|
||||||
|
|
@ -76,7 +76,7 @@
|
||||||
[path structure]
|
[path structure]
|
||||||
(compiled-select-one! (i/comp-paths* path) structure))
|
(compiled-select-one! (i/comp-paths* path) structure))
|
||||||
|
|
||||||
(def ^{:doc "Version of select-first that takes in a path pre-compiled with comp-paths"}
|
(def ^{:doc "Version of select-first that takes in a path precompiled with comp-paths"}
|
||||||
compiled-select-first i/compiled-select-first*)
|
compiled-select-first i/compiled-select-first*)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -85,7 +85,7 @@
|
||||||
[path structure]
|
[path structure]
|
||||||
(compiled-select-first (i/comp-paths* path) structure))
|
(compiled-select-first (i/comp-paths* path) structure))
|
||||||
|
|
||||||
(def ^{:doc "Version of select-any that takes in a path pre-compiled with comp-paths"}
|
(def ^{:doc "Version of select-any that takes in a path precompiled with comp-paths"}
|
||||||
compiled-select-any i/compiled-select-any*)
|
compiled-select-any i/compiled-select-any*)
|
||||||
|
|
||||||
(def ^{:doc "Global value used to indicate no elements selected during
|
(def ^{:doc "Global value used to indicate no elements selected during
|
||||||
|
|
@ -98,7 +98,7 @@
|
||||||
[path structure]
|
[path structure]
|
||||||
(compiled-select-any (i/comp-paths* path) structure))
|
(compiled-select-any (i/comp-paths* path) structure))
|
||||||
|
|
||||||
(def ^{:doc "Version of selected-any? that takes in a path pre-compiled with comp-paths"}
|
(def ^{:doc "Version of selected-any? that takes in a path precompiled with comp-paths"}
|
||||||
compiled-selected-any? i/compiled-selected-any?*)
|
compiled-selected-any? i/compiled-selected-any?*)
|
||||||
|
|
||||||
(defn selected-any?*
|
(defn selected-any?*
|
||||||
|
|
@ -108,7 +108,7 @@
|
||||||
|
|
||||||
;; Reducible traverse functions
|
;; Reducible traverse functions
|
||||||
|
|
||||||
(def ^{:doc "Version of traverse that takes in a path pre-compiled with comp-paths"}
|
(def ^{:doc "Version of traverse that takes in a path precompiled with comp-paths"}
|
||||||
compiled-traverse i/do-compiled-traverse)
|
compiled-traverse i/do-compiled-traverse)
|
||||||
|
|
||||||
(defn traverse*
|
(defn traverse*
|
||||||
|
|
@ -119,7 +119,7 @@
|
||||||
|
|
||||||
;; Transformation functions
|
;; Transformation functions
|
||||||
|
|
||||||
(def ^{:doc "Version of transform that takes in a path pre-compiled with comp-paths"}
|
(def ^{:doc "Version of transform that takes in a path precompiled with comp-paths"}
|
||||||
compiled-transform i/compiled-transform*)
|
compiled-transform i/compiled-transform*)
|
||||||
|
|
||||||
(defn transform*
|
(defn transform*
|
||||||
|
|
@ -128,6 +128,18 @@
|
||||||
[path transform-fn structure]
|
[path transform-fn structure]
|
||||||
(compiled-transform (i/comp-paths* path) transform-fn structure))
|
(compiled-transform (i/comp-paths* path) transform-fn structure))
|
||||||
|
|
||||||
|
(def ^{:doc "Version of `multi-transform` that takes in a path precompiled with `comp-paths`"}
|
||||||
|
compiled-multi-transform i/compiled-multi-transform*)
|
||||||
|
|
||||||
|
|
||||||
|
(defn multi-transform*
|
||||||
|
"Just like `transform` but expects transform functions to be specified
|
||||||
|
inline in the path using `terminal`. Error is thrown if navigation finishes
|
||||||
|
at a non-`terminal` navigator."
|
||||||
|
[path structure]
|
||||||
|
(compiled-multi-transform (i/comp-paths* path) structure))
|
||||||
|
|
||||||
|
|
||||||
(def ^{:doc "Version of setval that takes in a path precompiled with comp-paths"}
|
(def ^{:doc "Version of setval that takes in a path precompiled with comp-paths"}
|
||||||
compiled-setval i/compiled-setval*)
|
compiled-setval i/compiled-setval*)
|
||||||
|
|
||||||
|
|
@ -193,6 +205,20 @@
|
||||||
(transform* [this structure next-fn]
|
(transform* [this structure next-fn]
|
||||||
(next-fn structure)))
|
(next-fn structure)))
|
||||||
|
|
||||||
|
|
||||||
|
(def
|
||||||
|
^{:doc "For usage with `multi-transform`, defines an endpoint in the navigation
|
||||||
|
that will have the parameterized transform function run. The transform
|
||||||
|
function works just like it does in `transform`, with collected values
|
||||||
|
given as the first arguments"}
|
||||||
|
terminal
|
||||||
|
(richnav 1
|
||||||
|
(select* [params params-idx vals structure next-fn]
|
||||||
|
(i/throw-illegal "'terminal' should only be used in multi-transform"))
|
||||||
|
(transform* [params params-idx vals structure next-fn]
|
||||||
|
(i/terminal* params params-idx vals structure)
|
||||||
|
)))
|
||||||
|
|
||||||
(def
|
(def
|
||||||
^{:doc "Navigate to every element of the collection. For maps navigates to
|
^{:doc "Navigate to every element of the collection. For maps navigates to
|
||||||
a vector of `[key value]`."}
|
a vector of `[key value]`."}
|
||||||
|
|
|
||||||
|
|
@ -1103,6 +1103,13 @@
|
||||||
next-fn
|
next-fn
|
||||||
)))
|
)))
|
||||||
|
|
||||||
|
(defn terminal* [params params-idx vals structure]
|
||||||
|
(let [afn (aget ^objects params params-idx)]
|
||||||
|
(if (identical? vals [])
|
||||||
|
(afn structure)
|
||||||
|
(apply afn (conj vals structure)))
|
||||||
|
))
|
||||||
|
|
||||||
(defn filter-select [afn structure next-fn]
|
(defn filter-select [afn structure next-fn]
|
||||||
(if (afn structure)
|
(if (afn structure)
|
||||||
(next-fn structure)
|
(next-fn structure)
|
||||||
|
|
@ -1481,6 +1488,14 @@
|
||||||
(get-cell state)]
|
(get-cell state)]
|
||||||
))
|
))
|
||||||
|
|
||||||
|
(defn- multi-transform-error-fn [& nav]
|
||||||
|
(throw-illegal
|
||||||
|
"All navigation in multi-transform must end in 'terminal' "
|
||||||
|
"navigators. Instead navigated to: " nav))
|
||||||
|
|
||||||
|
(defn compiled-multi-transform* [path structure]
|
||||||
|
(compiled-transform* path multi-transform-error-fn structure))
|
||||||
|
|
||||||
#+clj
|
#+clj
|
||||||
(defn extend-protocolpath* [protpath protpath-prot extensions]
|
(defn extend-protocolpath* [protpath protpath-prot extensions]
|
||||||
(let [extensions (partition 2 extensions)
|
(let [extensions (partition 2 extensions)
|
||||||
|
|
|
||||||
|
|
@ -643,6 +643,17 @@
|
||||||
[apath transform-fn structure]
|
[apath transform-fn structure]
|
||||||
`(i/compiled-transform* (path ~apath) ~transform-fn ~structure))
|
`(i/compiled-transform* (path ~apath) ~transform-fn ~structure))
|
||||||
|
|
||||||
|
(defmacro multi-transform
|
||||||
|
"Just like `transform` but expects transform functions to be specified
|
||||||
|
inline in the path using `terminal`. Error is thrown if navigation finishes
|
||||||
|
at a non-`terminal` navigator.
|
||||||
|
This macro will attempt to do inline factoring and caching of the path, falling
|
||||||
|
back to compiling the path on every invocation if it's not possible to
|
||||||
|
factor/cache the path."
|
||||||
|
[apath structure]
|
||||||
|
`(i/compiled-multi-transform* (path ~apath) ~structure))
|
||||||
|
|
||||||
|
|
||||||
(defmacro setval
|
(defmacro setval
|
||||||
"Navigates to each value specified by the path and replaces it by `aval`.
|
"Navigates to each value specified by the path and replaces it by `aval`.
|
||||||
This macro will attempt to do inline factoring and caching of the path, falling
|
This macro will attempt to do inline factoring and caching of the path, falling
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue