(adapt-dance-fns dance)
(add-to-right-context ctx & kvs)
(atomic-dance? dance)
(atomic-dances dance)
(backtrack form)
(backtrack form ctx)
(backtrack? ctx)
(break-dance! form)
(break-dance! form ctx)
Used to immediately return a value during a dance
. Can be used
in any of the dance fns.
Used to immediately return a value during a [[dance]]. Can be used in any of the dance fns.
(context-walk step inner outer form context)
(dance form & args)
A finely tunable, composable, equivalent of clojure.walk with enhancements.
For a demo, just run
(dance
'(+ 1 (- 2 (* 3 (/ 4 5) (dec 3))))
:walk? #(and (seq? %) (-> % first (not= 'dec)))
:pre? #(and (seq? %) (-> % first (not= '/)))
:pre vec
:post? number?
:post (fn [x ctx] [(inc x) (update ctx :cnt inc)])
:context {:cnt 0}
:return :form-and-context
:debug true
)
See https://en.wikipedia.org/wiki/Tree_traversal
Namely walk?
, pre?
, pre
, post?
, post
, before
, after
,
before-all
and after-all
.
pre?
and post?
respectively condition pre
and post
while the
walk of the substructure itself occurs in between and is conditioned
by walk?
. before
and after
are respectively called before and
after any node is processed (before pre
) while before-all
and
after-all
are called once, at the beginning and the end of the walk.
Traversal appears to occur in pre-order for before
, walk?
pre?
and pre
, but in post-order for post?
, post
and after
.
Note that nodes that will not be walked may still be processed by
pre
and post
.
Dance fns can have 1 argument (the node at hand) or 2, in order to
receive an optional, latent context. If so, they must return a
vector like [result context]
where result
can be a processed
substructure (pre
, post
, ...) or the return value of a predicate
(pre?
, post?
, ...).
This context is passed walking down the structure from node to subnodes then back to the parent nodes up to the initial root in a depth-first manner.
By default, it is global to the walk and is not scoped by the depth at which it being accessed. In other words, the context is passed from siblings to siblings in the order of the walk and modifications done to it when walking a node can be seen when walking the sister nodes, their children and eventually their ancestors.
However the :scoped
option can be used to specify which keys in
the context should be scoped to the current node and its children
and not thread through the whole traversal. More specifically parents
pass the context to their children and changes made by the children are
not visibile by their ancestors or siblings.
Note that variadic arguments functions count as single-argument to
accomodate for functions like constantly
, and thus cannot receive
a context.
Multiple dances can be passed to dance
, including a varargs dance:
(dance collection
first-dance
second-dance
:before third-dance-before
:after third-dance-after)
Options in these dance will be merged in a smart way according to this plan:
- before, pre : composed from left to right
- after, post : composed from right to left
- pre?, walk? : composed like `and`, from left to right
- post? : composed like `and`, but from right to left.
- context : composed with `merge`
- scoped : composed with `#(clojure.set/union (set %1) (set %2))`
:debug
, :return
, and :step
are merged normally, i.e.
via right-most preference.
At any moment, from within any of the dance fns, break-dance!
can be used to halt the execution, returning the given form (and
optional context). Consider:
(dance [1 2 3 4 5 6]
:pre? number?
:pre (fn [x]
(if (> x 4)
(break-dance! :abc)
(println x))))
=> :abc
context
: The initial context (defaults to {}
)return
: :form
, :form-and-context
or :context
(:form).step
: low-level option. See step
and
step-indexed
. Defaults to step
depth
: The intial depth. Determines tabulation (0
)
when debugging.debug
: To print a detailed trace of the walk (false
).debug-depth
: To limit the debug trace to a certain depth.debug-context-blacklist
: To mask certain context keys when debugging.debug-context-whitelist
: To display only certain context keys.Any option passed to dance
is optional, including the dance fns.
A finely tunable, composable, equivalent of clojure.walk with enhancements. For a demo, just run ```clojure (dance '(+ 1 (- 2 (* 3 (/ 4 5) (dec 3)))) :walk? #(and (seq? %) (-> % first (not= 'dec))) :pre? #(and (seq? %) (-> % first (not= '/))) :pre vec :post? number? :post (fn [x ctx] [(inc x) (update ctx :cnt inc)]) :context {:cnt 0} :return :form-and-context :debug true ) ``` #### Dance fns See https://en.wikipedia.org/wiki/Tree_traversal Namely `walk?`, `pre?`, `pre`, `post?`, `post`, `before`, `after`, `before-all` and `after-all`. `pre?` and `post?` respectively condition `pre` and `post` while the walk of the substructure itself occurs in between and is conditioned by `walk?`. `before`and `after` are respectively called before and after any node is processed (before `pre`) while `before-all` and `after-all` are called once, at the beginning and the end of the walk. Traversal appears to occur in pre-order for `before`, `walk?` `pre?` and `pre`, but in post-order for `post?`, `post` and `after`. Note that nodes that will not be walked may still be processed by `pre` and `post`. #### Context Dance fns can have 1 argument (the node at hand) or 2, in order to receive an optional, latent context. If so, they must return a vector like `[result context]` where `result` can be a processed substructure (`pre`, `post`, ...) or the return value of a predicate (`pre?`, `post?`, ...). This context is passed walking down the structure from node to subnodes then back to the parent nodes up to the initial root in a depth-first manner. By default, it is global to the walk and is not scoped by the depth at which it being accessed. In other words, the context is passed from siblings to siblings in the order of the walk and modifications done to it when walking a node can be seen when walking the sister nodes, their children and eventually their ancestors. However the `:scoped` option can be used to specify which keys in the context should be scoped to the current node and its children and not thread through the whole traversal. More specifically parents pass the context to their children and changes made by the children are not visibile by their ancestors or siblings. Note that variadic arguments functions count as single-argument to accomodate for functions like `constantly`, and thus cannot receive a context. #### Merging dances Multiple dances can be passed to `dance`, including a varargs dance: ```clojure (dance collection first-dance second-dance :before third-dance-before :after third-dance-after) ``` Options in these dance will be merged in a smart way according to this plan: ``` - before, pre : composed from left to right - after, post : composed from right to left - pre?, walk? : composed like `and`, from left to right - post? : composed like `and`, but from right to left. - context : composed with `merge` - scoped : composed with `#(clojure.set/union (set %1) (set %2))` ``` `:debug`, `:return `, and `:step` are merged normally, i.e. via right-most preference. #### Early return At any moment, from within any of the dance fns, [[break-dance!]] can be used to halt the execution, returning the given form (and optional context). Consider: ```clojure (dance [1 2 3 4 5 6] :pre? number? :pre (fn [x] (if (> x 4) (break-dance! :abc) (println x)))) => :abc ``` #### Additional options - `context` : The initial context (defaults to `{}`) - `return` : `:form`, `:form-and-context` or `:context` (:form). - `step` : low-level option. See [[step]] and [[step-indexed]]. Defaults to [[step]] - `depth` : The intial depth. Determines tabulation (`0`) when debugging. - `debug` : To print a detailed trace of the walk (`false`). - `debug-depth` : To limit the debug trace to a certain depth. - `debug-context-blacklist`: To mask certain context keys when debugging. - `debug-context-whitelist`: To display only certain context keys. Any option passed to `dance` is optional, including the dance fns.
(dance-impl dance)
(dance-name dance)
(def-collecting-dance nme & args)
(defdance nme & args)
(dependent-dances dance)
A dance that keeps track of the depth of the traversal. Context key: :depth.
A dance that keeps track of the depth of the traversal. Context key: :depth.
Accepts an optional :bound-sym? function in the context.
Accepts an optional :bound-sym? function in the context.
(get-right-context ctx)
A dance that keeps track of the index the current nodes resides at in its parent node. The index can be either a number for list/vectors or anything else for maps. Context key: :index.
A dance that keeps track of the index the current nodes resides at in its parent node. The index can be either a number for list/vectors or anything else for maps. Context key: :index.
See [[locals-tracking-dance--fn*-multiple-bodies]].
See [[locals-tracking-dance--fn*-multiple-bodies]].
See [[locals-tracking-dance--fn*-single-body]].
See [[locals-tracking-dance--fn*-single-body]].
See [[locals-tracking-dance--handle-locals-from-left]].
See [[locals-tracking-dance--handle-locals-from-left]].
See [[locals-tracking-dance--let*-loop*-letfn*-binding-sym-expr]].
See [[locals-tracking-dance--let*-loop*-letfn*-binding-sym-expr]].
See [[locals-tracking-dance--let*-loop*-letfn*-binding-vec]].
See [[locals-tracking-dance--let*-loop*-letfn*-binding-vec]].
See [[locals-tracking-dance--top-level]].
See [[locals-tracking-dance--top-level]].
(map-entry x)
(merge-dances & dances)
Merges multiples dances together. A dance is a hash of arguments
to dance
.
Merges multiples dances together. A dance is a hash of arguments to [[dance]].
A dance that keeps track of the parent node. Context keys: :parent (:next-parent).
A dance that keeps track of the parent node. Context keys: :parent (:next-parent).
A dance that keeps track of the parent nodes, in genealogic order (parent, grand-parent, ...). Context keys: :parents (:next-parent).
A dance that keeps track of the parent nodes, in genealogic order (parent, grand-parent, ...). Context keys: :parents (:next-parent).
A dance that keeps track of the path from the root of the datastructure to the current node. To use a path to find back the node it leads to, use shuriken.core/get-nth-in and sister functions since contrary to get-in, assoc, etc ..., they support traversing sequences. Context key: :path.
A dance that keeps track of the path from the root of the datastructure to the current node. To use a path to find back the node it leads to, use shuriken.core/get-nth-in and sister functions since contrary to get-in, assoc, etc ..., they support traversing sequences. Context key: :path.
(splice form)
(step form {:keys [initial-acc initial-context wrap inner outer]})
(step-indexed form {:keys [initial-acc initial-context wrap inner outer]})
(subdances dance)
(update-right-context ctx k f & args)
(with-context ctx)
(with-context form ctx)
(with-context? form)
(with-dance-impl impl dance)
(with-dance-name dance-name dance)
(with-dependent-dances dependent-dances dance)
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close