From 06d41f7cd05652e8e80f266ff73bbba71a69eb5d Mon Sep 17 00:00:00 2001 From: Jack Hooper Date: Sat, 18 Jan 2014 16:33:40 +1100 Subject: [PATCH 1/6] Corrected chessboard example A chessboard is 8 x 8. Yours was 8 x 9. --- docs/tutorial.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/tutorial.rst b/docs/tutorial.rst index 819b7cd..f00fd98 100644 --- a/docs/tutorial.rst +++ b/docs/tutorial.rst @@ -363,7 +363,7 @@ In hy, you could do these like: (list-comp (, x y) - (x (range 9) + (x (range 8) y "ABCDEFGH")) ; [(0, 'A'), (0, 'B'), (0, 'C'), (0, 'D'), (0, 'E'), (0, 'F'), (0, 'G'), (0, 'H'), @@ -374,7 +374,6 @@ In hy, you could do these like: ; (5, 'A'), (5, 'B'), (5, 'C'), (5, 'D'), (5, 'E'), (5, 'F'), (5, 'G'), (5, 'H'), ; (6, 'A'), (6, 'B'), (6, 'C'), (6, 'D'), (6, 'E'), (6, 'F'), (6, 'G'), (6, 'H'), ; (7, 'A'), (7, 'B'), (7, 'C'), (7, 'D'), (7, 'E'), (7, 'F'), (7, 'G'), (7, 'H'), - ; (8, 'A'), (8, 'B'), (8, 'C'), (8, 'D'), (8, 'E'), (8, 'F'), (8, 'G'), (8, 'H')] Python has support for various fancy argument and keyword arguments. From d028b8a7b23f138358a0ca863b7420aab7389250 Mon Sep 17 00:00:00 2001 From: Jack Hooper Date: Sat, 18 Jan 2014 22:22:33 +1100 Subject: [PATCH 2/6] Added closing bracket ']' --- docs/tutorial.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorial.rst b/docs/tutorial.rst index f00fd98..ceae5a8 100644 --- a/docs/tutorial.rst +++ b/docs/tutorial.rst @@ -373,7 +373,7 @@ In hy, you could do these like: ; (4, 'A'), (4, 'B'), (4, 'C'), (4, 'D'), (4, 'E'), (4, 'F'), (4, 'G'), (4, 'H'), ; (5, 'A'), (5, 'B'), (5, 'C'), (5, 'D'), (5, 'E'), (5, 'F'), (5, 'G'), (5, 'H'), ; (6, 'A'), (6, 'B'), (6, 'C'), (6, 'D'), (6, 'E'), (6, 'F'), (6, 'G'), (6, 'H'), - ; (7, 'A'), (7, 'B'), (7, 'C'), (7, 'D'), (7, 'E'), (7, 'F'), (7, 'G'), (7, 'H'), + ; (7, 'A'), (7, 'B'), (7, 'C'), (7, 'D'), (7, 'E'), (7, 'F'), (7, 'G'), (7, 'H')] Python has support for various fancy argument and keyword arguments. From 21709f0199424df5b400c24ef5612219dec3ba13 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Fri, 17 Jan 2014 12:47:05 +0100 Subject: [PATCH 3/6] hy/core/macros.hy: Add defn-alias / defun-alias In the same vein as defmacro-alias, this implements defn-alias / defun-alias, which does essentially the same thing as defmacro-alias, but for functions. Signed-off-by: Gergely Nagy --- docs/language/api.rst | 25 +++++++++++++++++++++++++ hy/core/macros.hy | 11 +++++++++++ tests/native_tests/native_macros.hy | 8 ++++++++ 3 files changed, 44 insertions(+) diff --git a/docs/language/api.rst b/docs/language/api.rst index 7a3639c..423c50b 100644 --- a/docs/language/api.rst +++ b/docs/language/api.rst @@ -352,6 +352,8 @@ below: Meow +.. _defn: + defn / defun ------------ @@ -422,6 +424,29 @@ Parameters may have following keywords in front of them: => (zig-zag-sum 1 2 3 4 5 6) -3 +.. _defn-alias / defun-alias: + +defn-alias / defun-alias +------------------------ + +.. versionadded:: 0.9.13 + +The `defn-alias` and `defun-alias` macros are much like `defn`_ above, +with the difference that instead of defining a function with a single +name, these can also define aliases. Other than taking a list of +symbols for function names as the first parameter, `defn-alias` and +`defun-alias` have no other differences compared to `defn` and +`defun`. + +.. code-block:: clj + + => (defn-alias [main-name alias] [] + ... (print "Hello!")) + => (main-name) + "Hello!" + => (alias) + "Hello!" + .. _defmacro: defmacro diff --git a/hy/core/macros.hy b/hy/core/macros.hy index 9219975..d00ccce 100644 --- a/hy/core/macros.hy +++ b/hy/core/macros.hy @@ -181,3 +181,14 @@ (setv -args (cdr (car -args)))) `(apply ~-fun [~@-args] (dict (sum ~-okwargs []))))) + + +(defmacro-alias [defn-alias defun-alias] [names lambda-list &rest body] + "define one function with several names" + (let [[main (first names)] + [aliases (rest names)]] + (setv ret `(do (defn ~main ~lambda-list ~@body))) + (for* [name aliases] + (.append ret + `(setv ~name ~main))) + ret)) diff --git a/tests/native_tests/native_macros.hy b/tests/native_tests/native_macros.hy index 0841b1e..cf25118 100644 --- a/tests/native_tests/native_macros.hy +++ b/tests/native_tests/native_macros.hy @@ -176,3 +176,11 @@ (assert (in ":res_" s1)) (assert (in ":res_" s2)) (assert (not (= s1 s2)))) + +(defn test-defn-alias [] + (defn-alias [tda-main tda-a1 tda-a2] [] :bazinga) + (defun-alias [tda-main tda-a1 tda-a2] [] :bazinga) + (assert (= (tda-main) :bazinga)) + (assert (= (tda-a1) :bazinga)) + (assert (= (tda-a2) :bazinga)) + (assert (= tda-main tda-a1 tda-a2))) From ca40113e37ee5ba16b6cdfe6dca8242b6b9e096f Mon Sep 17 00:00:00 2001 From: Jack Hooper Date: Mon, 20 Jan 2014 14:29:57 +1100 Subject: [PATCH 4/6] Corrected --spy cmdoption (I think) For the --spy commmand line option, it currently says '''Print equivalent Hy code...'''. Now, I haven't actually gotten around to installing Hy on my computer yet, so I haven't had a chance to test this out to make sure, but from the looks of the code example, it looks as though it is printing out the equivalent Python code of the executed Hy code, not the other way around. This would certainly make more sense. As such, I have changed the word 'Hy' to 'Python' so that the documentation more accurately reflects what (one assumes) is going on. --- docs/language/cli.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/language/cli.rst b/docs/language/cli.rst index 875efa5..317e233 100644 --- a/docs/language/cli.rst +++ b/docs/language/cli.rst @@ -20,7 +20,7 @@ Command line options .. cmdoption:: --spy - Print equivalent Hy code before executing. For example:: + Print equivalent Python code before executing. For example:: => (defn salutationsnm [name] (print (+ "Hy " name "!"))) def salutationsnm(name): From 4d95cc48a5f5353a772b999bc4eef149162230f0 Mon Sep 17 00:00:00 2001 From: Abhishek L Date: Mon, 20 Jan 2014 21:17:22 +0530 Subject: [PATCH 5/6] Add @jackhooper to authors --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 775db60..e3a234e 100644 --- a/AUTHORS +++ b/AUTHORS @@ -37,3 +37,4 @@ * Vasudev Kamath * Yuval Langer * Fatih Kadir Akın +* Jack Hooper \ No newline at end of file From e71631f48fc77453e85266eabf3b243c561a7f3f Mon Sep 17 00:00:00 2001 From: Abhishek L Date: Mon, 20 Jan 2014 22:46:17 +0530 Subject: [PATCH 6/6] Wrong email id, I messed up that commit --- AUTHORS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index e3a234e..5eeaf05 100644 --- a/AUTHORS +++ b/AUTHORS @@ -37,4 +37,4 @@ * Vasudev Kamath * Yuval Langer * Fatih Kadir Akın -* Jack Hooper \ No newline at end of file +* Jack Hooper \ No newline at end of file