The clj-arrows Reference Manual

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.

Table of Contents


1 Introduction


2 Systems

The main system appears first, followed by any subsystem dependency.


2.1 clj-arrows

Implements Clojure-styled threading/transformation macros.

Author

Dave Tenny

License

MIT

Version

0.1.0

Source

clj-arrows.asd.

Child Components

3 Files

Files are sorted by type and then listed depth-first from the systems components trees.


3.1 Lisp


3.1.1 clj-arrows/clj-arrows.asd

Source

clj-arrows.asd.

Parent Component

clj-arrows (system).

ASDF Systems

clj-arrows.

Packages

clj-arrows-asd.


3.1.2 clj-arrows/package.lisp

Source

clj-arrows.asd.

Parent Component

clj-arrows (system).

Packages

clj-arrows.


3.1.3 clj-arrows/clj-arrows.lisp

Source

clj-arrows.asd.

Parent Component

clj-arrows (system).

Public Interface
Internals

4 Packages

Packages are listed by definition order.


4.1 clj-arrows-asd

Source

clj-arrows.asd.

Use List
  • asdf/interface.
  • common-lisp.

4.2 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.

Source

package.lisp.

Use List

common-lisp.

Public Interface
Internals

5 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.


5.1 Public Interface


5.1.1 Macros

Macro: -<> (expr &rest forms)

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.

Package

clj-arrows.

Source

clj-arrows.lisp.

Macro: -<>> (expr &rest forms)

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.

Package

clj-arrows.

Source

clj-arrows.lisp.

Macro: -> (x &rest forms)

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

Package

clj-arrows.

Source

clj-arrows.lisp.

Macro: ->> (x &rest forms)

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

Package

clj-arrows.

Source

clj-arrows.lisp.

Macro: as-> (expr name &rest forms)

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

Package

clj-arrows.

Source

clj-arrows.lisp.

Macro: cond-> (expr &rest clauses)

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

Package

clj-arrows.

Source

clj-arrows.lisp.

Macro: cond->> (expr &rest clauses)

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)

Package

clj-arrows.

Source

clj-arrows.lisp.

Macro: some-<> (expr &rest forms)

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)

Package

clj-arrows.

Source

clj-arrows.lisp.

Macro: some-<>> (expr &rest forms)

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))

Package

clj-arrows.

Source

clj-arrows.lisp.

Macro: some-> (expr &rest forms)

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

Package

clj-arrows.

Source

clj-arrows.lisp.

Macro: some->> (expr &rest forms)

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

Package

clj-arrows.

Source

clj-arrows.lisp.


5.2 Internals


5.2.1 Ordinary functions

Function: cond-macro (threading expr clauses)

Refactored cond-> logic, varies only by threading semantics. THREADING is the symbol for either ’-> or ’->>’.

Package

clj-arrows.

Source

clj-arrows.lisp.

Function: diamond-macro (threading x forms)

Utility routine to thread forms like ‘->‘ or ‘->>‘ unless there are ‘<>‘ substitutions.

Package

clj-arrows.

Source

clj-arrows.lisp.

Function: diamond-symbol-p (exp)

Check for the diamond substitution symbol, ignoring the package in which it resides.

Package

clj-arrows.

Source

clj-arrows.lisp.

Function: maybe-subst-diamonds (threading x form)

Thread a form as if for ‘->‘ or ‘->>‘ unless the form contains diamonds to be substituted

Package

clj-arrows.

Source

clj-arrows.lisp.

Function: some-diamond-macro (threading expr forms)

Utility routine to thread forms like ‘some->‘ or ‘some->>‘ unless there are ‘<>‘ substitutions.

Package

clj-arrows.

Source

clj-arrows.lisp.

Function: some-macro (threading expr forms)

Refactored some-> logic, varies only by threading semantics. THREADING is the symbol for either ’-> or ’->>’.

Package

clj-arrows.

Source

clj-arrows.lisp.


Appendix A Indexes


A.1 Concepts


A.3 Variables