60
60
We could of course have written the macro function in eslisp instead:
61
61
62
62
(= (. module exports)
63
- (function (name) (return (array (object atom "=") name "hello"))))
63
+ (lambda (name) (return (array (object atom "=") name "hello"))))
64
64
65
65
That compiles to the same JS before. You can write macros in any language you
66
66
want, as long as you can compile it to JS before ` require ` -ing it from eslisp.
@@ -73,7 +73,7 @@ about that next:
73
73
To make macros clearer to read, eslisp has special syntax for returning stuff
74
74
that represents code. Let's rewrite the previous hello-assigning macro:
75
75
76
- (= (. module exports) (function (name) (return `(= ,name "hello"))))
76
+ (= (. module exports) (lambda (name) (return `(= ,name "hello"))))
77
77
78
78
That does exactly the same thing, but it contains less of the
79
79
` array ` /` object ` fluff, so it's clearer to read. The ` array ` constructor
@@ -103,7 +103,7 @@ For example, if you want to create a shorthand `mean` for calculating the mean
103
103
of some numbers, you could do
104
104
105
105
(macro mean
106
- (function ()
106
+ (lambda ()
107
107
; convert arguments to Array
108
108
(var args ((. Array prototype slice call) arguments 0))
109
109
(var total (. args length))
@@ -150,11 +150,11 @@ list.
150
150
<!-- !test in function-expression scope macro -->
151
151
152
152
; Define at root scope
153
- (macro one (function () (return '1)))
153
+ (macro one (lambda () (return '1)))
154
154
155
- (function ()
155
+ (lambda ()
156
156
; Redefine the macro in an inner scope
157
- (macro one (function () (return '1.1))) ; "very large value of 1"
157
+ (macro one (lambda () (return '1.1))) ; "very large value of 1"
158
158
159
159
((. console log) (one)))
160
160
@@ -173,7 +173,7 @@ nesting level.
173
173
174
174
<!-- !test in scoped macro masking -->
175
175
176
- (macro ninja (function () (return `stealthMode))) ; define macro
176
+ (macro ninja (lambda () (return `stealthMode))) ; define macro
177
177
(if seriousBusiness ; in inner scope...
178
178
(block (macro ninja) ; mask it
179
179
(ninja))) ; function call
@@ -194,10 +194,10 @@ exist in a new, independent scope.
194
194
<!-- !test in non-capturing macro -->
195
195
196
196
; Define a macro "ok".
197
- (macro ok (function () (return 'null)))
197
+ (macro ok (lambda () (return 'null)))
198
198
199
199
; Define a non-capturing macro that expects "ok" not to be defined.
200
- (macro callOk (function (x)
200
+ (macro callOk (lambda (x)
201
201
(return `(ok)))) ; expects this to compile to calling a function "ok"
202
202
203
203
; Which it does, despite a macro "ok" being defined!
@@ -218,10 +218,10 @@ reset the macro environment.
218
218
<!-- !test in capturing macro -->
219
219
220
220
; Define a macro "ok".C
221
- (macro ok (function () (return 'null)))
221
+ (macro ok (lambda () (return 'null)))
222
222
223
223
; Define a capturing macro.
224
- (capmacro callOk (function (x)
224
+ (capmacro callOk (lambda (x)
225
225
(return `(ok)))) ; expects this to compile to calling the macro "ok"
226
226
; (NOT the function "ok"!)
227
227
@@ -262,7 +262,7 @@ call it with multiple arguments and return that.
262
262
<!-- !test in increment twice -->
263
263
264
264
(macro incrementTwice
265
- (function (x) (return ((. this multi) `(++ ,x) `(++ ,x)))))
265
+ (lambda (x) (return ((. this multi) `(++ ,x) `(++ ,x)))))
266
266
267
267
(incrementTwice hello)
268
268
@@ -282,7 +282,7 @@ For example, you might want to pre-compute some expression.
282
282
<!-- !test in precompute -->
283
283
284
284
(macro precompute
285
- (function (list) (return `,((. this evaluate) list))))
285
+ (lambda (list) (return `,((. this evaluate) list))))
286
286
287
287
(precompute (+ 1 2 (* 5 (. Math PI))))
288
288
@@ -309,7 +309,7 @@ shouldn't conflict with anything else.
309
309
310
310
; Generate the assignments needed to swap the values of two variables
311
311
(macro swap
312
- (function (varA varB)
312
+ (lambda (varA varB)
313
313
(var swapVar ((. this gensym))) ; Generate a new symbol we can use
314
314
(return ((. this multi)
315
315
@@ -343,7 +343,7 @@ implicitly return the last thing in their bodies if it's an expression.
343
343
344
344
<!-- !test in implicit-return function -->
345
345
346
- (macro fn (function ()
346
+ (macro fn (lambda ()
347
347
(var args ((. Array prototype slice call) arguments))
348
348
(var fnArgs (. args 0))
349
349
(var fnBody ((. args slice) 1))
@@ -358,7 +358,7 @@ implicitly return the last thing in their bodies if it's an expression.
358
358
((. fnBody push) lastConverted) ; push the maybe-converted thing back on
359
359
360
360
; return the function definition
361
- (return `(function ,fnArgs ,@fnBody))))
361
+ (return `(lambda ,fnArgs ,@fnBody))))
362
362
363
363
(fn (a b) (+ a b))
364
364
0 commit comments