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 <algernon@balabit.hu>
This commit is contained in:
Gergely Nagy 2014-01-17 12:47:05 +01:00 committed by Gergely Nagy
parent 8447a9dfdb
commit 21709f0199
3 changed files with 44 additions and 0 deletions

View File

@ -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

View File

@ -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))

View File

@ -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)))