better macro example
This commit is contained in:
parent
d580351b41
commit
22d8a78330
@ -226,21 +226,23 @@ below:
|
|||||||
defmacro
|
defmacro
|
||||||
--------
|
--------
|
||||||
|
|
||||||
`defmacro` is used to define macros.
|
`defmacro` is used to define macros. The general format is
|
||||||
|
`(defmacro [parameters] expr)`.
|
||||||
|
|
||||||
The general format is `(defmacro [parameters] expr)`.
|
Following example defines a macro that can be used to swap order of elements in
|
||||||
|
code, allowing the user to write code in infix notation, where operator is in
|
||||||
Following example defines a macro that can be used to multiply all but the first
|
between the operands.
|
||||||
parameter given to it.
|
|
||||||
|
|
||||||
.. codeblock:: clj
|
.. codeblock:: clj
|
||||||
|
|
||||||
=> (defmacro multiply-some [&rest params] (quasiquote (* (unquote-splice (list (slice params 1))))))
|
=> (defmacro infix [code]
|
||||||
|
... (quasiquote (
|
||||||
|
... (unquote (get code 1))
|
||||||
|
... (unquote (get code 0))
|
||||||
|
... (unquote (get code 2)))))
|
||||||
|
|
||||||
=> (multiply-some 0 2 3)
|
=> (infix (1 + 1))
|
||||||
6
|
2
|
||||||
=> (multiply-some 2 0 3)
|
|
||||||
0
|
|
||||||
|
|
||||||
eval
|
eval
|
||||||
----
|
----
|
||||||
|
Loading…
x
Reference in New Issue
Block a user