hy.contrib.alias: Move defn-alias and defmacro-alias here
As discussed in #880, move defn-alias and defmacro-alias to a contrib module. Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
This commit is contained in:
parent
7d8ddd9ecb
commit
8e2a892469
60
docs/contrib/alias.rst
Normal file
60
docs/contrib/alias.rst
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
============
|
||||||
|
Alias macros
|
||||||
|
============
|
||||||
|
|
||||||
|
.. versionadded:: 0.12
|
||||||
|
|
||||||
|
The alias macro module provides the ``(defn-alias)`` and
|
||||||
|
``(defmacro-alias)``, that were in Hy core previously.
|
||||||
|
|
||||||
|
|
||||||
|
Macros
|
||||||
|
======
|
||||||
|
|
||||||
|
|
||||||
|
.. _defn-alias:
|
||||||
|
|
||||||
|
defn-alias
|
||||||
|
------------------------
|
||||||
|
|
||||||
|
The ``defn-alias`` and macro is much like `defn`_,
|
||||||
|
with the distinction 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``
|
||||||
|
is no different from ``defn``.
|
||||||
|
|
||||||
|
.. code-block:: clj
|
||||||
|
|
||||||
|
=> (defn-alias [main-name alias] []
|
||||||
|
... (print "Hello!"))
|
||||||
|
=> (main-name)
|
||||||
|
"Hello!"
|
||||||
|
=> (alias)
|
||||||
|
"Hello!"
|
||||||
|
|
||||||
|
|
||||||
|
.. _defmacro-alias:
|
||||||
|
|
||||||
|
defmacro-alias
|
||||||
|
--------------
|
||||||
|
|
||||||
|
``defmacro-alias`` is used to define macros with multiple names
|
||||||
|
(aliases). The general format is ``(defmacro-alias [names] [parameters]
|
||||||
|
expr)``. It creates multiple macros with the same parameter list and
|
||||||
|
body, under the specified list of names.
|
||||||
|
|
||||||
|
The following example defines two macros, both of which allow the user
|
||||||
|
to write code in infix notation.
|
||||||
|
|
||||||
|
.. code-block:: clj
|
||||||
|
|
||||||
|
=> (defmacro-alias [infix infi] [code]
|
||||||
|
... (quasiquote (
|
||||||
|
... (unquote (get code 1))
|
||||||
|
... (unquote (get code 0))
|
||||||
|
... (unquote (get code 2)))))
|
||||||
|
|
||||||
|
=> (infix (1 + 1))
|
||||||
|
2
|
||||||
|
=> (infi (1 + 1))
|
||||||
|
2
|
@ -11,3 +11,4 @@ Contents:
|
|||||||
loop
|
loop
|
||||||
multi
|
multi
|
||||||
flow
|
flow
|
||||||
|
alias
|
||||||
|
@ -488,28 +488,6 @@ Parameters may have the following keywords in front of them:
|
|||||||
|
|
||||||
Availability: Python 3.
|
Availability: Python 3.
|
||||||
|
|
||||||
.. _defn-alias:
|
|
||||||
|
|
||||||
defn-alias
|
|
||||||
------------------------
|
|
||||||
|
|
||||||
.. versionadded:: 0.10.0
|
|
||||||
|
|
||||||
The ``defn-alias`` and macro is much like `defn`_,
|
|
||||||
with the distinction 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``
|
|
||||||
is no different from ``defn``.
|
|
||||||
|
|
||||||
.. code-block:: clj
|
|
||||||
|
|
||||||
=> (defn-alias [main-name alias] []
|
|
||||||
... (print "Hello!"))
|
|
||||||
=> (main-name)
|
|
||||||
"Hello!"
|
|
||||||
=> (alias)
|
|
||||||
"Hello!"
|
|
||||||
|
|
||||||
|
|
||||||
defmain
|
defmain
|
||||||
-------
|
-------
|
||||||
@ -571,31 +549,6 @@ between the operands.
|
|||||||
=> (infix (1 + 1))
|
=> (infix (1 + 1))
|
||||||
2
|
2
|
||||||
|
|
||||||
.. _defmacro-alias:
|
|
||||||
|
|
||||||
defmacro-alias
|
|
||||||
--------------
|
|
||||||
|
|
||||||
``defmacro-alias`` is used to define macros with multiple names
|
|
||||||
(aliases). The general format is ``(defmacro-alias [names] [parameters]
|
|
||||||
expr)``. It creates multiple macros with the same parameter list and
|
|
||||||
body, under the specified list of names.
|
|
||||||
|
|
||||||
The following example defines two macros, both of which allow the user
|
|
||||||
to write code in infix notation.
|
|
||||||
|
|
||||||
.. code-block:: clj
|
|
||||||
|
|
||||||
=> (defmacro-alias [infix infi] [code]
|
|
||||||
... (quasiquote (
|
|
||||||
... (unquote (get code 1))
|
|
||||||
... (unquote (get code 0))
|
|
||||||
... (unquote (get code 2)))))
|
|
||||||
|
|
||||||
=> (infix (1 + 1))
|
|
||||||
2
|
|
||||||
=> (infi (1 + 1))
|
|
||||||
2
|
|
||||||
|
|
||||||
.. _defmacro/g!:
|
.. _defmacro/g!:
|
||||||
|
|
||||||
|
39
hy/contrib/alias.hy
Normal file
39
hy/contrib/alias.hy
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
;; Copyright (c) 2014, 2015 Gergely Nagy
|
||||||
|
;; Copyright (c) 2014, 2015 Paul Tagliamonte <paultag@debian.org>
|
||||||
|
|
||||||
|
;; Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
;; copy of this software and associated documentation files (the "Software"),
|
||||||
|
;; to deal in the Software without restriction, including without limitation
|
||||||
|
;; the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
;; and/or sell copies of the Software, and to permit persons to whom the
|
||||||
|
;; Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
;; The above copyright notice and this permission notice shall be included in
|
||||||
|
;; all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||||
|
;; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
;; DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
(defmacro defmacro-alias [names lambda-list &rest body]
|
||||||
|
"define one macro with several names"
|
||||||
|
(setv ret `(do))
|
||||||
|
(for* [name names]
|
||||||
|
(.append ret
|
||||||
|
`(defmacro ~name ~lambda-list ~@body)))
|
||||||
|
ret)
|
||||||
|
|
||||||
|
|
||||||
|
(defmacro defn-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))
|
@ -31,15 +31,6 @@
|
|||||||
`(raise (hy.errors.HyMacroExpansionError ~location ~reason)))
|
`(raise (hy.errors.HyMacroExpansionError ~location ~reason)))
|
||||||
|
|
||||||
|
|
||||||
(defmacro defmacro-alias [names lambda-list &rest body]
|
|
||||||
"define one macro with several names"
|
|
||||||
(setv ret `(do))
|
|
||||||
(for* [name names]
|
|
||||||
(.append ret
|
|
||||||
`(defmacro ~name ~lambda-list ~@body)))
|
|
||||||
ret)
|
|
||||||
|
|
||||||
|
|
||||||
(defmacro defn [name lambda-list &rest body]
|
(defmacro defn [name lambda-list &rest body]
|
||||||
"define a function `name` with signature `lambda-list` and body `body`"
|
"define a function `name` with signature `lambda-list` and body `body`"
|
||||||
(if (not (= (type name) HySymbol))
|
(if (not (= (type name) HySymbol))
|
||||||
|
@ -210,16 +210,6 @@
|
|||||||
(sys.exit ~retval)))))
|
(sys.exit ~retval)))))
|
||||||
|
|
||||||
|
|
||||||
(defmacro defn-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))
|
|
||||||
|
|
||||||
(defreader @ [expr]
|
(defreader @ [expr]
|
||||||
(let [[decorators (cut expr nil -1)]
|
(let [[decorators (cut expr nil -1)]
|
||||||
[fndef (get expr -1)]]
|
[fndef (get expr -1)]]
|
||||||
|
8
tests/native_tests/contrib/alias.hy
Normal file
8
tests/native_tests/contrib/alias.hy
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
(require hy.contrib.alias)
|
||||||
|
|
||||||
|
(defn test-defn-alias []
|
||||||
|
(defn-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)))
|
@ -221,13 +221,6 @@
|
|||||||
(assert (= (lif-not 0 "false" "true") "true")))
|
(assert (= (lif-not 0 "false" "true") "true")))
|
||||||
|
|
||||||
|
|
||||||
(defn test-defn-alias []
|
|
||||||
(defn-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)))
|
|
||||||
|
|
||||||
(defn test-yield-from []
|
(defn test-yield-from []
|
||||||
"NATIVE: testing yield from"
|
"NATIVE: testing yield from"
|
||||||
(defn yield-from-test []
|
(defn yield-from-test []
|
||||||
|
Loading…
x
Reference in New Issue
Block a user