From 22d8a783300c2504c7bfa8d1eb011af0fdb07d63 Mon Sep 17 00:00:00 2001 From: Tuukka Turto Date: Fri, 19 Jul 2013 12:06:23 +0300 Subject: [PATCH] better macro example --- docs/language/api.rst | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/docs/language/api.rst b/docs/language/api.rst index f3adaf1..cf3cd1c 100644 --- a/docs/language/api.rst +++ b/docs/language/api.rst @@ -226,21 +226,23 @@ below: 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 multiply all but the first -parameter given to it. +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 +between the operands. .. 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) - 6 - => (multiply-some 2 0 3) - 0 + => (infix (1 + 1)) + 2 eval ----