Autoformat specter.cljc

This commit is contained in:
djds 2022-10-28 18:50:00 +00:00
parent 548c18225a
commit 40cb36af48
No known key found for this signature in database

View file

@ -35,8 +35,7 @@
(defn- static-path? [path]
(if (sequential? path)
(every? static-path? path)
(-> path i/dynamic-param? not)
))
(-> path i/dynamic-param? not)))
(defn wrap-dynamic-nav [f]
(fn [& args]
@ -48,8 +47,7 @@
(first ret)
:else
ret
))))
ret))))
#?(:clj
(do
@ -74,7 +72,6 @@
(defmacro defcollector [name & body]
`(def ~name (collector ~@body)))
(defn- late-bound-operation [bindings builder-op impls]
(let [bindings (partition 2 bindings)
params (map first bindings)
@ -190,7 +187,6 @@
(let [[name args] (name-with-attributes name args)]
`(def ~name (dynamicnav ~@args))))
(defn- ic-prepare-path [locals-set path]
(cond
(vector? path)
@ -207,7 +203,6 @@
~(if-not (instance? Class (resolve path)) `(var ~path))
(quote ~path)))
(i/fn-invocation? path)
(let [[op & params] path]
;; need special case for 'fn since macroexpand does NOT
@ -219,13 +214,11 @@
~(mapv #(ic-prepare-path locals-set %) params)
(quote ~path))))
:else
(if (empty? (i/used-locals locals-set path))
path
`(com.rpl.specter.impl/->DynamicVal (quote ~path)))))
(defn- ic-possible-params [path]
(do
(mapcat
@ -241,10 +234,8 @@
(sequential? e)
(concat (if (vector? e) [e]) (ic-possible-params e))))
path)))
(defn- cljs-macroexpand [env form]
(let [expand-fn (i/cljs-analyzer-macroexpand-1)
mform (expand-fn env form)]
@ -259,12 +250,10 @@
(let [expanded (if (seq? form) (cljs-macroexpand env form) form)]
(cljwalk/walk #(cljs-macroexpand-all* env %) identity expanded))))
(defn- cljs-macroexpand-all [env form]
(let [ret (cljs-macroexpand-all* env form)]
ret))
(defmacro path
"Same as calling comp-paths, except it caches the composition of the static parts
of the path for later re-use (when possible). For almost all idiomatic uses
@ -343,7 +332,6 @@
~handle-params-code
~precompiled-sym))))
(defmacro select
"Navigates to and returns a sequence of all the elements specified by the path.
This macro will do inline caching of the path."
@ -403,7 +391,6 @@
[apath structure]
`(i/compiled-multi-transform* (path ~apath) ~structure))
(defmacro setval
"Navigates to each value specified by the path and replaces it by `aval`.
This macro will do inline caching of the path."
@ -440,14 +427,12 @@
[params & body]
`(i/collected?* (~'fn [~params] ~@body)))
(defn- protpath-sym [name]
(-> name (str "-prot") symbol))
(defn- protpath-meth-sym [name]
(-> name (str "-retrieve") symbol))
(defmacro defprotocolpath
"Defines a navigator that chooses the path to take based on the type
of the value at the current point. May be specified with parameters to
@ -505,11 +490,7 @@
~embed)))
(defmacro end-fn [& args]
`(n/->SrangeEndFunction (fn ~@args)))
))
`(n/->SrangeEndFunction (fn ~@args)))))
(defn comp-paths
"Returns a compiled version of the given path for use with
@ -547,7 +528,6 @@
(def ^{:doc "Version of select-first that takes in a path precompiled with comp-paths"}
compiled-select-first i/compiled-select-first*)
(defn select-first*
"Returns first element found."
[path structure]
@ -601,7 +581,6 @@
(def ^{:doc "Version of vtransform that takes in a path precompiled with comp-paths"}
compiled-vtransform i/compiled-vtransform*)
(defn transform*
"Navigates to each value specified by the path and replaces it by the result of running
the transform-fn on it"
@ -611,7 +590,6 @@
(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` or `vterminal`. Error is thrown if navigation finishes
@ -620,7 +598,6 @@
[path structure]
(compiled-multi-transform (i/comp-paths* path) structure))
(def ^{:doc "Version of setval that takes in a path precompiled with comp-paths"}
compiled-setval i/compiled-setval*)
@ -647,7 +624,6 @@
(def dynamic-param? i/dynamic-param?)
(def late-resolved-fn i/late-resolved-fn)
(defdynamicnav
^{:doc "Turns a navigator that takes one argument into a navigator that takes
many arguments and uses the same navigator with each argument. There
@ -658,7 +634,6 @@
(dynamicnav [& args]
(map latenavfn args))))
;; Helpers for making recursive or mutually recursive navs
(def local-declarepath i/local-declarepath)
@ -675,8 +650,6 @@
(transform* [this structure next-fn]
structure))
(def
^{:doc "Stays navigated at the current point. Essentially a no-op navigator."}
STAY
@ -732,8 +705,7 @@
(let [m (meta structure)
res (n/all-transform structure next-fn)]
(if (some? res)
(with-meta res m)
))))
(with-meta res m)))))
(defnav
^{:doc "Navigate to each value of the map. This is more efficient than
@ -757,7 +729,6 @@
(transform* [this structure next-fn]
(n/map-keys-transform structure next-fn)))
(defcollector VAL []
(collect-val [this structure]
structure))
@ -787,7 +758,6 @@
(let [s (start-index-fn structure)]
(n/srange-transform structure s (n/invoke-end-fn end-index-fn structure s) next-fn))))
(defnav
^{:doc "Navigates to the subsequence bound by the indexes start (inclusive)
and end (exclusive)"}
@ -798,7 +768,6 @@
(transform* [this structure next-fn]
(n/srange-transform structure start end next-fn)))
(defnav
^{:doc "Navigates to every continuous subsequence of elements matching `pred`"}
continuous-subseqs
@ -809,7 +778,6 @@
(transform* [this structure next-fn]
(i/continuous-subseqs-transform* pred structure next-fn)))
(defnav
^{:doc "Navigate to the empty subsequence before the first element of the collection."}
BEGINNING
@ -822,7 +790,6 @@
(let [to-prepend (next-fn [])]
(n/prepend-all structure to-prepend)))))
(defnav
^{:doc "Navigate to the empty subsequence after the last element of the collection."}
END
@ -849,8 +816,7 @@
structure
(if (nil? structure)
#{newe}
(conj structure newe)
)))))
(conj structure newe))))))
(defnav
^{:doc "Navigate to 'void' element before the sequence.
@ -864,8 +830,7 @@
(let [newe (next-fn NONE)]
(if (identical? NONE newe)
structure
(n/prepend-one structure newe)
))))
(n/prepend-one structure newe)))))
(defnav
^{:doc "Navigate to 'void' element after the sequence.
@ -879,8 +844,7 @@
(let [newe (next-fn NONE)]
(if (identical? NONE newe)
structure
(n/append-one structure newe)
))))
(n/append-one structure newe)))))
(defnav
^{:doc "Navigates to the specified subset (by taking an intersection).
@ -897,7 +861,6 @@
(set/difference subset)
(set/union newset)))))
(defnav
^{:doc "Navigates to the specified submap (using select-keys).
In a transform, that submap in the original map is changed to the new
@ -938,8 +901,7 @@
(if vs
(do (i/update-cell! values-to-insert next)
(first vs))
NONE
)))
NONE)))
structure)))))
(defrichnav
@ -951,18 +913,15 @@
(select* [this vals structure next-fn]
(if (contains? structure key)
(next-fn vals key)
NONE
))
NONE))
(transform* [this vals structure next-fn]
(if (contains? structure key)
(let [newkey (next-fn vals key)
dissoced (dissoc structure key)]
(if (identical? NONE newkey)
dissoced
(assoc dissoced newkey (get structure key))
))
structure
)))
(assoc dissoced newkey (get structure key))))
structure)))
(defrichnav
^{:doc "Navigates to the given element in the set only if it exists in the set.
@ -972,18 +931,15 @@
(select* [this vals structure next-fn]
(if (contains? structure elem)
(next-fn vals elem)
NONE
))
NONE))
(transform* [this vals structure next-fn]
(if (contains? structure elem)
(let [newelem (next-fn vals elem)
removed (disj structure elem)]
(if (identical? NONE newelem)
removed
(conj removed newelem)
))
structure
)))
(conj removed newelem)))
structure)))
(def ^{:doc "Navigate to the specified keys one after another. If navigate to NONE,
that element is removed from the map or vector."}
@ -1023,8 +979,7 @@
(select* [this vals structure next-fn]
(if (and (>= i 0) (< i (count structure)))
(next-fn vals i)
NONE
))
NONE))
(transform* [this vals structure next-fn]
(if (and (>= i 0) (< i (count structure)))
(let [newi (next-fn vals i)]
@ -1037,22 +992,17 @@
s structure]
(if (< j newi)
s
(recur (dec j) (assoc s (inc j) (nth s j)))
))
(recur (dec j) (assoc s (inc j) (nth s j)))))
(loop [j (inc i)
s structure]
(if (> j newi)
s
(recur (inc j) (assoc s (dec j) (nth s j)))
)))]
(assoc shifted newi v)
)
(recur (inc j) (assoc s (dec j) (nth s j))))))]
(assoc shifted newi v))
(->> structure
(setval (nthpath i) NONE)
(setval (before-index newi) v)
)))))
structure
)))
(setval (before-index newi) v))))))
structure)))
(defnav
^{:doc "Navigate to [index elem] pairs for each element in a sequence. The sequence will be indexed
@ -1065,8 +1015,7 @@
(let [i (i/mutable-cell (dec start))]
(doseqres NONE [e structure]
(i/update-cell! i inc)
(next-fn [(i/get-cell i) e])
)))
(next-fn [(i/get-cell i) e]))))
(transform* [this structure next-fn]
(let [indices (i/mutable-cell (-> structure count range))]
(reduce
@ -1080,15 +1029,12 @@
(let [ii2 (next ii)]
(if (> newi curri)
(transform [ALL #(>= % (inc curri)) #(<= % newi)] dec ii2)
ii2
))))
ii2))))
(->> s
(setval (nthpath curri) newe)
(setval (index-nav curri) newi)
)))
(setval (index-nav curri) newi))))
structure
structure
))))
structure))))
(def
^{:doc "`indexed-vals` with a starting index of 0."}
@ -1104,7 +1050,6 @@
(transform* [this vals structure next-fn]
(next-fn vals (afn structure))))
(defnav
^{:doc "Navigate to the result of running `parse-fn` on the value. For
transforms, the transformed value then has `unparse-fn` run on
@ -1116,7 +1061,6 @@
(transform* [this structure next-fn]
(unparse-fn (next-fn (parse-fn structure)))))
(defnav
^{:doc "Navigates to atom value."}
ATOM
@ -1204,8 +1148,7 @@
(select* [this structure next-fn]
(next-fn (reduce late-fn (compiled-traverse late structure))))
(transform* [this structure next-fn]
(next-fn (reduce late-fn (compiled-traverse late structure)))
)))
(next-fn (reduce late-fn (compiled-traverse late structure))))))
(def
^{:doc "Keeps the element only if it matches the supplied predicate. Functions in paths
@ -1214,7 +1157,6 @@
pred
i/pred*)
(defn ^:direct-nav pred= [v] (pred #(= % v)))
(defn ^:direct-nav pred< [v] (pred #(< % v)))
(defn ^:direct-nav pred> [v] (pred #(> % v)))
@ -1308,8 +1250,7 @@
ns (namespace structure)]
(cond (keyword? structure) (keyword ns new-name)
(symbol? structure) (symbol ns new-name)
:else (throw (ex-info "NAME can only be used on symbols or keywords" {:structure structure}))
))))
:else (throw (ex-info "NAME can only be used on symbols or keywords" {:structure structure}))))))
(defnav ^{:doc "Navigates to the namespace portion of the keyword or symbol"}
NAMESPACE
@ -1322,8 +1263,7 @@
(cond (keyword? structure) (keyword new-ns name)
(symbol? structure) (symbol new-ns name)
:else (throw (ex-info "NAMESPACE can only be used on symbols or keywords"
{:structure structure}))
))))
{:structure structure}))))))
(defdynamicnav
^{:doc "Adds the result of running select with the given path on the
@ -1334,7 +1274,6 @@
(collect-val [this structure]
(compiled-select late structure))))
(defdynamicnav
^{:doc "Adds the result of running select-one with the given path on the
current value to the collected vals."}
@ -1344,7 +1283,6 @@
(collect-val [this structure]
(compiled-select-one late structure))))
(defcollector
^{:doc
"Adds an external value to the collected vals. Useful when additional arguments
@ -1367,11 +1305,9 @@
[& path]
(late-bound-richnav [late (late-path path)]
(select* [this vals structure next-fn]
(i/exec-select* late [] structure (fn [_ structure] (next-fn vals structure)))
)
(i/exec-select* late [] structure (fn [_ structure] (next-fn vals structure))))
(transform* [this vals structure next-fn]
(i/exec-transform* late [] structure (fn [_ structure] (next-fn vals structure))))
))
(i/exec-transform* late [] structure (fn [_ structure] (next-fn vals structure))))))
(defrichnav
^{:doc "Drops all collected values for subsequent navigation."}
@ -1426,7 +1362,6 @@
late-then
late-else))))))
(defdynamicnav cond-path
"Takes in alternating cond-path path cond-path path...
Tests the structure if selecting with cond-path returns anything.
@ -1441,7 +1376,6 @@
STOP
pairs)))
(defdynamicnav multi-path
"A path that branches on multiple paths. For updates,
applies updates to the paths in order."
@ -1457,15 +1391,13 @@
(let [res2 (i/exec-select* late2 vals structure next-fn)]
(if (identical? NONE res1)
res2
res1
)))))
res1)))))
(transform* [this vals structure next-fn]
(let [s1 (i/exec-transform* late1 vals structure next-fn)]
(i/exec-transform* late2 vals s1 next-fn)))))
([path1 path2 & paths]
(reduce multi-path (multi-path path1 path2) paths)))
(defdynamicnav stay-then-continue
"Navigates to the current element and then navigates via the provided path.
This can be used to implement pre-order traversal."
@ -1485,16 +1417,14 @@
walker
(recursive-path [afn] p
(cond-path (pred afn) STAY
coll? [ALL p]
)))
coll? [ALL p])))
(def
^{:doc "Like `walker` but maintains metadata of any forms traversed."}
codewalker
(recursive-path [afn] p
(cond-path (pred afn) STAY
coll? [ALL-WITH-META p]
)))
coll? [ALL-WITH-META p])))
(let [empty->NONE (if-path empty? (terminal-val NONE))
compact* (fn [nav] (multi-path nav empty->NONE))]
@ -1502,5 +1432,4 @@
"During transforms, after each step of navigation in subpath check if the
value is empty. If so, remove that value by setting it to NONE."
[& path]
(map compact* path)
))
(map compact* path)))