This is the clj-arrows Reference Manual, version 0.1.0, generated automatically by Declt version 4.0 beta 2 "William Riker" on Sun Dec 15 05:40:42 2024 GMT+0.
The main system appears first, followed by any subsystem dependency.
clj-arrows
Implements Clojure-styled threading/transformation macros.
Dave Tenny
MIT
0.1.0
package.lisp
(file).
clj-arrows.lisp
(file).
Files are sorted by type and then listed depth-first from the systems components trees.
clj-arrows/clj-arrows.lisp
clj-arrows
(system).
cond-macro
(function).
diamond-macro
(function).
diamond-symbol-p
(function).
maybe-subst-diamonds
(function).
some-diamond-macro
(function).
some-macro
(function).
Packages are listed by definition order.
clj-arrows
Arrow macros that follow clojure as closely as possible. Also adds some basic diamond wand macros because I just couldn’t resist the temptation.
common-lisp
.
cond-macro
(function).
diamond-macro
(function).
diamond-symbol-p
(function).
maybe-subst-diamonds
(function).
some-diamond-macro
(function).
some-macro
(function).
Definitions are sorted by export status, category, package, and then by lexicographic order.
So-called ’diamond wand’ replaces top-level references to ‘<>‘ with the
threaded expr/form. If there is no reference to ‘<>‘, behaves like the
thread-first (‘->‘) macro. Note that this implementation allows ‘<>‘ to be
referenced multiple times in a form, and will use a LET binding to avoid
re-evaluation of the substitued expression.
E.g.
“‘
(defun foo () (print "hey!") 5)
(-<> (foo) (list 1 <> <> 2) (list 3 <> <> 4)) => (3 (1 5 5 2) (1 5 5 2) 4)
“‘
Note that the ’hey!’ is printed only once.
Note also that the above example works identically in both ‘-<>‘ and ‘-<>>‘,
the behave differently only in forms which do not use the ‘<>‘ substitution.
So-called ’diamond spear’ replaces top-level references to ‘<>‘ with the
threaded expr/form. If there is no reference to ‘<>‘, behaves like the
thread-last (‘->>‘) macro. Note that this implementation allows ‘<>‘ to be
referenced multiple times in a form, and will use a LET binding to avoid
re-evaluation of the substitued expression.
E.g. (-<>> 10 (list 1 <> 3)) => (1 10 3)
(-<>> 10 (list 1 3)) => (1 3 10)
See ‘-<>‘ for more substitution examples which would work identically between the first and last threading forms of diamond macros.
Threads the expr through the forms. Inserts x as the
second item in the first form, making a list of it if it is not a
list already. If there are more forms, inserts the first form as the
second item in second form, etc.
E.g. ‘(-> 3 (/ 2) /)‘
expands to ‘(/ (/ 3 2))‘
=> 2/3
Threads the expr through the forms. Inserts x as the
last item in the first form, making a list of it if it is not a
list already. If there are more forms, inserts the first form as the
last item in second form, etc.
E.g. ‘(->> 3 (/ 2) /)‘
expands to ‘(/ (/ 2 3))‘
=> 3/2
Binds name to expr, evaluates the first form in the lexical context
of that binding, then binds name to that result, repeating for each
successive form, returning the result of the last form.
E.g. ‘(as-> 3 $ (* 5 $) (/ $ 7))‘
expands to
“‘
(LET* (($ 3)
($ (* 5 $)))
(/ $ 7))
“‘
=> 15/7
Takes an expression and a set of test/form pairs. Threads expr (via ->)
through each form for which the corresponding test
expression is true. Note that, unlike cond branching, cond-> threading does
not short circuit after the first true test expression.
Note that the substitution is never in the tests, only the expression executed
if the test was true. Also note that this macro, adopting Clojure’s namesake,
does not have an additional set of parenthesis around the ‘test expr‘ pairs, you
may need to do a PROGN if you want to evaluate multiple expressions.
E.g.
“‘
(cond-> 1
t incf
nil (* 42)
(= 2 2) (* 3))
“‘
expands to
“‘
(LET* ((#:G1492 1)
(#:G1492
(IF T
(-> #:G1492 INCF)
#:G1492))
(#:G1492
(IF NIL
(-> #:G1492 (* 42))
#:G1492)))
(IF (= 2 2)
(-> #:G1492 (* 3))
#:G1492))
“‘
=> 6
Takes an expression and a set of test/form pairs. Threads expr (via ->>)
through each form for which the corresponding test expression
is true. Note that, unlike cond branching, cond->> threading does not short circuit
after the first true test expression.
Note that the substitution is never in the tests, the threading only occurs
only the expression executed if the test was true. Also note that this macro,
adopting Clojure’s namesake, does not have an additional set of parenthesis
around the ‘test expr‘ pairs, you may need to do a PROGN if you want to
evaluate multiple expressions.
E.g.
“‘
(cond->> nil
(oddp 1) (cons 1)
(oddp 2) (cons 2)
(oddp 3) (cons 3))
“‘
=> (3 1)
When expr is not nil, threads it into the first form (via ‘-<>‘),
and when that result is not nil, through the next etc, performing diamond substitutions
in the same way as ‘-<>‘.
See ‘-<>‘ for more details on substitutions or lack thereof. Without ‘<>‘ substitutions
‘some-<>‘ behaves identically to ‘some->‘.
E.g. ‘(some-<> ’((:a . 1) (:b . 2)) (assoc :b <>) cdr 1+)‘ => 3
‘(some-<> 5 (list 1 2) (list 3 4))‘ => ((5 1 2) 3 4)
When expr is not nil, threads it into the first form (via ‘-<>>‘),
and when that result is not nil, through the next etc, performing diamond substitutions
in the same way as ‘-<>>‘.
See ‘-<>>‘ for more details on substitutions or lack thereof. Without ‘<>‘ substitutions
‘some-<>>‘ behaves identically to ‘some->>‘.
E.g. ‘(some-<>> ’(:a 1 :b 2) (getf <> :b) 1+)‘ => 3
‘(some-<>> 5 (list 1 2) (list 3 4))‘ => (3 4 (1 2 5))
When expr is not nil, threads it into the first form (via ->),
and when that result is not nil, through the next etc.
E.g. ‘(some-> ’(:a 1 :b 2) (getf :b) 1+)‘ => 3
‘(some-> ’(:a 1 :b 2) (getf :c) 1+)‘ => NIL
When expr is not nil, threads it into the first form (via ->>),
and when that result is not nil, through the next etc.
E.g. ‘(some->> ’((:a . 1) (:b . 2)) (assoc :b) cdr 1+)‘ => 3 ‘(some->> ’((:a . 1) (:b . 2)) (assoc :c) cdr 1+)‘ => NIL
Refactored cond-> logic, varies only by threading semantics. THREADING is the symbol for either ’-> or ’->>’.
Utility routine to thread forms like ‘->‘ or ‘->>‘ unless there are ‘<>‘ substitutions.
Check for the diamond substitution symbol, ignoring the package in which it resides.
Thread a form as if for ‘->‘ or ‘->>‘ unless the form contains diamonds to be substituted
Utility routine to thread forms like ‘some->‘ or ‘some->>‘ unless there are ‘<>‘ substitutions.
Refactored some-> logic, varies only by threading semantics. THREADING is the symbol for either ’-> or ’->>’.
Jump to: | -
A C D F M S |
---|
Jump to: | -
A C D F M S |
---|
Jump to: | C F P S |
---|
Jump to: | C F P S |
---|