clean up terminology usage by replacing selector with navigator or path as appropriate

This commit is contained in:
Nathan Marz 2016-04-21 10:59:35 -04:00
parent 0014e413b0
commit a9aafc3eb8

View file

@ -24,81 +24,81 @@
(defn comp-paths [& paths] (defn comp-paths [& paths]
(i/comp-paths* (vec paths))) (i/comp-paths* (vec paths)))
;; Selector functions ;; Selection functions
(def ^{:doc "Version of select that takes in a selector pre-compiled with comp-paths"} (def ^{:doc "Version of select that takes in a path pre-compiled with comp-paths"}
compiled-select i/compiled-select*) compiled-select i/compiled-select*)
(defn select (defn select
"Navigates to and returns a sequence of all the elements specified by the selector." "Navigates to and returns a sequence of all the elements specified by the path."
[selector structure] [path structure]
(compiled-select (i/comp-paths* selector) (compiled-select (i/comp-paths* path)
structure)) structure))
(defn compiled-select-one (defn compiled-select-one
"Version of select-one that takes in a selector pre-compiled with comp-paths" "Version of select-one that takes in a path pre-compiled with comp-paths"
[selector structure] [path structure]
(let [res (compiled-select selector structure)] (let [res (compiled-select path structure)]
(when (> (count res) 1) (when (> (count res) 1)
(i/throw-illegal "More than one element found for params: " selector structure)) (i/throw-illegal "More than one element found for params: " path structure))
(first res) (first res)
)) ))
(defn select-one (defn select-one
"Like select, but returns either one element or nil. Throws exception if multiple elements found" "Like select, but returns either one element or nil. Throws exception if multiple elements found"
[selector structure] [path structure]
(compiled-select-one (i/comp-paths* selector) structure)) (compiled-select-one (i/comp-paths* path) structure))
(defn compiled-select-one! (defn compiled-select-one!
"Version of select-one! that takes in a selector pre-compiled with comp-paths" "Version of select-one! that takes in a path pre-compiled with comp-paths"
[selector structure] [path structure]
(let [res (compiled-select selector structure)] (let [res (compiled-select path structure)]
(when (not= 1 (count res)) (i/throw-illegal "Expected exactly one element for params: " selector structure)) (when (not= 1 (count res)) (i/throw-illegal "Expected exactly one element for params: " path structure))
(first res) (first res)
)) ))
(defn select-one! (defn select-one!
"Returns exactly one element, throws exception if zero or multiple elements found" "Returns exactly one element, throws exception if zero or multiple elements found"
[selector structure] [path structure]
(compiled-select-one! (i/comp-paths* selector) structure)) (compiled-select-one! (i/comp-paths* path) structure))
(defn compiled-select-first (defn compiled-select-first
"Version of select-first that takes in a selector pre-compiled with comp-paths" "Version of select-first that takes in a path pre-compiled with comp-paths"
[selector structure] [path structure]
(first (compiled-select selector structure))) (first (compiled-select path structure)))
(defn select-first (defn select-first
"Returns first element found. Not any more efficient than select, just a convenience" "Returns first element found. Not any more efficient than select, just a convenience"
[selector structure] [path structure]
(compiled-select-first (i/comp-paths* selector) structure)) (compiled-select-first (i/comp-paths* path) structure))
;; Transform functions ;; Transformation 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 path pre-compiled with comp-paths"}
compiled-transform i/compiled-transform*) compiled-transform i/compiled-transform*)
(defn transform (defn transform
"Navigates to each value specified by the selector and replaces it by the result of running "Navigates to each value specified by the path and replaces it by the result of running
the transform-fn on it" the transform-fn on it"
[selector transform-fn structure] [path transform-fn structure]
(compiled-transform (i/comp-paths* selector) transform-fn structure)) (compiled-transform (i/comp-paths* path) transform-fn structure))
(defn compiled-setval (defn compiled-setval
"Version of setval that takes in a selector pre-compiled with comp-paths" "Version of setval that takes in a path pre-compiled with comp-paths"
[selector val structure] [path val structure]
(compiled-transform selector (fn [_] val) structure)) (compiled-transform path (fn [_] val) structure))
(defn setval (defn setval
"Navigates to each value specified by the selector and replaces it by val" "Navigates to each value specified by the path and replaces it by val"
[selector val structure] [path val structure]
(compiled-setval (i/comp-paths* selector) val structure)) (compiled-setval (i/comp-paths* path) val structure))
(defn compiled-replace-in (defn compiled-replace-in
"Version of replace-in that takes in a selector pre-compiled with comp-paths" "Version of replace-in that takes in a path pre-compiled with comp-paths"
[selector transform-fn structure & {:keys [merge-fn] :or {merge-fn concat}}] [path transform-fn structure & {:keys [merge-fn] :or {merge-fn concat}}]
(let [state (i/mutable-cell nil)] (let [state (i/mutable-cell nil)]
[(compiled-transform selector [(compiled-transform path
(fn [& args] (fn [& args]
(let [res (apply transform-fn args)] (let [res (apply transform-fn args)]
(if res (if res
@ -119,12 +119,12 @@
what's used to transform the data structure, while user-ret will be added to the user-ret sequence what's used to transform the data structure, while user-ret will be added to the user-ret sequence
in the final return. replace-in is useful for situations where you need to know the specific values in the final return. replace-in is useful for situations where you need to know the specific values
of what was transformed in the data structure." of what was transformed in the data structure."
[selector transform-fn structure & {:keys [merge-fn] :or {merge-fn concat}}] [path transform-fn structure & {:keys [merge-fn] :or {merge-fn concat}}]
(compiled-replace-in (i/comp-paths* selector) transform-fn structure :merge-fn merge-fn)) (compiled-replace-in (i/comp-paths* path) transform-fn structure :merge-fn merge-fn))
;; Helpers for defining selectors and collectors with late-bound params ;; Helpers for defining selectors and collectors with late-bound params
(def ^{:doc "Takes a compiled selector that needs late-bound params and supplies it with (def ^{:doc "Takes a compiled path that needs late-bound params and supplies it with
an array of params and a position in the array from which to begin reading an array of params and a position in the array from which to begin reading
params. The return value is an executable selector."} params. The return value is an executable selector."}
bind-params* i/bind-params*) bind-params* i/bind-params*)
@ -159,7 +159,7 @@
)) ))
(defpath (defpath
^{:doc "Stays navigated at the current point. Essentially a no-op selector."} ^{:doc "Stays navigated at the current point. Essentially a no-op navigator."}
STAY STAY
[] []
(select* [this structure next-fn] (select* [this structure next-fn]
@ -291,12 +291,12 @@
)) ))
(defn selected? (defn selected?
"Filters the current value based on whether a selector finds anything. "Filters the current value based on whether a path finds anything.
e.g. (selected? :vals ALL even?) keeps the current element only if an e.g. (selected? :vals ALL even?) keeps the current element only if an
even number exists for the :vals key. even number exists for the :vals key.
The input path may be parameterized, in which case the result of selected? The input path may be parameterized, in which case the result of selected?
will be parameterized in the order of which the parameterized selectors will be parameterized in the order of which the parameterized navigators
were declared." were declared."
[& path] [& path]
(fixed-pathed-path [late path] (fixed-pathed-path [late path]
@ -326,8 +326,8 @@
(defn filterer (defn filterer
"Navigates to a view of the current sequence that only contains elements that "Navigates to a view of the current sequence that only contains elements that
match the given selector path. An element matches the selector path if calling select match the given path. An element matches the selector path if calling select
on that element with the selector path yields anything other than an empty sequence. on that element with the path yields anything other than an empty sequence.
The input path may be parameterized, in which case the result of filterer The input path may be parameterized, in which case the result of filterer
will be parameterized in the order of which the parameterized selectors will be parameterized in the order of which the parameterized selectors
@ -337,10 +337,10 @@
(defn transformed (defn transformed
"Navigates to a view of the current value by transforming it with the "Navigates to a view of the current value by transforming it with the
specified selector and update-fn. specified path and update-fn.
The input path may be parameterized, in which case the result of transformed The input path may be parameterized, in which case the result of transformed
will be parameterized in the order of which the parameterized selectors will be parameterized in the order of which the parameterized navigators
were declared." were declared."
[path update-fn] [path update-fn]
(fixed-pathed-path [late path] (fixed-pathed-path [late path]
@ -421,14 +421,14 @@
val )) val ))
(defn cond-path (defn cond-path
"Takes in alternating cond-path selector cond-path selector... "Takes in alternating cond-path path cond-path path...
Tests the structure if selecting with cond-path returns anything. Tests the structure if selecting with cond-path returns anything.
If so, it uses the following selector for this portion of the navigation. If so, it uses the following path for this portion of the navigation.
Otherwise, it tries the next cond-path. If nothing matches, then the structure Otherwise, it tries the next cond-path. If nothing matches, then the structure
is not selected. is not selected.
The input paths may be parameterized, in which case the result of cond-path The input paths may be parameterized, in which case the result of cond-path
will be parameterized in the order of which the parameterized selectors will be parameterized in the order of which the parameterized navigators
were declared." were declared."
[& conds] [& conds]
(variable-pathed-path [compiled-paths conds] (variable-pathed-path [compiled-paths conds]
@ -462,8 +462,8 @@
)) ))
(transform* [this structure next-fn] (transform* [this structure next-fn]
(reduce (reduce
(fn [structure selector] (fn [structure path]
(compiled-transform selector next-fn structure)) (compiled-transform path next-fn structure))
structure structure
compiled-paths compiled-paths
)))) ))))