Skip to content

Commit e567184

Browse files
committed
Correct code example in quasiquote explanation
This example was out of date and errored when run manually; it wasn't being automatically tested with tests-ex-markdown (txm), which would have caught the regression. This change adds explanatory comments, and txm metadata to ensure the example is automatically tested in the future. Fixes anko#37.
1 parent 468894e commit e567184

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

doc/how-macros-work.markdown

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,31 @@ an array of stuff all at once.
111111
For example, if you want to create a shorthand `mean` for calculating the mean
112112
of some numbers, you could do
113113

114+
<!-- !test in mean macro -->
115+
114116
(macro mean
115117
(lambda ()
116-
; convert arguments to Array
117-
(var args ((. Array prototype slice call) arguments 0))
118-
(var total ((. this atom) (. args length)))
118+
119+
; Convert arguments object to an array
120+
(var argumentsAsArray ((. Array prototype slice call) arguments 0))
121+
122+
; Make an eslisp list object from the arguments
123+
(var args ((. this list apply) null argumentsAsArray))
124+
125+
; Make an eslisp atom representing the number of arguments
126+
(var total ((. this atom) (. arguments length)))
127+
128+
; Return a division of the sum of the arguments by the total
119129
(return `(/ (+ ,@args) ,total))))
120130

121-
(mean 1 2 3) ; call it
131+
(mean 1 2 3)
122132

123133
which effectively creates the eslisp code `(/ (+ 1 2 3) 3)` that compiles to JS
124-
as `(1 + (2 + 3)) / 3;`
134+
as—
135+
136+
<!-- !test out mean macro -->
137+
138+
(1 + (2 + 3)) / 3;
125139

126140
If we had used the plain unquote (`,`) instead of unquote-splicing (`,@`), we'd
127141
have gotten `(/ (+ (1 2 3)) 3)` which would compile to nonsense JS, as eslisp

0 commit comments

Comments
 (0)