2013-04-07 17:35:10 +02:00
|
|
|
|
=================
|
|
|
|
|
Hy (the language)
|
|
|
|
|
=================
|
|
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
This is incomplete; please consider contributing to the documentation
|
|
|
|
|
effort.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Theory of Hy
|
|
|
|
|
============
|
|
|
|
|
|
|
|
|
|
Hy maintains, over everything else, 100% compatibility in both directions
|
2013-06-07 05:53:53 +02:00
|
|
|
|
with Python itself. All Hy code follows a few simple rules. Memorize
|
2014-12-06 21:28:28 +01:00
|
|
|
|
this, as it's going to come in handy.
|
2013-04-07 17:35:10 +02:00
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
These rules help ensure that Hy code is idiomatic and interfaceable in both
|
2013-04-07 17:35:10 +02:00
|
|
|
|
languages.
|
|
|
|
|
|
|
|
|
|
|
2017-02-14 22:18:51 +01:00
|
|
|
|
* Symbols in earmuffs will be translated to the upper-cased version of that
|
2014-12-07 07:05:52 +01:00
|
|
|
|
string. For example, ``foo`` will become ``FOO``.
|
2013-04-07 17:35:10 +02:00
|
|
|
|
|
|
|
|
|
* UTF-8 entities will be encoded using
|
2016-09-04 22:35:46 +02:00
|
|
|
|
`punycode <https://en.wikipedia.org/wiki/Punycode>`_ and prefixed with
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``hy_``. For instance, ``⚘`` will become ``hy_w7h``, ``♥`` will become
|
|
|
|
|
``hy_g6h``, and ``i♥u`` will become ``hy_iu_t0x``.
|
2013-04-07 17:35:10 +02:00
|
|
|
|
|
|
|
|
|
* Symbols that contain dashes will have them replaced with underscores. For
|
2014-12-07 07:05:52 +01:00
|
|
|
|
example, ``render-template`` will become ``render_template``. This means
|
|
|
|
|
that symbols with dashes will shadow their underscore equivalents, and vice
|
2014-01-11 15:58:47 +01:00
|
|
|
|
versa.
|
2013-04-07 17:35:10 +02:00
|
|
|
|
|
2015-09-15 18:18:56 +02:00
|
|
|
|
Notes on Syntax
|
|
|
|
|
===============
|
|
|
|
|
|
2017-02-14 22:18:51 +01:00
|
|
|
|
numeric literals
|
|
|
|
|
----------------
|
2015-09-15 18:18:56 +02:00
|
|
|
|
|
|
|
|
|
In addition to regular numbers, standard notation from Python 3 for non-base 10
|
|
|
|
|
integers is used. ``0x`` for Hex, ``0o`` for Octal, ``0b`` for Binary.
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
(print 0x80 0b11101 0o102 30)
|
|
|
|
|
|
2017-09-16 23:19:05 +02:00
|
|
|
|
Underscores and commas can appear anywhere in a numeric literal except the very
|
|
|
|
|
beginning. They have no effect on the value of the literal, but they're useful
|
|
|
|
|
for visually separating digits.
|
2017-02-14 22:18:51 +01:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
(print 10,000,000,000 10_000_000_000)
|
2013-04-07 17:35:10 +02:00
|
|
|
|
|
2017-09-08 20:27:11 +02:00
|
|
|
|
Unlike Python, Hy provides literal forms for NaN and infinity: ``NaN``,
|
|
|
|
|
``Inf``, and ``-Inf``.
|
2017-05-14 17:12:28 +02:00
|
|
|
|
|
2017-02-19 01:15:58 +01:00
|
|
|
|
string literals
|
|
|
|
|
---------------
|
|
|
|
|
|
2017-09-08 20:26:59 +02:00
|
|
|
|
Hy allows double-quoted strings (e.g., ``"hello"``), but not single-quoted
|
|
|
|
|
strings like Python. The single-quote character ``'`` is reserved for
|
|
|
|
|
preventing the evaluation of a form (e.g., ``'(+ 1 1)``), as in most Lisps.
|
2017-03-30 21:30:00 +02:00
|
|
|
|
|
|
|
|
|
Python's so-called triple-quoted strings (e.g., ``'''hello'''`` and
|
|
|
|
|
``"""hello"""``) aren't supported. However, in Hy, unlike Python, any string
|
2017-09-08 20:26:59 +02:00
|
|
|
|
literal can contain newlines. Furthermore, Hy supports an alternative form of
|
|
|
|
|
string literal called a "bracket string" similar to Lua's long brackets.
|
|
|
|
|
Bracket strings have customizable delimiters, like the here-documents of other
|
|
|
|
|
languages. A bracket string begins with ``#[FOO[`` and ends with ``]FOO]``,
|
|
|
|
|
where ``FOO`` is any string not containing ``[`` or ``]``, including the empty
|
|
|
|
|
string. For example::
|
|
|
|
|
|
|
|
|
|
=> (print #[["That's very kind of yuo [sic]" Tom wrote back.]])
|
|
|
|
|
"That's very kind of yuo [sic]" Tom wrote back.
|
|
|
|
|
=> (print #[==[1 + 1 = 2]==])
|
|
|
|
|
1 + 1 = 2
|
|
|
|
|
|
|
|
|
|
A bracket string can contain newlines, but if it begins with one, the newline
|
|
|
|
|
is removed, so you can begin the content of a bracket string on the line
|
|
|
|
|
following the opening delimiter with no effect on the content. Any leading
|
|
|
|
|
newlines past the first are preserved.
|
|
|
|
|
|
|
|
|
|
Plain string literals support :ref:`a variety of backslash escapes
|
|
|
|
|
<py:strings>`. To create a "raw string" that interprets all backslashes
|
|
|
|
|
literally, prefix the string with ``r``, as in ``r"slash\not"``. Bracket
|
|
|
|
|
strings are always raw strings and don't allow the ``r`` prefix.
|
|
|
|
|
|
|
|
|
|
Whether running under Python 2 or Python 3, Hy treats all string literals as
|
|
|
|
|
sequences of Unicode characters by default, and allows you to prefix a plain
|
|
|
|
|
string literal (but not a bracket string) with ``b`` to treat it as a sequence
|
|
|
|
|
of bytes. So when running under Python 3, Hy translates ``"foo"`` and
|
|
|
|
|
``b"foo"`` to the identical Python code, but when running under Python 2,
|
|
|
|
|
``"foo"`` is translated to ``u"foo"`` and ``b"foo"`` is translated to
|
|
|
|
|
``"foo"``.
|
2017-02-19 01:15:58 +01:00
|
|
|
|
|
2017-03-30 21:30:00 +02:00
|
|
|
|
.. _syntax-keywords:
|
|
|
|
|
|
|
|
|
|
keywords
|
|
|
|
|
--------
|
|
|
|
|
|
|
|
|
|
An identifier headed by a colon, such as ``:foo``, is a keyword. Keywords
|
|
|
|
|
evaluate to a string preceded by the Unicode non-character code point U+FDD0,
|
|
|
|
|
like ``"\ufdd0:foo"``, so ``:foo`` and ``":foo"`` aren't equal. However, if a
|
|
|
|
|
literal keyword appears in a function call, it's used to indicate a keyword
|
|
|
|
|
argument rather than passed in as a value. For example, ``(f :foo 3)`` calls
|
|
|
|
|
the function ``f`` with the keyword argument named ``foo`` set to ``3``. Hence,
|
|
|
|
|
trying to call a function on a literal keyword may fail: ``(f :foo)`` yields
|
|
|
|
|
the error ``Keyword argument :foo needs a value``. To avoid this, you can quote
|
|
|
|
|
the keyword, as in ``(f ':foo)``, or use it as the value of another keyword
|
|
|
|
|
argument, as in ``(f :arg :foo)``.
|
|
|
|
|
|
2017-08-04 23:59:34 +02:00
|
|
|
|
discard prefix
|
|
|
|
|
--------------
|
|
|
|
|
|
|
|
|
|
Hy supports the Extensible Data Notation discard prefix, like Clojure.
|
|
|
|
|
Any form prefixed with ``#_`` is discarded instead of compiled.
|
|
|
|
|
This completely removes the form so it doesn't evaluate to anything,
|
|
|
|
|
not even None.
|
|
|
|
|
It's often more useful than linewise comments for commenting out a
|
|
|
|
|
form, because it respects code structure even when part of another
|
|
|
|
|
form is on the same line. For example:
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (print "Hy" "cruel" "World!")
|
|
|
|
|
Hy cruel World!
|
|
|
|
|
=> (print "Hy" #_"cruel" "World!")
|
|
|
|
|
Hy World!
|
|
|
|
|
=> (+ 1 1 (print "Math is hard!"))
|
|
|
|
|
Math is hard!
|
|
|
|
|
Traceback (most recent call last):
|
|
|
|
|
...
|
|
|
|
|
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
|
|
|
|
|
=> (+ 1 1 #_(print "Math is hard!"))
|
|
|
|
|
2
|
|
|
|
|
|
2014-12-06 21:28:28 +01:00
|
|
|
|
Built-Ins
|
|
|
|
|
=========
|
2013-04-07 17:35:10 +02:00
|
|
|
|
|
2014-01-16 00:25:43 +01:00
|
|
|
|
Hy features a number of special forms that are used to help generate
|
2013-04-07 17:35:10 +02:00
|
|
|
|
correct Python AST. The following are "special" forms, which may have
|
|
|
|
|
behavior that's slightly unexpected in some situations.
|
|
|
|
|
|
2014-01-09 03:46:35 +01:00
|
|
|
|
.
|
|
|
|
|
-
|
|
|
|
|
|
2014-04-10 20:21:32 +02:00
|
|
|
|
.. versionadded:: 0.10.0
|
2014-01-09 03:46:35 +01:00
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``.`` is used to perform attribute access on objects. It uses a small DSL
|
2014-12-06 21:28:28 +01:00
|
|
|
|
to allow quick access to attributes and items in a nested data structure.
|
2014-01-09 03:46:35 +01:00
|
|
|
|
|
|
|
|
|
For instance,
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
(. foo bar baz [(+ 1 2)] frob)
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
Compiles down to:
|
2014-01-09 03:46:35 +01:00
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
foo.bar.baz[1 + 2].frob
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``.`` compiles its first argument (in the example, *foo*) as the object on
|
|
|
|
|
which to do the attribute dereference. It uses bare symbols as attributes
|
|
|
|
|
to access (in the example, *bar*, *baz*, *frob*), and compiles the contents
|
|
|
|
|
of lists (in the example, ``[(+ 1 2)]``) for indexation. Other arguments
|
2015-08-09 06:04:02 +02:00
|
|
|
|
raise a compilation error.
|
2014-01-09 03:46:35 +01:00
|
|
|
|
|
2015-08-09 06:04:02 +02:00
|
|
|
|
Access to unknown attributes raises an :exc:`AttributeError`. Access to
|
|
|
|
|
unknown keys raises an :exc:`IndexError` (on lists and tuples) or a
|
2014-12-06 21:28:28 +01:00
|
|
|
|
:exc:`KeyError` (on dictionaries).
|
2014-01-09 03:46:35 +01:00
|
|
|
|
|
2013-07-22 22:36:59 +02:00
|
|
|
|
->
|
|
|
|
|
--
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``->`` (or the *threading macro*) is used to avoid nesting of expressions. The
|
|
|
|
|
threading macro inserts each expression into the next expression's first argument
|
|
|
|
|
place. The following code demonstrates this:
|
2013-07-22 23:36:34 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (defn output [a b] (print a b))
|
2015-03-12 02:38:15 +01:00
|
|
|
|
=> (-> (+ 4 6) (output 5))
|
2013-07-22 23:36:34 +02:00
|
|
|
|
10 5
|
|
|
|
|
|
2013-07-22 22:36:59 +02:00
|
|
|
|
|
|
|
|
|
->>
|
|
|
|
|
---
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``->>`` (or the *threading tail macro*) is similar to the *threading macro*, but
|
|
|
|
|
instead of inserting each expression into the next expression's first argument,
|
|
|
|
|
it appends it as the last argument. The following code demonstrates this:
|
2013-07-22 23:36:34 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (defn output [a b] (print a b))
|
2015-03-12 02:38:15 +01:00
|
|
|
|
=> (->> (+ 4 6) (output 5))
|
2013-07-22 23:36:34 +02:00
|
|
|
|
5 10
|
|
|
|
|
|
2013-07-22 22:36:59 +02:00
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
and
|
|
|
|
|
---
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``and`` is used in logical expressions. It takes at least two parameters. If
|
|
|
|
|
all parameters evaluate to ``True``, the last parameter is returned. In any
|
|
|
|
|
other case, the first false value will be returned. Example usage:
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (and True False)
|
|
|
|
|
False
|
|
|
|
|
|
|
|
|
|
=> (and True True)
|
|
|
|
|
True
|
|
|
|
|
|
|
|
|
|
=> (and True 1)
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
|
=> (and True [] False True)
|
|
|
|
|
[]
|
|
|
|
|
|
2014-03-14 22:53:10 +01:00
|
|
|
|
.. note::
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``and`` short-circuits and stops evaluating parameters as soon as the first
|
2014-03-14 22:53:10 +01:00
|
|
|
|
false is encountered.
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (and False (print "hello"))
|
|
|
|
|
False
|
|
|
|
|
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
2016-11-03 09:20:26 +01:00
|
|
|
|
as->
|
|
|
|
|
----
|
|
|
|
|
|
|
|
|
|
.. versionadded:: 0.12.0
|
|
|
|
|
|
|
|
|
|
Expands to sequence of assignments to the provided name, starting with head.
|
|
|
|
|
The previous result is thus available in the subsequent form. Returns the final
|
|
|
|
|
result, and leaves the name bound to it in the local scope. This behaves much
|
|
|
|
|
like the other threading macros, but requires you to specify the threading
|
|
|
|
|
point per form via the name instead of always the first or last argument.
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
;; example how -> and as-> relate
|
|
|
|
|
|
|
|
|
|
=> (as-> 0 it
|
|
|
|
|
... (inc it)
|
|
|
|
|
... (inc it))
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
|
=> (-> 0 inc inc)
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
|
;; create data for our cuttlefish database
|
|
|
|
|
|
|
|
|
|
=> (setv data [{:name "hooded cuttlefish"
|
|
|
|
|
... :classification {:subgenus "Acanthosepion"
|
|
|
|
|
... :species "Sepia prashadi"}
|
|
|
|
|
... :discovered {:year 1936
|
|
|
|
|
... :name "Ronald Winckworth"}}
|
|
|
|
|
... {:name "slender cuttlefish"
|
|
|
|
|
... :classification {:subgenus "Doratosepion"
|
|
|
|
|
... :species "Sepia braggi"}
|
|
|
|
|
... :discovered {:year 1907
|
|
|
|
|
... :name "Sir Joseph Cooke Verco"}}])
|
|
|
|
|
|
|
|
|
|
;; retrieve name of first entry
|
|
|
|
|
=> (as-> (first data) it
|
|
|
|
|
... (:name it))
|
|
|
|
|
'hooded cuttlefish'
|
|
|
|
|
|
|
|
|
|
;; retrieve species of first entry
|
|
|
|
|
=> (as-> (first data) it
|
|
|
|
|
... (:classification it)
|
|
|
|
|
... (:species it))
|
|
|
|
|
'Sepia prashadi'
|
|
|
|
|
|
|
|
|
|
;; find out who discovered slender cuttlefish
|
|
|
|
|
=> (as-> (filter (fn [entry] (= (:name entry)
|
|
|
|
|
... "slender cuttlefish")) data) it
|
|
|
|
|
... (first it)
|
|
|
|
|
... (:discovered it)
|
|
|
|
|
... (:name it))
|
|
|
|
|
'Sir Joseph Cooke Verco'
|
|
|
|
|
|
|
|
|
|
;; more convoluted example to load web page and retrieve data from it
|
|
|
|
|
=> (import [urllib.request [urlopen]])
|
|
|
|
|
=> (as-> (urlopen "http://docs.hylang.org/en/stable/") it
|
|
|
|
|
... (.read it)
|
|
|
|
|
... (.decode it "utf-8")
|
|
|
|
|
... (drop (.index it "Welcome") it)
|
|
|
|
|
... (take 30 it)
|
|
|
|
|
... (list it)
|
|
|
|
|
... (.join "" it))
|
|
|
|
|
'Welcome to Hy’s documentation!
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
In these examples, the REPL will report a tuple (e.g. `('Sepia prashadi',
|
|
|
|
|
'Sepia prashadi')`) as the result, but only a single value is actually
|
|
|
|
|
returned.
|
|
|
|
|
|
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
assert
|
|
|
|
|
------
|
|
|
|
|
|
2015-02-22 17:34:19 +01:00
|
|
|
|
``assert`` is used to verify conditions while the program is
|
|
|
|
|
running. If the condition is not met, an :exc:`AssertionError` is
|
|
|
|
|
raised. ``assert`` may take one or two parameters. The first
|
|
|
|
|
parameter is the condition to check, and it should evaluate to either
|
|
|
|
|
``True`` or ``False``. The second parameter, optional, is a label for
|
|
|
|
|
the assert, and is the string that will be raised with the
|
|
|
|
|
:exc:`AssertionError`. For example:
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2015-02-22 17:34:19 +01:00
|
|
|
|
(assert (= variable expected-value))
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
2015-02-22 17:34:19 +01:00
|
|
|
|
(assert False)
|
|
|
|
|
; AssertionError
|
|
|
|
|
|
|
|
|
|
(assert (= 1 2) "one should equal two")
|
|
|
|
|
; AssertionError: one should equal two
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
assoc
|
|
|
|
|
-----
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``assoc`` is used to associate a key with a value in a dictionary or to set an
|
|
|
|
|
index of a list to a value. It takes at least three parameters: the *data
|
|
|
|
|
structure* to be modified, a *key* or *index*, and a *value*. If more than
|
|
|
|
|
three parameters are used, it will associate in pairs.
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
|
|
|
|
Examples of usage:
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2017-02-09 20:33:09 +01:00
|
|
|
|
=>(do
|
|
|
|
|
... (setv collection {})
|
2013-06-30 15:39:06 +02:00
|
|
|
|
... (assoc collection "Dog" "Bark")
|
|
|
|
|
... (print collection))
|
|
|
|
|
{u'Dog': u'Bark'}
|
|
|
|
|
|
2017-02-09 20:33:09 +01:00
|
|
|
|
=>(do
|
|
|
|
|
... (setv collection {})
|
2013-07-17 16:25:22 +02:00
|
|
|
|
... (assoc collection "Dog" "Bark" "Cat" "Meow")
|
|
|
|
|
... (print collection))
|
|
|
|
|
{u'Cat': u'Meow', u'Dog': u'Bark'}
|
|
|
|
|
|
2017-02-09 20:33:09 +01:00
|
|
|
|
=>(do
|
|
|
|
|
... (setv collection [1 2 3 4])
|
2013-06-30 15:39:06 +02:00
|
|
|
|
... (assoc collection 2 None)
|
|
|
|
|
... (print collection))
|
|
|
|
|
[1, 2, None, 4]
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
.. note:: ``assoc`` modifies the datastructure in place and returns ``None``.
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
break
|
|
|
|
|
-----
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``break`` is used to break out from a loop. It terminates the loop immediately.
|
|
|
|
|
The following example has an infinite ``while`` loop that is terminated as soon
|
|
|
|
|
as the user enters *k*.
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2014-12-06 21:28:28 +01:00
|
|
|
|
(while True (if (= "k" (raw-input "? "))
|
|
|
|
|
(break)
|
2013-06-30 15:39:06 +02:00
|
|
|
|
(print "Try again")))
|
|
|
|
|
|
|
|
|
|
|
2017-08-04 23:59:34 +02:00
|
|
|
|
comment
|
|
|
|
|
----
|
|
|
|
|
|
|
|
|
|
The ``comment`` macro ignores its body and always expands to ``None``.
|
|
|
|
|
Unlike linewise comments, the body of the ``comment`` macro must
|
|
|
|
|
be grammatically valid Hy, so the compiler can tell where the comment ends.
|
|
|
|
|
Besides the semicolon linewise comments,
|
|
|
|
|
Hy also has the ``#_`` discard prefix syntax to discard the next form.
|
|
|
|
|
This is completely discarded and doesn't expand to anything, not even ``None``.
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (print (comment <h1>Suprise!</h1>
|
|
|
|
|
... <p>You'd be surprised what's grammatically valid in Hy.</p>
|
|
|
|
|
... <p>(Keep delimiters in balance, and you're mostly good to go.)</p>)
|
|
|
|
|
... "Hy")
|
|
|
|
|
None Hy
|
|
|
|
|
=> (print #_(comment <h1>Suprise!</h1>
|
|
|
|
|
... <p>You'd be surprised what's grammatically valid in Hy.</p>
|
|
|
|
|
... <p>(Keep delimiters in balance, and you're mostly good to go.)</p>))
|
|
|
|
|
... "Hy")
|
|
|
|
|
Hy
|
|
|
|
|
|
|
|
|
|
|
2013-07-22 22:36:59 +02:00
|
|
|
|
cond
|
|
|
|
|
----
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``cond`` can be used to build nested ``if`` statements. The following example
|
|
|
|
|
shows the relationship between the macro and its expansion:
|
2013-07-22 22:59:21 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2013-10-16 18:31:18 +02:00
|
|
|
|
(cond [condition-1 result-1]
|
|
|
|
|
[condition-2 result-2])
|
2013-07-22 22:59:21 +02:00
|
|
|
|
|
|
|
|
|
(if condition-1 result-1
|
|
|
|
|
(if condition-2 result-2))
|
|
|
|
|
|
2017-06-10 03:22:23 +02:00
|
|
|
|
If only the condition is given in a branch, then the condition is also used as
|
|
|
|
|
the result. The expansion of this single argument version is demonstrated
|
|
|
|
|
below:
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
(cond [condition-1]
|
|
|
|
|
[condition-2])
|
|
|
|
|
|
|
|
|
|
(if condition-1 condition-1
|
|
|
|
|
(if condition-2 condition-2))
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
As shown below, only the first matching result block is executed.
|
2013-07-22 22:59:21 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (defn check-value [value]
|
2013-10-16 18:31:18 +02:00
|
|
|
|
... (cond [(< value 5) (print "value is smaller than 5")]
|
|
|
|
|
... [(= value 5) (print "value is equal to 5")]
|
|
|
|
|
... [(> value 5) (print "value is greater than 5")]
|
|
|
|
|
... [True (print "value is something that it should not be")]))
|
2014-12-06 21:28:28 +01:00
|
|
|
|
|
2013-07-22 22:59:21 +02:00
|
|
|
|
=> (check-value 6)
|
|
|
|
|
value is greater than 5
|
|
|
|
|
|
2013-07-22 22:36:59 +02:00
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
continue
|
|
|
|
|
--------
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``continue`` returns execution to the start of a loop. In the following example,
|
|
|
|
|
``(side-effect1)`` is called for each iteration. ``(side-effect2)``, however,
|
|
|
|
|
is only called on every other value in the list.
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
;; assuming that (side-effect1) and (side-effect2) are functions and
|
|
|
|
|
;; collection is a list of numerical values
|
|
|
|
|
|
2013-12-31 19:35:31 +01:00
|
|
|
|
(for [x collection]
|
2015-07-31 00:31:57 +02:00
|
|
|
|
(side-effect1 x)
|
|
|
|
|
(if (% x 2)
|
|
|
|
|
(continue))
|
|
|
|
|
(side-effect2 x))
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
2014-04-28 20:09:06 +02:00
|
|
|
|
dict-comp
|
|
|
|
|
---------
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``dict-comp`` is used to create dictionaries. It takes three or four parameters.
|
|
|
|
|
The first two parameters are for controlling the return value (key-value pair)
|
|
|
|
|
while the third is used to select items from a sequence. The fourth and optional
|
|
|
|
|
parameter can be used to filter out some of the items in the sequence based on a
|
|
|
|
|
conditional expression.
|
2014-04-28 20:09:06 +02:00
|
|
|
|
|
2014-04-28 20:35:28 +02:00
|
|
|
|
.. code-block:: hy
|
2014-04-28 20:09:06 +02:00
|
|
|
|
|
|
|
|
|
=> (dict-comp x (* x 2) [x (range 10)] (odd? x))
|
|
|
|
|
{1: 2, 3: 6, 9: 18, 5: 10, 7: 14}
|
|
|
|
|
|
|
|
|
|
|
2015-08-09 09:00:51 +02:00
|
|
|
|
do
|
2013-06-30 15:39:06 +02:00
|
|
|
|
----------
|
|
|
|
|
|
2015-08-09 09:00:51 +02:00
|
|
|
|
``do`` is used to evaluate each of its arguments and return the
|
2014-12-07 07:05:52 +01:00
|
|
|
|
last one. Return values from every other than the last argument are discarded.
|
2017-02-23 00:36:52 +01:00
|
|
|
|
It can be used in ``list-comp`` to perform more complex logic as shown in one
|
|
|
|
|
of the following examples.
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
|
|
|
|
Some example usage:
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2016-11-24 03:35:17 +01:00
|
|
|
|
=> (if True
|
2013-06-30 15:39:06 +02:00
|
|
|
|
... (do (print "Side effects rock!")
|
|
|
|
|
... (print "Yeah, really!")))
|
|
|
|
|
Side effects rock!
|
|
|
|
|
Yeah, really!
|
|
|
|
|
|
|
|
|
|
;; assuming that (side-effect) is a function that we want to call for each
|
2014-05-17 19:46:01 +02:00
|
|
|
|
;; and every value in the list, but whose return value we do not care about
|
2014-12-06 21:28:28 +01:00
|
|
|
|
=> (list-comp (do (side-effect x)
|
|
|
|
|
... (if (< x 5) (* 2 x)
|
|
|
|
|
... (* 4 x)))
|
2013-06-30 15:39:06 +02:00
|
|
|
|
... (x (range 10)))
|
|
|
|
|
[0, 2, 4, 6, 8, 20, 24, 28, 32, 36]
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``do`` can accept any number of arguments, from 1 to n.
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
|
|
|
|
|
2017-09-07 20:57:19 +02:00
|
|
|
|
doc / #doc
|
|
|
|
|
----------
|
|
|
|
|
|
|
|
|
|
Documentation macro and tag macro.
|
|
|
|
|
Gets help for macros or tag macros, respectively.
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (doc doc)
|
|
|
|
|
Help on function (doc) in module hy.core.macros:
|
|
|
|
|
|
|
|
|
|
(doc)(symbol)
|
|
|
|
|
macro documentation
|
|
|
|
|
|
|
|
|
|
Gets help for a macro function available in this module.
|
|
|
|
|
Use ``require`` to make other macros available.
|
|
|
|
|
|
|
|
|
|
Use ``#doc foo`` instead for help with tag macro ``#foo``.
|
|
|
|
|
Use ``(help foo)`` instead for help with runtime objects.
|
|
|
|
|
|
|
|
|
|
=> (doc comment)
|
|
|
|
|
Help on function (comment) in module hy.core.macros:
|
|
|
|
|
|
|
|
|
|
(comment)(*body)
|
|
|
|
|
Ignores body and always expands to None
|
|
|
|
|
|
|
|
|
|
=> #doc doc
|
|
|
|
|
Help on function #doc in module hy.core.macros:
|
|
|
|
|
|
|
|
|
|
#doc(symbol)
|
|
|
|
|
tag macro documentation
|
|
|
|
|
|
|
|
|
|
Gets help for a tag macro function available in this module.
|
|
|
|
|
|
|
|
|
|
|
2013-07-10 02:16:49 +02:00
|
|
|
|
def / setv
|
2014-12-06 21:28:28 +01:00
|
|
|
|
----------
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``def`` and ``setv`` are used to bind a value, object, or function to a symbol.
|
|
|
|
|
For example:
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2013-08-25 20:29:24 +02:00
|
|
|
|
=> (def names ["Alice" "Bob" "Charlie"])
|
2013-07-06 22:43:47 +02:00
|
|
|
|
=> (print names)
|
|
|
|
|
[u'Alice', u'Bob', u'Charlie']
|
|
|
|
|
|
2013-07-10 10:39:27 +02:00
|
|
|
|
=> (setv counter (fn [collection item] (.count collection item)))
|
2013-07-06 22:43:47 +02:00
|
|
|
|
=> (counter [1 2 3 4 5 2 3] 2)
|
|
|
|
|
2
|
|
|
|
|
|
2015-07-25 01:11:13 +02:00
|
|
|
|
They can be used to assign multiple variables at once:
|
|
|
|
|
|
|
|
|
|
.. code-block:: hy
|
|
|
|
|
|
|
|
|
|
=> (setv a 1 b 2)
|
|
|
|
|
(1L, 2L)
|
|
|
|
|
=> a
|
|
|
|
|
1L
|
|
|
|
|
=> b
|
|
|
|
|
2L
|
|
|
|
|
=>
|
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
|
|
|
|
defclass
|
|
|
|
|
--------
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
New classes are declared with ``defclass``. It can takes two optional parameters:
|
2013-07-10 06:11:11 +02:00
|
|
|
|
a vector defining a possible super classes and another vector containing
|
2013-07-06 22:43:47 +02:00
|
|
|
|
attributes of the new class as two item vectors.
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2013-07-10 06:11:11 +02:00
|
|
|
|
(defclass class-name [super-class-1 super-class-2]
|
2015-08-04 16:43:07 +02:00
|
|
|
|
[attribute value]
|
|
|
|
|
|
|
|
|
|
(defn method [self] (print "hello!")))
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
|
|
|
|
Both values and functions can be bound on the new class as shown by the example
|
|
|
|
|
below:
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (defclass Cat []
|
2015-08-04 16:43:07 +02:00
|
|
|
|
... [age None
|
|
|
|
|
... colour "white"]
|
|
|
|
|
...
|
|
|
|
|
... (defn speak [self] (print "Meow")))
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
|
|
|
|
=> (def spot (Cat))
|
|
|
|
|
=> (setv spot.colour "Black")
|
|
|
|
|
'Black'
|
|
|
|
|
=> (.speak spot)
|
|
|
|
|
Meow
|
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
2014-01-17 12:47:05 +01:00
|
|
|
|
.. _defn:
|
|
|
|
|
|
2015-08-09 09:09:52 +02:00
|
|
|
|
defn
|
2016-01-26 21:27:07 +01:00
|
|
|
|
----
|
2013-07-22 22:36:59 +02:00
|
|
|
|
|
2015-08-09 09:09:52 +02:00
|
|
|
|
``defn`` macro is used to define functions. It takes three
|
2014-12-07 07:05:52 +01:00
|
|
|
|
parameters: the *name* of the function to define, a vector of *parameters*,
|
|
|
|
|
and the *body* of the function:
|
2013-07-23 15:50:12 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
(defn name [params] body)
|
|
|
|
|
|
2014-12-06 21:28:28 +01:00
|
|
|
|
Parameters may have the following keywords in front of them:
|
2013-08-05 22:23:35 +02:00
|
|
|
|
|
|
|
|
|
&optional
|
2014-12-06 21:28:28 +01:00
|
|
|
|
Parameter is optional. The parameter can be given as a two item list, where
|
2013-08-05 22:23:35 +02:00
|
|
|
|
the first element is parameter name and the second is the default value. The
|
|
|
|
|
parameter can be also given as a single item, in which case the default
|
2014-12-07 07:05:52 +01:00
|
|
|
|
value is ``None``.
|
2013-08-05 22:23:35 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (defn total-value [value &optional [value-added-tax 10]]
|
|
|
|
|
... (+ (/ (* value value-added-tax) 100) value))
|
|
|
|
|
|
|
|
|
|
=> (total-value 100)
|
|
|
|
|
110.0
|
|
|
|
|
|
|
|
|
|
=> (total-value 100 1)
|
|
|
|
|
101.0
|
|
|
|
|
|
|
|
|
|
&key
|
2016-01-26 21:27:07 +01:00
|
|
|
|
Parameter is a dict of keyword arguments. The keys of the dict
|
|
|
|
|
specify the parameter names and the values give the default values
|
|
|
|
|
of the parameters.
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (defn key-parameters [&key {"a" 1 "b" 2}]
|
|
|
|
|
... (print "a is" a "and b is" b))
|
|
|
|
|
=> (key-parameters :a 1 :b 2)
|
|
|
|
|
a is 1 and b is 2
|
|
|
|
|
=> (key-parameters :b 1 :a 2)
|
|
|
|
|
a is 2 and b is 1
|
|
|
|
|
|
|
|
|
|
The following declarations are equivalent:
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
(defn key-parameters [&key {"a" 1 "b" 2}])
|
2014-12-06 21:28:28 +01:00
|
|
|
|
|
2016-01-26 21:27:07 +01:00
|
|
|
|
(defn key-parameters [&optional [a 1] [b 2]])
|
2013-08-05 22:23:35 +02:00
|
|
|
|
|
|
|
|
|
&kwargs
|
2014-12-06 21:28:28 +01:00
|
|
|
|
Parameter will contain 0 or more keyword arguments.
|
2013-08-05 22:23:35 +02:00
|
|
|
|
|
|
|
|
|
The following code examples defines a function that will print all keyword
|
|
|
|
|
arguments and their values.
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (defn print-parameters [&kwargs kwargs]
|
|
|
|
|
... (for [(, k v) (.items kwargs)] (print k v)))
|
|
|
|
|
|
2015-07-01 14:57:53 +02:00
|
|
|
|
=> (print-parameters :parameter-1 1 :parameter-2 2)
|
|
|
|
|
parameter_1 1
|
|
|
|
|
parameter_2 2
|
|
|
|
|
|
2017-07-19 20:00:43 +02:00
|
|
|
|
; to avoid the mangling of '-' to '_', use unpacking:
|
|
|
|
|
=> (print-parameters #** {"parameter-1" 1 "parameter-2" 2})
|
2013-08-05 22:23:35 +02:00
|
|
|
|
parameter-1 1
|
2015-07-01 14:57:53 +02:00
|
|
|
|
parameter-2 2
|
2013-08-05 22:23:35 +02:00
|
|
|
|
|
|
|
|
|
&rest
|
2014-12-06 21:28:28 +01:00
|
|
|
|
Parameter will contain 0 or more positional arguments. No other positional
|
2013-08-05 22:23:35 +02:00
|
|
|
|
arguments may be specified after this one.
|
|
|
|
|
|
|
|
|
|
The following code example defines a function that can be given 0 to n
|
2014-05-17 20:04:41 +02:00
|
|
|
|
numerical parameters. It then sums every odd number and subtracts
|
2013-08-05 22:23:35 +02:00
|
|
|
|
every even number.
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (defn zig-zag-sum [&rest numbers]
|
2017-02-09 20:33:09 +01:00
|
|
|
|
(setv odd-numbers (list-comp x [x numbers] (odd? x))
|
|
|
|
|
even-numbers (list-comp x [x numbers] (even? x)))
|
|
|
|
|
(- (sum odd-numbers) (sum even-numbers)))
|
2013-08-05 22:23:35 +02:00
|
|
|
|
|
|
|
|
|
=> (zig-zag-sum)
|
|
|
|
|
0
|
|
|
|
|
=> (zig-zag-sum 3 9 4)
|
|
|
|
|
8
|
|
|
|
|
=> (zig-zag-sum 1 2 3 4 5 6)
|
2013-08-05 22:30:03 +02:00
|
|
|
|
-3
|
2013-07-22 22:36:59 +02:00
|
|
|
|
|
2015-03-16 01:22:48 +01:00
|
|
|
|
&kwonly
|
|
|
|
|
.. versionadded:: 0.12.0
|
|
|
|
|
|
|
|
|
|
Parameters that can only be called as keywords. Mandatory
|
|
|
|
|
keyword-only arguments are declared with the argument's name;
|
|
|
|
|
optional keyword-only arguments are declared as a two-element list
|
|
|
|
|
containing the argument name followed by the default value (as
|
|
|
|
|
with `&optional` above).
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2017-07-19 20:00:43 +02:00
|
|
|
|
=> (defn compare [a b &kwonly keyfn [reverse False]]
|
2017-02-09 20:33:09 +01:00
|
|
|
|
... (setv result (keyfn a b))
|
|
|
|
|
... (if (not reverse)
|
|
|
|
|
... result
|
|
|
|
|
... (- result)))
|
2017-07-19 20:00:43 +02:00
|
|
|
|
=> (compare "lisp" "python"
|
|
|
|
|
... :keyfn (fn [x y]
|
|
|
|
|
... (reduce - (map (fn [s] (ord (first s))) [x y]))))
|
2015-03-16 01:22:48 +01:00
|
|
|
|
-4
|
2017-07-19 20:00:43 +02:00
|
|
|
|
=> (compare "lisp" "python"
|
|
|
|
|
... :keyfn (fn [x y]
|
2015-03-16 01:22:48 +01:00
|
|
|
|
... (reduce - (map (fn [s] (ord (first s))) [x y])))
|
2017-07-19 20:00:43 +02:00
|
|
|
|
... :reverse True)
|
2015-03-16 01:22:48 +01:00
|
|
|
|
4
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
=> (compare "lisp" "python")
|
|
|
|
|
Traceback (most recent call last):
|
|
|
|
|
File "<input>", line 1, in <module>
|
|
|
|
|
TypeError: compare() missing 1 required keyword-only argument: 'keyfn'
|
|
|
|
|
|
|
|
|
|
Availability: Python 3.
|
|
|
|
|
|
2014-03-11 19:37:29 +01:00
|
|
|
|
|
|
|
|
|
defmain
|
|
|
|
|
-------
|
|
|
|
|
|
2014-05-02 11:27:19 +02:00
|
|
|
|
.. versionadded:: 0.10.1
|
2014-03-11 19:37:29 +01:00
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
The ``defmain`` macro defines a main function that is immediately called
|
2014-12-06 21:28:28 +01:00
|
|
|
|
with ``sys.argv`` as arguments if and only if this file is being executed
|
|
|
|
|
as a script. In other words, this:
|
2014-03-11 19:37:29 +01:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
(defmain [&rest args]
|
|
|
|
|
(do-something-with args))
|
|
|
|
|
|
|
|
|
|
is the equivalent of::
|
|
|
|
|
|
|
|
|
|
def main(*args):
|
|
|
|
|
do_something_with(args)
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
import sys
|
2016-12-13 17:57:31 +01:00
|
|
|
|
retval = main(*sys.argv)
|
2014-03-11 19:37:29 +01:00
|
|
|
|
|
|
|
|
|
if isinstance(retval, int):
|
|
|
|
|
sys.exit(retval)
|
|
|
|
|
|
2014-12-06 21:28:28 +01:00
|
|
|
|
Note that as you can see above, if you return an integer from this
|
2014-03-11 19:37:29 +01:00
|
|
|
|
function, this will be used as the exit status for your script.
|
|
|
|
|
(Python defaults to exit status 0 otherwise, which means everything's
|
2016-12-13 17:57:31 +01:00
|
|
|
|
okay!) Since ``(sys.exit 0)`` is not run explicitly in the case of a
|
|
|
|
|
non-integer return from ``defmain``, it's a good idea to put ``(defmain)``
|
|
|
|
|
as the last piece of code in your file.
|
2014-03-11 19:37:29 +01:00
|
|
|
|
|
2016-12-13 17:57:31 +01:00
|
|
|
|
If you want fancy command-line arguments, you can use the standard Python
|
|
|
|
|
module ``argparse`` in the usual way:
|
2014-03-11 19:37:29 +01:00
|
|
|
|
|
2016-12-13 17:57:31 +01:00
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
(import argparse)
|
|
|
|
|
|
|
|
|
|
(defmain [&rest _]
|
|
|
|
|
(setv parser (argparse.ArgumentParser))
|
|
|
|
|
(.add-argument parser "STRING"
|
|
|
|
|
:help "string to replicate")
|
|
|
|
|
(.add-argument parser "-n" :type int :default 3
|
|
|
|
|
:help "number of copies")
|
|
|
|
|
(setv args (parser.parse_args))
|
|
|
|
|
|
|
|
|
|
(print (* args.STRING args.n))
|
|
|
|
|
|
|
|
|
|
0)
|
2014-03-11 19:37:29 +01:00
|
|
|
|
|
2013-12-30 22:42:55 +01:00
|
|
|
|
.. _defmacro:
|
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
defmacro
|
|
|
|
|
--------
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``defmacro`` is used to define macros. The general format is
|
|
|
|
|
``(defmacro name [parameters] expr)``.
|
2013-07-18 14:00:24 +02:00
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
The 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
|
2013-07-19 11:06:23 +02:00
|
|
|
|
between the operands.
|
2013-07-18 14:00:24 +02:00
|
|
|
|
|
2013-07-22 22:36:59 +02:00
|
|
|
|
.. code-block:: clj
|
2013-07-18 14:00:24 +02:00
|
|
|
|
|
2013-07-19 11:06:23 +02:00
|
|
|
|
=> (defmacro infix [code]
|
|
|
|
|
... (quasiquote (
|
|
|
|
|
... (unquote (get code 1))
|
|
|
|
|
... (unquote (get code 0))
|
|
|
|
|
... (unquote (get code 2)))))
|
2013-07-18 14:00:24 +02:00
|
|
|
|
|
2013-07-19 11:06:23 +02:00
|
|
|
|
=> (infix (1 + 1))
|
|
|
|
|
2
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
2014-01-17 12:05:35 +01:00
|
|
|
|
|
2013-12-30 22:42:55 +01:00
|
|
|
|
.. _defmacro/g!:
|
|
|
|
|
|
|
|
|
|
defmacro/g!
|
|
|
|
|
------------
|
|
|
|
|
|
|
|
|
|
.. versionadded:: 0.9.12
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``defmacro/g!`` is a special version of ``defmacro`` that is used to
|
|
|
|
|
automatically generate :ref:`gensym` for any symbol that starts with
|
|
|
|
|
``g!``.
|
2013-12-30 22:42:55 +01:00
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
For example, ``g!a`` would become ``(gensym "a")``.
|
2013-12-30 22:42:55 +01:00
|
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
|
|
Section :ref:`using-gensym`
|
|
|
|
|
|
2016-12-15 01:10:46 +01:00
|
|
|
|
.. _defmacro!:
|
|
|
|
|
|
|
|
|
|
defmacro!
|
|
|
|
|
---------
|
|
|
|
|
|
|
|
|
|
``defmacro!`` is like ``defmacro/g!`` plus automatic once-only evaluation for
|
|
|
|
|
``o!`` parameters, which are available as the equivalent ``g!`` symbol.
|
|
|
|
|
|
|
|
|
|
For example,
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (defn expensive-get-number [] (print "spam") 14)
|
2017-03-09 15:02:23 +01:00
|
|
|
|
=> (defmacro triple-1 [n] `(+ ~n ~n ~n))
|
2016-12-15 01:10:46 +01:00
|
|
|
|
=> (triple-1 (expensive-get-number)) ; evals n three times
|
|
|
|
|
spam
|
|
|
|
|
spam
|
|
|
|
|
spam
|
|
|
|
|
42
|
|
|
|
|
=> (defmacro/g! triple-2 [n] `(do (setv ~g!n ~n) (+ ~g!n ~g!n ~g!n)))
|
|
|
|
|
=> (triple-2 (expensive-get-number)) ; avoid repeats with a gensym
|
|
|
|
|
spam
|
|
|
|
|
42
|
|
|
|
|
=> (defmacro! triple-3 [o!n] `(+ ~g!n ~g!n ~g!n))
|
|
|
|
|
=> (triple-3 (expensive-get-number)) ; easier with defmacro!
|
|
|
|
|
spam
|
|
|
|
|
42
|
|
|
|
|
|
|
|
|
|
|
2017-06-21 05:48:54 +02:00
|
|
|
|
deftag
|
2017-04-21 17:07:48 +02:00
|
|
|
|
--------
|
2013-12-31 02:06:51 +01:00
|
|
|
|
|
2017-04-21 17:07:48 +02:00
|
|
|
|
.. versionadded:: 0.13.0
|
2013-12-31 02:06:51 +01:00
|
|
|
|
|
2017-06-21 05:48:54 +02:00
|
|
|
|
``deftag`` defines a tag macro. A tag macro is a unary macro that has the
|
2017-05-10 03:54:32 +02:00
|
|
|
|
same semantics as an ordinary macro defined with ``defmacro``. It is called with
|
|
|
|
|
the syntax ``#tag FORM``, where ``tag`` is the name of the macro, and ``FORM``
|
|
|
|
|
is any form. The ``tag`` is often only one character, but it can be any symbol.
|
2013-12-31 02:06:51 +01:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2017-06-21 05:48:54 +02:00
|
|
|
|
=> (deftag ♣ [expr] `[~expr ~expr])
|
2017-04-21 17:07:48 +02:00
|
|
|
|
<function <lambda> at 0x7f76d0271158>
|
2017-05-10 03:54:32 +02:00
|
|
|
|
=> #♣ 5
|
2017-04-21 17:07:48 +02:00
|
|
|
|
[5, 5]
|
|
|
|
|
=> (setv x 0)
|
|
|
|
|
=> #♣(+= x 1)
|
|
|
|
|
[None, None]
|
|
|
|
|
=> x
|
|
|
|
|
2
|
2013-12-31 02:06:51 +01:00
|
|
|
|
|
2017-06-21 05:48:54 +02:00
|
|
|
|
In this example, if you used ``(defmacro ♣ ...)`` instead of ``(deftag
|
2017-04-21 17:07:48 +02:00
|
|
|
|
♣ ...)``, you would call the macro as ``(♣ 5)`` or ``(♣ (+= x 1))``.
|
2013-12-31 02:06:51 +01:00
|
|
|
|
|
2017-06-21 05:48:54 +02:00
|
|
|
|
The syntax for calling tag macros is similar to that of reader macros a la
|
|
|
|
|
Common Lisp's ``SET-MACRO-CHARACTER``. In fact, before Hy 0.13.0, tag macros
|
2017-04-21 17:07:48 +02:00
|
|
|
|
were called "reader macros", and defined with ``defreader`` rather than
|
2017-06-21 05:48:54 +02:00
|
|
|
|
``deftag``. True reader macros are not (yet) implemented in Hy.
|
2013-12-30 22:42:55 +01:00
|
|
|
|
|
2013-12-21 23:33:44 +01:00
|
|
|
|
del
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
.. versionadded:: 0.9.12
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``del`` removes an object from the current namespace.
|
2013-12-21 23:33:44 +01:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (setv foo 42)
|
|
|
|
|
=> (del foo)
|
|
|
|
|
=> foo
|
|
|
|
|
Traceback (most recent call last):
|
|
|
|
|
File "<console>", line 1, in <module>
|
|
|
|
|
NameError: name 'foo' is not defined
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``del`` can also remove objects from mappings, lists, and more.
|
2013-12-21 23:33:44 +01:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (setv test (list (range 10)))
|
|
|
|
|
=> test
|
|
|
|
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
2014-09-06 03:17:46 +02:00
|
|
|
|
=> (del (cut test 2 4)) ;; remove items from 2 to 4 excluded
|
2013-12-21 23:33:44 +01:00
|
|
|
|
=> test
|
|
|
|
|
[0, 1, 4, 5, 6, 7, 8, 9]
|
|
|
|
|
=> (setv dic {"foo" "bar"})
|
|
|
|
|
=> dic
|
|
|
|
|
{"foo": "bar"}
|
|
|
|
|
=> (del (get dic "foo"))
|
|
|
|
|
=> dic
|
|
|
|
|
{}
|
|
|
|
|
|
2014-04-21 12:28:08 +02:00
|
|
|
|
doto
|
|
|
|
|
----
|
|
|
|
|
|
2014-04-25 15:19:22 +02:00
|
|
|
|
.. versionadded:: 0.10.1
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``doto`` is used to simplify a sequence of method calls to an object.
|
2014-04-21 12:28:08 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2014-04-21 21:35:56 +02:00
|
|
|
|
=> (doto [] (.append 1) (.append 2) .reverse)
|
2017-08-26 06:50:13 +02:00
|
|
|
|
[2, 1]
|
2014-04-21 12:28:08 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (setv collection [])
|
2014-04-21 21:35:56 +02:00
|
|
|
|
=> (.append collection 1)
|
|
|
|
|
=> (.append collection 2)
|
|
|
|
|
=> (.reverse collection)
|
2014-04-21 12:28:08 +02:00
|
|
|
|
=> collection
|
2017-08-26 06:50:13 +02:00
|
|
|
|
[2, 1]
|
2014-04-21 12:28:08 +02:00
|
|
|
|
|
2017-01-19 18:26:12 +01:00
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
eval-and-compile
|
|
|
|
|
----------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
eval-when-compile
|
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
|
|
|
2017-03-06 17:34:40 +01:00
|
|
|
|
first
|
|
|
|
|
-----
|
2013-07-22 22:36:59 +02:00
|
|
|
|
|
2017-03-06 17:34:40 +01:00
|
|
|
|
``first`` is a function for accessing the first element of a collection.
|
2013-07-22 23:36:34 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (first (range 10))
|
|
|
|
|
0
|
|
|
|
|
|
2017-03-06 17:34:40 +01:00
|
|
|
|
It is implemented as ``(next (iter coll) None)``, so it works with any
|
|
|
|
|
iterable, and if given an empty iterable, it will return ``None`` instead of
|
|
|
|
|
raising an exception.
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (first (repeat 10))
|
|
|
|
|
10
|
|
|
|
|
=> (first [])
|
|
|
|
|
None
|
2013-07-22 22:36:59 +02:00
|
|
|
|
|
|
|
|
|
for
|
2014-12-06 21:28:28 +01:00
|
|
|
|
---
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``for`` is used to call a function for each element in a list or vector.
|
|
|
|
|
The results of each call are discarded and the ``for`` expression returns
|
|
|
|
|
``None`` instead. The example code iterates over *collection* and for each
|
|
|
|
|
*element* in *collection* calls the ``side-effect`` function with *element*
|
|
|
|
|
as its argument:
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
;; assuming that (side-effect) is a function that takes a single parameter
|
2013-12-31 19:35:31 +01:00
|
|
|
|
(for [element collection] (side-effect element))
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
2013-11-10 19:00:01 +01:00
|
|
|
|
;; for can have an optional else block
|
2013-12-31 19:35:31 +01:00
|
|
|
|
(for [element collection] (side-effect element)
|
2013-11-10 19:00:01 +01:00
|
|
|
|
(else (side-effect-2)))
|
2013-07-10 07:24:58 +02:00
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
The optional ``else`` block is only executed if the ``for`` loop terminates
|
|
|
|
|
normally. If the execution is halted with ``break``, the ``else`` block does
|
|
|
|
|
not execute.
|
2013-07-10 07:24:58 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2013-12-31 19:35:31 +01:00
|
|
|
|
=> (for [element [1 2 3]] (if (< element 3)
|
2014-12-06 21:28:28 +01:00
|
|
|
|
... (print element)
|
2013-11-10 19:00:01 +01:00
|
|
|
|
... (break))
|
2013-07-10 07:24:58 +02:00
|
|
|
|
... (else (print "loop finished")))
|
|
|
|
|
1
|
|
|
|
|
2
|
|
|
|
|
|
2013-12-31 19:35:31 +01:00
|
|
|
|
=> (for [element [1 2 3]] (if (< element 4)
|
2013-11-10 19:00:01 +01:00
|
|
|
|
... (print element)
|
|
|
|
|
... (break))
|
2013-07-10 07:24:58 +02:00
|
|
|
|
... (else (print "loop finished")))
|
|
|
|
|
1
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
|
|
loop finished
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
2014-04-28 20:09:06 +02:00
|
|
|
|
genexpr
|
|
|
|
|
-------
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``genexpr`` is used to create generator expressions. It takes two or three
|
|
|
|
|
parameters. The first parameter is the expression controlling the return value,
|
|
|
|
|
while the second is used to select items from a list. The third and optional
|
2014-12-06 21:28:28 +01:00
|
|
|
|
parameter can be used to filter out some of the items in the list based on a
|
2014-12-07 07:05:52 +01:00
|
|
|
|
conditional expression. ``genexpr`` is similar to ``list-comp``, except it
|
|
|
|
|
returns an iterable that evaluates values one by one instead of evaluating them
|
|
|
|
|
immediately.
|
2014-04-28 20:09:06 +02:00
|
|
|
|
|
2014-04-28 20:35:28 +02:00
|
|
|
|
.. code-block:: hy
|
2014-04-28 20:09:06 +02:00
|
|
|
|
|
|
|
|
|
=> (def collection (range 10))
|
|
|
|
|
=> (def filtered (genexpr x [x collection] (even? x)))
|
|
|
|
|
=> (list filtered)
|
|
|
|
|
[0, 2, 4, 6, 8]
|
|
|
|
|
|
|
|
|
|
|
2013-12-30 22:42:55 +01:00
|
|
|
|
.. _gensym:
|
|
|
|
|
|
|
|
|
|
gensym
|
|
|
|
|
------
|
|
|
|
|
|
|
|
|
|
.. versionadded:: 0.9.12
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``gensym`` is used to generate a unique symbol that allows macros to be
|
|
|
|
|
written without accidental variable name clashes.
|
2013-12-30 22:42:55 +01:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (gensym)
|
|
|
|
|
u':G_1235'
|
|
|
|
|
|
|
|
|
|
=> (gensym "x")
|
|
|
|
|
u':x_1236'
|
|
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
|
|
Section :ref:`using-gensym`
|
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
get
|
|
|
|
|
---
|
|
|
|
|
|
2017-02-24 23:22:27 +01:00
|
|
|
|
``get`` is used to access single elements in collections. ``get`` takes at
|
|
|
|
|
least two parameters: the *data structure* and the *index* or *key* of the
|
|
|
|
|
item. It will then return the corresponding value from the collection. If
|
|
|
|
|
multiple *index* or *key* values are provided, they are used to access
|
|
|
|
|
successive elements in a nested structure. Example usage:
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2017-02-09 20:33:09 +01:00
|
|
|
|
=> (do
|
|
|
|
|
... (setv animals {"dog" "bark" "cat" "meow"}
|
2017-02-24 23:22:27 +01:00
|
|
|
|
... numbers (, "zero" "one" "two" "three")
|
|
|
|
|
... nested [0 1 ["a" "b" "c"] 3 4])
|
2013-06-30 15:39:06 +02:00
|
|
|
|
... (print (get animals "dog"))
|
2017-02-24 23:22:27 +01:00
|
|
|
|
... (print (get numbers 2))
|
|
|
|
|
... (print (get nested 2 1)))
|
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
bark
|
|
|
|
|
two
|
2017-02-24 23:22:27 +01:00
|
|
|
|
b
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
.. note:: ``get`` raises a KeyError if a dictionary is queried for a
|
|
|
|
|
non-existing key.
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
.. note:: ``get`` raises an IndexError if a list or a tuple is queried for an
|
|
|
|
|
index that is out of bounds.
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
global
|
|
|
|
|
------
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``global`` can be used to mark a symbol as global. This allows the programmer to
|
2013-07-19 14:57:25 +02:00
|
|
|
|
assign a value to a global symbol. Reading a global symbol does not require the
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``global`` keyword -- only assigning it does.
|
2013-07-19 14:57:25 +02:00
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
The following example shows how the global symbol ``a`` is assigned a value in a
|
|
|
|
|
function and is later on printed in another function. Without the ``global``
|
2015-08-09 06:04:02 +02:00
|
|
|
|
keyword, the second function would have raised a ``NameError``.
|
2013-07-19 14:57:25 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
(defn set-a [value]
|
|
|
|
|
(global a)
|
|
|
|
|
(setv a value))
|
|
|
|
|
|
|
|
|
|
(defn print-a []
|
|
|
|
|
(print a))
|
|
|
|
|
|
|
|
|
|
(set-a 5)
|
|
|
|
|
(print-a)
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
2015-10-14 03:38:15 +02:00
|
|
|
|
if / if* / if-not
|
|
|
|
|
-----------------
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
2014-12-06 21:28:28 +01:00
|
|
|
|
.. versionadded:: 0.10.0
|
|
|
|
|
if-not
|
|
|
|
|
|
2015-10-14 03:38:15 +02:00
|
|
|
|
``if / if* / if-not`` respect Python *truthiness*, that is, a *test* fails if it
|
2016-11-24 03:35:17 +01:00
|
|
|
|
evaluates to a "zero" (including values of ``len`` zero, ``None``, and
|
|
|
|
|
``False``), and passes otherwise, but values with a ``__bool__`` method
|
2015-10-14 03:38:15 +02:00
|
|
|
|
(``__nonzero__`` in Python 2) can overrides this.
|
|
|
|
|
|
|
|
|
|
The ``if`` macro is for conditionally selecting an expression for evaluation.
|
|
|
|
|
The result of the selected expression becomes the result of the entire ``if``
|
|
|
|
|
form. ``if`` can select a group of expressions with the help of a ``do`` block.
|
2014-12-07 07:05:52 +01:00
|
|
|
|
|
2015-10-14 03:38:15 +02:00
|
|
|
|
``if`` takes any number of alternating *test* and *then* expressions, plus an
|
2016-11-24 03:35:17 +01:00
|
|
|
|
optional *else* expression at the end, which defaults to ``None``. ``if`` checks
|
2015-10-14 03:38:15 +02:00
|
|
|
|
each *test* in turn, and selects the *then* corresponding to the first passed
|
|
|
|
|
test. ``if`` does not evaluate any expressions following its selection, similar
|
|
|
|
|
to the ``if/elif/else`` control structure from Python. If no tests pass, ``if``
|
|
|
|
|
selects *else*.
|
|
|
|
|
|
|
|
|
|
The ``if*`` special form is restricted to 2 or 3 arguments, but otherwise works
|
|
|
|
|
exactly like ``if`` (which expands to nested ``if*`` forms), so there is
|
|
|
|
|
generally no reason to use it directly.
|
|
|
|
|
|
|
|
|
|
``if-not`` is similar to ``if*`` but the second expression will be executed
|
|
|
|
|
when the condition fails while the third and final expression is executed when
|
|
|
|
|
the test succeeds -- the opposite order of ``if*``. The final expression is
|
2016-11-24 03:35:17 +01:00
|
|
|
|
again optional and defaults to ``None``.
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
|
|
|
|
Example usage:
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2015-10-14 03:38:15 +02:00
|
|
|
|
(print (if (< n 0.0) "negative"
|
|
|
|
|
(= n 0.0) "zero"
|
|
|
|
|
(> n 0.0) "positive"
|
|
|
|
|
"not a number"))
|
|
|
|
|
|
|
|
|
|
(if* (money-left? account)
|
2014-12-06 21:28:28 +01:00
|
|
|
|
(print "let's go shopping")
|
|
|
|
|
(print "let's go and work"))
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
2014-01-18 16:27:26 +01:00
|
|
|
|
(if-not (money-left? account)
|
2014-12-06 21:28:28 +01:00
|
|
|
|
(print "let's go and work")
|
|
|
|
|
(print "let's go shopping"))
|
2014-01-18 16:27:26 +01:00
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
|
|
|
|
|
2015-08-09 09:21:12 +02:00
|
|
|
|
lif and lif-not
|
2014-09-10 18:55:11 +02:00
|
|
|
|
---------------------------------------
|
2014-02-24 16:39:45 +01:00
|
|
|
|
|
2014-04-13 16:42:48 +02:00
|
|
|
|
.. versionadded:: 0.10.0
|
|
|
|
|
|
2015-05-09 21:12:04 +02:00
|
|
|
|
.. versionadded:: 0.11.0
|
2015-08-09 09:21:12 +02:00
|
|
|
|
lif-not
|
2014-09-10 18:55:11 +02:00
|
|
|
|
|
2015-08-10 09:33:23 +02:00
|
|
|
|
For those that prefer a more Lispy ``if`` clause, we have
|
2016-11-24 03:35:17 +01:00
|
|
|
|
``lif``. This *only* considers ``None`` to be false! All other
|
2014-12-07 07:05:52 +01:00
|
|
|
|
"false-ish" Python values are considered true. Conversely, we have
|
2015-08-09 09:21:12 +02:00
|
|
|
|
``lif-not`` in parallel to ``if`` and ``if-not`` which
|
2014-12-07 07:05:52 +01:00
|
|
|
|
reverses the comparison.
|
2014-02-24 16:39:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2015-08-09 09:21:12 +02:00
|
|
|
|
=> (lif True "true" "false")
|
2014-02-24 16:39:45 +01:00
|
|
|
|
"true"
|
2015-08-09 09:21:12 +02:00
|
|
|
|
=> (lif False "true" "false")
|
2014-02-24 16:39:45 +01:00
|
|
|
|
"true"
|
2015-08-09 09:21:12 +02:00
|
|
|
|
=> (lif 0 "true" "false")
|
2014-02-24 16:39:45 +01:00
|
|
|
|
"true"
|
2015-08-09 09:21:12 +02:00
|
|
|
|
=> (lif None "true" "false")
|
2014-09-10 18:55:11 +02:00
|
|
|
|
"false"
|
|
|
|
|
=> (lif-not None "true" "false")
|
|
|
|
|
"true"
|
2015-08-09 09:21:12 +02:00
|
|
|
|
=> (lif-not False "true" "false")
|
|
|
|
|
"false"
|
2014-02-24 16:39:45 +01:00
|
|
|
|
|
2017-02-11 02:11:21 +01:00
|
|
|
|
.. _import:
|
2014-02-24 16:39:45 +01:00
|
|
|
|
|
2013-05-09 04:16:03 +02:00
|
|
|
|
import
|
|
|
|
|
------
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``import`` is used to import modules, like in Python. There are several ways
|
|
|
|
|
that ``import`` can be used.
|
2013-05-09 04:16:03 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
2013-05-09 04:32:11 +02:00
|
|
|
|
|
2013-05-09 04:16:03 +02:00
|
|
|
|
;; Imports each of these modules
|
|
|
|
|
;;
|
|
|
|
|
;; Python:
|
|
|
|
|
;; import sys
|
|
|
|
|
;; import os.path
|
|
|
|
|
(import sys os.path)
|
|
|
|
|
|
|
|
|
|
;; Import from a module
|
|
|
|
|
;;
|
|
|
|
|
;; Python: from os.path import exists, isdir, isfile
|
|
|
|
|
(import [os.path [exists isdir isfile]])
|
|
|
|
|
|
|
|
|
|
;; Import with an alias
|
|
|
|
|
;;
|
|
|
|
|
;; Python: import sys as systest
|
|
|
|
|
(import [sys :as systest])
|
|
|
|
|
|
|
|
|
|
;; You can list as many imports as you like of different types.
|
2016-09-26 05:29:53 +02:00
|
|
|
|
;;
|
|
|
|
|
;; Python:
|
|
|
|
|
;; from tests.resources import kwtest, function_with_a_dash
|
|
|
|
|
;; from os.path import exists, isdir as is_dir, isfile as is_file
|
|
|
|
|
;; import sys as systest
|
2013-05-09 04:16:03 +02:00
|
|
|
|
(import [tests.resources [kwtest function-with-a-dash]]
|
2016-09-26 05:29:53 +02:00
|
|
|
|
[os.path [exists
|
|
|
|
|
isdir :as dir?
|
|
|
|
|
isfile :as file?]]
|
2013-05-09 04:16:03 +02:00
|
|
|
|
[sys :as systest])
|
|
|
|
|
|
2014-02-11 16:25:17 +01:00
|
|
|
|
;; Import all module functions into current namespace
|
2016-09-26 05:29:53 +02:00
|
|
|
|
;;
|
|
|
|
|
;; Python: from sys import *
|
2014-02-11 16:25:17 +01:00
|
|
|
|
(import [sys [*]])
|
|
|
|
|
|
2013-05-09 04:16:03 +02:00
|
|
|
|
|
2017-02-23 00:36:52 +01:00
|
|
|
|
fn
|
2013-06-30 15:39:06 +02:00
|
|
|
|
-----------
|
2013-04-09 03:53:06 +02:00
|
|
|
|
|
2017-02-23 00:36:52 +01:00
|
|
|
|
``fn``, like Python's ``lambda``, can be used to define an anonymous function.
|
|
|
|
|
Unlike Python's ``lambda``, the body of the function can comprise several
|
|
|
|
|
statements. The parameters are similar to ``defn``: the first parameter is
|
|
|
|
|
vector of parameters and the rest is the body of the function. ``fn`` returns a
|
|
|
|
|
new function. In the following example, an anonymous function is defined and
|
|
|
|
|
passed to another function for filtering output.
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (def people [{:name "Alice" :age 20}
|
|
|
|
|
... {:name "Bob" :age 25}
|
|
|
|
|
... {:name "Charlie" :age 50}
|
|
|
|
|
... {:name "Dave" :age 5}])
|
|
|
|
|
|
|
|
|
|
=> (defn display-people [people filter]
|
2013-12-31 19:35:31 +01:00
|
|
|
|
... (for [person people] (if (filter person) (print (:name person)))))
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
|
|
|
|
=> (display-people people (fn [person] (< (:age person) 25)))
|
|
|
|
|
Alice
|
|
|
|
|
Dave
|
|
|
|
|
|
2014-02-23 22:20:43 +01:00
|
|
|
|
Just as in normal function definitions, if the first element of the
|
2014-12-07 07:05:52 +01:00
|
|
|
|
body is a string, it serves as a docstring. This is useful for giving
|
2014-02-23 22:20:43 +01:00
|
|
|
|
class methods docstrings.
|
|
|
|
|
|
2014-04-13 17:04:42 +02:00
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2014-02-23 22:20:43 +01:00
|
|
|
|
=> (setv times-three
|
|
|
|
|
... (fn [x]
|
|
|
|
|
... "Multiplies input by three and returns the result."
|
|
|
|
|
... (* x 3)))
|
|
|
|
|
|
2014-12-06 21:28:28 +01:00
|
|
|
|
This can be confirmed via Python's built-in ``help`` function::
|
2014-04-13 17:04:42 +02:00
|
|
|
|
|
2014-02-23 22:20:43 +01:00
|
|
|
|
=> (help times-three)
|
|
|
|
|
Help on function times_three:
|
|
|
|
|
|
|
|
|
|
times_three(x)
|
|
|
|
|
Multiplies input by three and returns result
|
|
|
|
|
(END)
|
2015-05-09 21:12:04 +02:00
|
|
|
|
|
2015-03-19 09:43:11 +01:00
|
|
|
|
last
|
|
|
|
|
-----------
|
|
|
|
|
|
2015-05-09 21:12:04 +02:00
|
|
|
|
.. versionadded:: 0.11.0
|
2015-03-19 09:43:11 +01:00
|
|
|
|
|
|
|
|
|
``last`` can be used for accessing the last element of a collection:
|
2014-02-23 22:20:43 +01:00
|
|
|
|
|
2015-03-19 09:43:11 +01:00
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (last [2 4 6])
|
|
|
|
|
6
|
2015-05-09 21:12:04 +02:00
|
|
|
|
|
2013-04-09 03:53:06 +02:00
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
list-comp
|
|
|
|
|
---------
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``list-comp`` performs list comprehensions. It takes two or three parameters.
|
2013-06-30 15:39:06 +02:00
|
|
|
|
The first parameter is the expression controlling the return value, while
|
|
|
|
|
the second is used to select items from a list. The third and optional
|
2014-12-06 21:28:28 +01:00
|
|
|
|
parameter can be used to filter out some of the items in the list based on a
|
2013-06-30 15:39:06 +02:00
|
|
|
|
conditional expression. Some examples:
|
2013-04-09 03:53:06 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
=> (def collection (range 10))
|
|
|
|
|
=> (list-comp x [x collection])
|
|
|
|
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
2013-04-09 03:53:06 +02:00
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
=> (list-comp (* x 2) [x collection])
|
|
|
|
|
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
|
|
|
|
|
|
|
|
|
|
=> (list-comp (* x 2) [x collection] (< x 5))
|
|
|
|
|
[0, 2, 4, 6, 8]
|
|
|
|
|
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
2015-05-11 23:38:02 +02:00
|
|
|
|
nonlocal
|
|
|
|
|
--------
|
|
|
|
|
|
2015-05-12 09:29:55 +02:00
|
|
|
|
.. versionadded:: 0.11.1
|
|
|
|
|
|
2015-05-12 00:00:54 +02:00
|
|
|
|
**PYTHON 3.0 AND UP ONLY!**
|
|
|
|
|
|
2015-05-11 23:38:02 +02:00
|
|
|
|
``nonlocal`` can be used to mark a symbol as not local to the current scope.
|
|
|
|
|
The parameters are the names of symbols to mark as nonlocal. This is necessary
|
2017-02-09 20:33:09 +01:00
|
|
|
|
to modify variables through nested ``fn`` scopes:
|
2015-05-11 23:38:02 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2015-05-12 09:30:22 +02:00
|
|
|
|
(defn some-function []
|
2017-02-09 20:33:09 +01:00
|
|
|
|
(setv x 0)
|
|
|
|
|
(register-some-callback
|
|
|
|
|
(fn [stuff]
|
|
|
|
|
(nonlocal x)
|
|
|
|
|
(setv x stuff))))
|
2015-05-12 09:30:22 +02:00
|
|
|
|
|
2017-02-09 20:33:09 +01:00
|
|
|
|
Without the call to ``(nonlocal x)``, the inner function would redefine ``x`` to
|
|
|
|
|
``stuff`` inside its local scope instead of overwriting the ``x`` in the outer
|
|
|
|
|
function.
|
2015-05-12 09:30:22 +02:00
|
|
|
|
|
|
|
|
|
See `PEP3104 <https://www.python.org/dev/peps/pep-3104/>`_ for further
|
|
|
|
|
information.
|
2015-05-11 23:38:02 +02:00
|
|
|
|
|
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
not
|
|
|
|
|
---
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``not`` is used in logical expressions. It takes a single parameter and
|
|
|
|
|
returns a reversed truth value. If ``True`` is given as a parameter, ``False``
|
|
|
|
|
will be returned, and vice-versa. Example usage:
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (not True)
|
|
|
|
|
False
|
|
|
|
|
|
|
|
|
|
=> (not False)
|
|
|
|
|
True
|
|
|
|
|
|
|
|
|
|
=> (not None)
|
|
|
|
|
True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
or
|
|
|
|
|
--
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``or`` is used in logical expressions. It takes at least two parameters. It
|
|
|
|
|
will return the first non-false parameter. If no such value exists, the last
|
2013-06-30 15:39:06 +02:00
|
|
|
|
parameter will be returned.
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (or True False)
|
|
|
|
|
True
|
|
|
|
|
|
|
|
|
|
=> (and False False)
|
|
|
|
|
False
|
|
|
|
|
|
|
|
|
|
=> (and False 1 True False)
|
|
|
|
|
1
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
.. note:: ``or`` short-circuits and stops evaluating parameters as soon as the
|
2014-12-06 21:28:28 +01:00
|
|
|
|
first true value is encountered.
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (or True (print "hello"))
|
|
|
|
|
True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print
|
|
|
|
|
-----
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``print`` is used to output on screen. Example usage:
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
(print "Hello world!")
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
.. note:: ``print`` always returns ``None``.
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
2013-11-26 19:31:10 +01:00
|
|
|
|
quasiquote
|
|
|
|
|
----------
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``quasiquote`` allows you to quote a form, but also selectively evaluate
|
|
|
|
|
expressions. Expressions inside a ``quasiquote`` can be selectively evaluated
|
|
|
|
|
using ``unquote`` (``~``). The evaluated form can also be spliced using
|
|
|
|
|
``unquote-splice`` (``~@``). Quasiquote can be also written using the backquote
|
|
|
|
|
(`````) symbol.
|
2013-11-26 19:31:10 +01:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
2013-11-30 20:50:58 +01:00
|
|
|
|
|
2013-11-26 19:31:10 +01:00
|
|
|
|
;; let `qux' be a variable with value (bar baz)
|
|
|
|
|
`(foo ~qux)
|
|
|
|
|
; equivalent to '(foo (bar baz))
|
|
|
|
|
`(foo ~@qux)
|
|
|
|
|
; equivalent to '(foo bar baz)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
quote
|
|
|
|
|
-----
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``quote`` returns the form passed to it without evaluating it. ``quote`` can
|
|
|
|
|
alternatively be written using the apostrophe (``'``) symbol.
|
2013-11-26 19:31:10 +01:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
2013-11-30 20:50:58 +01:00
|
|
|
|
|
2013-11-26 19:31:10 +01:00
|
|
|
|
=> (setv x '(print "Hello World"))
|
2017-08-26 06:50:13 +02:00
|
|
|
|
=> x ; varible x is set to unevaluated expression
|
|
|
|
|
HyExpression([
|
|
|
|
|
HySymbol('print'),
|
|
|
|
|
HyString('Hello World')])
|
2013-11-26 19:31:10 +01:00
|
|
|
|
=> (eval x)
|
|
|
|
|
Hello World
|
|
|
|
|
|
2013-11-30 20:50:58 +01:00
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
require
|
|
|
|
|
-------
|
|
|
|
|
|
Give `require` the same features as `import` (#1142)
Give `require` the same features as `import`
You can now do (require foo), (require [foo [a b c]]), (require [foo [*]]), and (require [foo :as bar]). The first and last forms get you macros named foo.a, foo.b, etc. or bar.a, bar.b, etc., respectively. The second form only gets the macros in the list.
Implements #1118 and perhaps partly addresses #277.
N.B. The new meaning of (require foo) will cause all existing code that uses macros to break. Simply replace these forms with (require [foo [*]]) to get your code working again.
There's a bit of a hack involved in the forms (require foo) or (require [foo :as bar]). When you call (foo.a ...) or (bar.a ...), Hy doesn't actually look inside modules. Instead, these (require ...) forms give the macros names that have periods in them, which happens to work fine with the way Hy finds and interprets macro calls.
* Make `require` syntax stricter and add tests
* Update documentation for `require`
* Documentation wording improvements
* Allow :as in `require` name lists
2016-11-03 08:35:58 +01:00
|
|
|
|
``require`` is used to import macros from one or more given modules. It allows
|
|
|
|
|
parameters in all the same formats as ``import``. The ``require`` form itself
|
|
|
|
|
produces no code in the final program: its effect is purely at compile-time, for
|
|
|
|
|
the benefit of macro expansion. Specifically, ``require`` imports each named
|
|
|
|
|
module and then makes each requested macro available in the current module.
|
2013-07-22 22:36:59 +02:00
|
|
|
|
|
Give `require` the same features as `import` (#1142)
Give `require` the same features as `import`
You can now do (require foo), (require [foo [a b c]]), (require [foo [*]]), and (require [foo :as bar]). The first and last forms get you macros named foo.a, foo.b, etc. or bar.a, bar.b, etc., respectively. The second form only gets the macros in the list.
Implements #1118 and perhaps partly addresses #277.
N.B. The new meaning of (require foo) will cause all existing code that uses macros to break. Simply replace these forms with (require [foo [*]]) to get your code working again.
There's a bit of a hack involved in the forms (require foo) or (require [foo :as bar]). When you call (foo.a ...) or (bar.a ...), Hy doesn't actually look inside modules. Instead, these (require ...) forms give the macros names that have periods in them, which happens to work fine with the way Hy finds and interprets macro calls.
* Make `require` syntax stricter and add tests
* Update documentation for `require`
* Documentation wording improvements
* Allow :as in `require` name lists
2016-11-03 08:35:58 +01:00
|
|
|
|
The following are all equivalent ways to call a macro named ``foo`` in the module ``mymodule``:
|
2013-07-22 22:36:59 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
Give `require` the same features as `import` (#1142)
Give `require` the same features as `import`
You can now do (require foo), (require [foo [a b c]]), (require [foo [*]]), and (require [foo :as bar]). The first and last forms get you macros named foo.a, foo.b, etc. or bar.a, bar.b, etc., respectively. The second form only gets the macros in the list.
Implements #1118 and perhaps partly addresses #277.
N.B. The new meaning of (require foo) will cause all existing code that uses macros to break. Simply replace these forms with (require [foo [*]]) to get your code working again.
There's a bit of a hack involved in the forms (require foo) or (require [foo :as bar]). When you call (foo.a ...) or (bar.a ...), Hy doesn't actually look inside modules. Instead, these (require ...) forms give the macros names that have periods in them, which happens to work fine with the way Hy finds and interprets macro calls.
* Make `require` syntax stricter and add tests
* Update documentation for `require`
* Documentation wording improvements
* Allow :as in `require` name lists
2016-11-03 08:35:58 +01:00
|
|
|
|
(require mymodule)
|
|
|
|
|
(mymodule.foo 1)
|
2013-07-22 22:36:59 +02:00
|
|
|
|
|
Give `require` the same features as `import` (#1142)
Give `require` the same features as `import`
You can now do (require foo), (require [foo [a b c]]), (require [foo [*]]), and (require [foo :as bar]). The first and last forms get you macros named foo.a, foo.b, etc. or bar.a, bar.b, etc., respectively. The second form only gets the macros in the list.
Implements #1118 and perhaps partly addresses #277.
N.B. The new meaning of (require foo) will cause all existing code that uses macros to break. Simply replace these forms with (require [foo [*]]) to get your code working again.
There's a bit of a hack involved in the forms (require foo) or (require [foo :as bar]). When you call (foo.a ...) or (bar.a ...), Hy doesn't actually look inside modules. Instead, these (require ...) forms give the macros names that have periods in them, which happens to work fine with the way Hy finds and interprets macro calls.
* Make `require` syntax stricter and add tests
* Update documentation for `require`
* Documentation wording improvements
* Allow :as in `require` name lists
2016-11-03 08:35:58 +01:00
|
|
|
|
(require [mymodule :as M])
|
|
|
|
|
(M.foo 1)
|
|
|
|
|
|
|
|
|
|
(require [mymodule [foo]])
|
|
|
|
|
(foo 1)
|
|
|
|
|
|
|
|
|
|
(require [mymodule [*]])
|
|
|
|
|
(foo 1)
|
|
|
|
|
|
|
|
|
|
(require [mymodule [foo :as bar]])
|
|
|
|
|
(bar 1)
|
|
|
|
|
|
|
|
|
|
Macros that call macros
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
One aspect of ``require`` that may be surprising is what happens when one
|
|
|
|
|
macro's expansion calls another macro. Suppose ``mymodule.hy`` looks like this:
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
(defmacro repexpr [n expr]
|
|
|
|
|
; Evaluate the expression n times
|
|
|
|
|
; and collect the results in a list.
|
|
|
|
|
`(list (map (fn [_] ~expr) (range ~n))))
|
|
|
|
|
|
|
|
|
|
(defmacro foo [n]
|
|
|
|
|
`(repexpr ~n (input "Gimme some input: ")))
|
|
|
|
|
|
|
|
|
|
And then, in your main program, you write:
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
(require [mymodule [foo]])
|
|
|
|
|
|
|
|
|
|
(print (mymodule.foo 3))
|
|
|
|
|
|
|
|
|
|
Running this raises ``NameError: name 'repexpr' is not defined``, even though
|
|
|
|
|
writing ``(print (foo 3))`` in ``mymodule`` works fine. The trouble is that your
|
|
|
|
|
main program doesn't have the macro ``repexpr`` available, since it wasn't
|
|
|
|
|
imported (and imported under exactly that name, as opposed to a qualified name).
|
|
|
|
|
You could do ``(require [mymodule [*]])`` or ``(require [mymodule [foo
|
|
|
|
|
repexpr]])``, but a less error-prone approach is to change the definition of
|
|
|
|
|
``foo`` to require whatever sub-macros it needs:
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
(defmacro foo [n]
|
|
|
|
|
`(do
|
|
|
|
|
(require mymodule)
|
|
|
|
|
(mymodule.repexpr ~n (raw-input "Gimme some input: "))))
|
|
|
|
|
|
|
|
|
|
It's wise to use ``(require mymodule)`` here rather than ``(require [mymodule
|
|
|
|
|
[repexpr]])`` to avoid accidentally shadowing a function named ``repexpr`` in
|
|
|
|
|
the main program.
|
|
|
|
|
|
|
|
|
|
Qualified macro names
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
Note that in the current implementation, there's a trick in qualified macro
|
|
|
|
|
names, like ``mymodule.foo`` and ``M.foo`` in the above example. These names
|
|
|
|
|
aren't actually attributes of module objects; they're just identifiers with
|
|
|
|
|
periods in them. In fact, ``mymodule`` and ``M`` aren't defined by these
|
|
|
|
|
``require`` forms, even at compile-time. None of this will hurt you unless try
|
|
|
|
|
to do introspection of the current module's set of defined macros, which isn't
|
|
|
|
|
really supported anyway.
|
2013-07-22 22:36:59 +02:00
|
|
|
|
|
2017-03-06 17:34:40 +01:00
|
|
|
|
rest
|
|
|
|
|
----
|
2013-07-22 22:36:59 +02:00
|
|
|
|
|
2017-03-06 17:34:40 +01:00
|
|
|
|
``rest`` takes the given collection and returns an iterable of all but the
|
|
|
|
|
first element.
|
2013-07-22 23:36:34 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2017-03-06 17:34:40 +01:00
|
|
|
|
=> (list (rest (range 10)))
|
2013-07-22 23:36:34 +02:00
|
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
|
|
|
|
2017-03-06 17:34:40 +01:00
|
|
|
|
Given an empty collection, it returns an empty iterable.
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (list (rest []))
|
|
|
|
|
[]
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
2017-08-20 17:45:17 +02:00
|
|
|
|
return
|
|
|
|
|
-------
|
|
|
|
|
|
|
|
|
|
``return`` compiles to a :py:keyword:`return` statement. It exits the
|
|
|
|
|
current function, returning its argument if provided with one or
|
|
|
|
|
``None`` if not.
|
|
|
|
|
|
|
|
|
|
.. code-block:: hy
|
|
|
|
|
|
|
|
|
|
=> (defn f [x] (for [n (range 10)] (when (> n x) (return n))))
|
|
|
|
|
=> (f 3.9)
|
|
|
|
|
4
|
|
|
|
|
|
|
|
|
|
Note that in Hy, ``return`` is necessary much less often than in Python,
|
|
|
|
|
since the last form of a function is returned automatically. Hence, an
|
|
|
|
|
explicit ``return`` is only necessary to exit a function early.
|
|
|
|
|
|
|
|
|
|
.. code-block:: hy
|
|
|
|
|
|
|
|
|
|
=> (defn f [x] (setv y 10) (+ x y))
|
|
|
|
|
=> (f 4)
|
|
|
|
|
14
|
|
|
|
|
|
|
|
|
|
To get Python's behavior of returning ``None`` when execution reaches
|
|
|
|
|
the end of a function, put ``None`` there yourself.
|
|
|
|
|
|
|
|
|
|
.. code-block:: hy
|
|
|
|
|
|
|
|
|
|
=> (defn f [x] (setv y 10) (+ x y) None)
|
|
|
|
|
=> (print (f 4))
|
|
|
|
|
None
|
|
|
|
|
|
2014-04-28 20:35:28 +02:00
|
|
|
|
set-comp
|
|
|
|
|
--------
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``set-comp`` is used to create sets. It takes two or three parameters.
|
2014-04-28 20:35:28 +02:00
|
|
|
|
The first parameter is for controlling the return value, while the second is
|
|
|
|
|
used to select items from a sequence. The third and optional parameter can be
|
|
|
|
|
used to filter out some of the items in the sequence based on a conditional
|
|
|
|
|
expression.
|
|
|
|
|
|
|
|
|
|
.. code-block:: hy
|
|
|
|
|
|
|
|
|
|
=> (setv data [1 2 3 4 5 2 3 4 5 3 4 5])
|
|
|
|
|
=> (set-comp x [x data] (odd? x))
|
|
|
|
|
{1, 3, 5}
|
|
|
|
|
|
|
|
|
|
|
2014-09-06 03:17:46 +02:00
|
|
|
|
cut
|
2013-06-30 15:39:06 +02:00
|
|
|
|
-----
|
|
|
|
|
|
2014-09-06 03:17:46 +02:00
|
|
|
|
``cut`` can be used to take a subset of a list and create a new list from it.
|
|
|
|
|
The form takes at least one parameter specifying the list to cut. Two
|
2013-06-30 15:39:06 +02:00
|
|
|
|
optional parameters can be used to give the start and end position of the
|
2014-12-07 07:05:52 +01:00
|
|
|
|
subset. If they are not supplied, the default value of ``None`` will be used
|
|
|
|
|
instead. The third optional parameter is used to control step between the elements.
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
2014-09-06 03:17:46 +02:00
|
|
|
|
``cut`` follows the same rules as its Python counterpart. Negative indices are
|
2014-12-06 21:28:28 +01:00
|
|
|
|
counted starting from the end of the list. Some example usage:
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (def collection (range 10))
|
|
|
|
|
|
2014-09-06 03:17:46 +02:00
|
|
|
|
=> (cut collection)
|
2013-06-30 15:39:06 +02:00
|
|
|
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
|
|
|
|
2014-09-06 03:17:46 +02:00
|
|
|
|
=> (cut collection 5)
|
2013-06-30 15:39:06 +02:00
|
|
|
|
[5, 6, 7, 8, 9]
|
|
|
|
|
|
2014-09-06 03:17:46 +02:00
|
|
|
|
=> (cut collection 2 8)
|
2013-06-30 15:39:06 +02:00
|
|
|
|
[2, 3, 4, 5, 6, 7]
|
|
|
|
|
|
2014-09-06 03:17:46 +02:00
|
|
|
|
=> (cut collection 2 8 2)
|
2013-06-30 15:39:06 +02:00
|
|
|
|
[2, 4, 6]
|
|
|
|
|
|
2014-09-06 03:17:46 +02:00
|
|
|
|
=> (cut collection -4 -2)
|
2013-06-30 15:39:06 +02:00
|
|
|
|
[6, 7]
|
2013-04-09 03:53:06 +02:00
|
|
|
|
|
|
|
|
|
|
2015-08-09 06:04:02 +02:00
|
|
|
|
raise
|
2013-04-09 03:53:06 +02:00
|
|
|
|
-------------
|
|
|
|
|
|
2015-08-10 09:33:23 +02:00
|
|
|
|
The ``raise`` form can be used to raise an ``Exception`` at
|
2014-12-07 07:05:52 +01:00
|
|
|
|
runtime. Example usage:
|
2013-04-09 03:53:06 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2015-08-09 06:04:02 +02:00
|
|
|
|
(raise)
|
2013-04-09 03:53:06 +02:00
|
|
|
|
; re-rase the last exception
|
2014-12-06 21:28:28 +01:00
|
|
|
|
|
2015-08-09 06:04:02 +02:00
|
|
|
|
(raise IOError)
|
|
|
|
|
; raise an IOError
|
2014-12-06 21:28:28 +01:00
|
|
|
|
|
2015-08-09 06:04:02 +02:00
|
|
|
|
(raise (IOError "foobar"))
|
|
|
|
|
; raise an IOError("foobar")
|
2013-04-09 03:53:06 +02:00
|
|
|
|
|
|
|
|
|
|
2015-08-09 06:04:02 +02:00
|
|
|
|
``raise`` can accept a single argument (an ``Exception`` class or instance)
|
2014-12-07 07:05:52 +01:00
|
|
|
|
or no arguments to re-raise the last ``Exception``.
|
2013-04-09 03:53:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
---
|
|
|
|
|
|
2017-05-14 03:35:12 +02:00
|
|
|
|
The ``try`` form is used to catch exceptions (``except``) and run cleanup
|
|
|
|
|
actions (``finally``).
|
2013-04-09 03:53:06 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
(try
|
2017-05-14 03:35:12 +02:00
|
|
|
|
(error-prone-function)
|
|
|
|
|
(except [ZeroDivisionError]
|
|
|
|
|
(print "Division by zero"))
|
|
|
|
|
(except [[IndexError KeyboardInterrupt]]
|
|
|
|
|
(print "Index error or Ctrl-C"))
|
|
|
|
|
(except [e ValueError]
|
|
|
|
|
(print "ValueError:" (repr e)))
|
|
|
|
|
(except [e [TabError PermissionError ReferenceError]]
|
|
|
|
|
(print "Some sort of error:" (repr e)))
|
|
|
|
|
(else
|
|
|
|
|
(print "No errors"))
|
|
|
|
|
(finally
|
|
|
|
|
(print "All done")))
|
|
|
|
|
|
|
|
|
|
The first argument of ``try`` is its body. (To put more than one form in the
|
|
|
|
|
body, use ``do``.) Then comes any number of ``except`` clauses, then optionally
|
|
|
|
|
an ``else`` clause, then optionally a ``finally`` clause. If an exception is
|
|
|
|
|
raised with a matching ``except`` clause during the execution of the body, that
|
|
|
|
|
``except`` clause will be executed. If no exceptions are raised, the ``else``
|
|
|
|
|
clause is executed. The ``finally`` clause will be executed last regardless of
|
|
|
|
|
whether an exception was raised.
|
|
|
|
|
|
|
|
|
|
The return value of ``try`` is the last form of the ``except`` clause that was
|
|
|
|
|
run, or the last form of ``else`` if no exception was raised, or the ``try``
|
|
|
|
|
body if there is no ``else`` clause.
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
|
|
|
|
|
2013-07-22 22:36:59 +02:00
|
|
|
|
unless
|
|
|
|
|
------
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
The ``unless`` macro is a shorthand for writing an ``if`` statement that checks if
|
|
|
|
|
the given conditional is ``False``. The following shows the expansion of this macro.
|
2013-07-22 23:36:34 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
(unless conditional statement)
|
|
|
|
|
|
2014-12-06 21:28:28 +01:00
|
|
|
|
(if conditional
|
|
|
|
|
None
|
2013-07-22 23:36:34 +02:00
|
|
|
|
(do statement))
|
2013-07-22 22:36:59 +02:00
|
|
|
|
|
2014-01-12 05:29:48 +01:00
|
|
|
|
|
2017-07-19 20:00:43 +02:00
|
|
|
|
unpack-iterable, unpack-mapping
|
|
|
|
|
-------------------------------
|
|
|
|
|
|
|
|
|
|
``unpack-iterable`` and ``unpack-mapping`` allow an iterable or mapping
|
|
|
|
|
object (respectively) to provide positional or keywords arguments
|
|
|
|
|
(respectively) to a function.
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (defn f [a b c d] [a b c d])
|
|
|
|
|
=> (f (unpack-iterable [1 2]) (unpack-mapping {"c" 3 "d" 4}))
|
|
|
|
|
[1, 2, 3, 4]
|
|
|
|
|
|
|
|
|
|
``unpack-iterable`` is usually written with the shorthand ``#*``, and
|
|
|
|
|
``unpack-mapping`` with ``#**``.
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (f #* [1 2] #** {"c" 3 "d" 4})
|
|
|
|
|
[1, 2, 3, 4]
|
|
|
|
|
|
|
|
|
|
With Python 3, you can unpack in an assignment list (:pep:`3132`).
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (setv [a #* b c] [1 2 3 4 5])
|
|
|
|
|
=> [a b c]
|
|
|
|
|
[1, [2, 3, 4], 5]
|
|
|
|
|
|
|
|
|
|
With Python 3.5 or greater, unpacking is allowed in more contexts than just
|
|
|
|
|
function calls, and you can unpack more than once in the same expression
|
|
|
|
|
(:pep:`448`).
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> [#* [1 2] #* [3 4]]
|
|
|
|
|
[1, 2, 3, 4]
|
|
|
|
|
=> {#** {1 2} #** {3 4}}
|
|
|
|
|
{1: 2, 3: 4}
|
|
|
|
|
=> (f #* [1] #* [2] #** {"c" 3} #** {"d" 4})
|
|
|
|
|
[1, 2, 3, 4]
|
|
|
|
|
|
|
|
|
|
|
2014-01-12 05:29:48 +01:00
|
|
|
|
unquote
|
|
|
|
|
-------
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
Within a quasiquoted form, ``unquote`` forces evaluation of a symbol. ``unquote``
|
|
|
|
|
is aliased to the tilde (``~``) symbol.
|
2014-01-12 05:29:48 +01:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2017-08-26 06:50:13 +02:00
|
|
|
|
=> (setv nickname "Cuddles")
|
|
|
|
|
=> (quasiquote (= nickname (unquote nickname)))
|
|
|
|
|
HyExpression([
|
|
|
|
|
HySymbol('='),
|
|
|
|
|
HySymbol('nickname'),
|
|
|
|
|
'Cuddles'])
|
|
|
|
|
=> `(= nickname ~nickname)
|
|
|
|
|
HyExpression([
|
|
|
|
|
HySymbol('='),
|
|
|
|
|
HySymbol('nickname'),
|
|
|
|
|
'Cuddles'])
|
2014-01-12 05:29:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unquote-splice
|
|
|
|
|
--------------
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``unquote-splice`` forces the evaluation of a symbol within a quasiquoted form,
|
2017-08-01 17:51:19 +02:00
|
|
|
|
much like ``unquote``. ``unquote-splice`` can be used when the symbol
|
2014-12-07 07:05:52 +01:00
|
|
|
|
being unquoted contains an iterable value, as it "splices" that iterable into
|
2017-08-01 17:51:19 +02:00
|
|
|
|
the quasiquoted form. ``unquote-splice`` can also be used when the value
|
|
|
|
|
evaluates to a false value such as ``None``, ``False``, or ``0``, in which
|
|
|
|
|
case the value is treated as an empty list and thus does not splice anything
|
|
|
|
|
into the form. ``unquote-splice`` is aliased to the ``~@`` syntax.
|
2014-01-12 05:29:48 +01:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2017-08-26 06:50:13 +02:00
|
|
|
|
=> (setv nums [1 2 3 4])
|
|
|
|
|
=> (quasiquote (+ (unquote-splice nums)))
|
|
|
|
|
HyExpression([
|
|
|
|
|
HySymbol('+'),
|
|
|
|
|
1,
|
|
|
|
|
2,
|
|
|
|
|
3,
|
|
|
|
|
4])
|
|
|
|
|
=> `(+ ~@nums)
|
|
|
|
|
HyExpression([
|
|
|
|
|
HySymbol('+'),
|
|
|
|
|
1,
|
|
|
|
|
2,
|
|
|
|
|
3,
|
|
|
|
|
4])
|
|
|
|
|
=> `[1 2 ~@(if (neg? (first nums)) nums)]
|
|
|
|
|
HyList([
|
|
|
|
|
HyInteger(1),
|
|
|
|
|
HyInteger(2)])
|
2017-08-01 17:51:19 +02:00
|
|
|
|
|
|
|
|
|
Here, the last example evaluates to ``('+' 1 2)``, since the condition
|
|
|
|
|
``(< (nth nums 0) 0)`` is ``False``, which makes this ``if`` expression
|
|
|
|
|
evaluate to ``None``, because the ``if`` expression here does not have an
|
|
|
|
|
else clause. ``unquote-splice`` then evaluates this as an empty value,
|
|
|
|
|
leaving no effects on the list it is enclosed in, therefore resulting in
|
|
|
|
|
``('+' 1 2)``.
|
2014-01-12 05:29:48 +01:00
|
|
|
|
|
2013-07-22 22:36:59 +02:00
|
|
|
|
when
|
|
|
|
|
----
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``when`` is similar to ``unless``, except it tests when the given conditional is
|
|
|
|
|
``True``. It is not possible to have an ``else`` block in a ``when`` macro. The
|
|
|
|
|
following shows the expansion of the macro.
|
2013-07-22 23:36:34 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
(when conditional statement)
|
|
|
|
|
|
|
|
|
|
(if conditional (do statement))
|
2013-07-22 22:36:59 +02:00
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
while
|
|
|
|
|
-----
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``while`` is used to execute one or more blocks as long as a condition is met.
|
|
|
|
|
The following example will output "Hello world!" to the screen indefinitely:
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
(while True (print "Hello world!"))
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
with
|
|
|
|
|
----
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``with`` is used to wrap the execution of a block within a context manager. The
|
2014-12-06 21:28:28 +01:00
|
|
|
|
context manager can then set up the local system and tear it down in a controlled
|
2014-12-07 07:05:52 +01:00
|
|
|
|
manner. The archetypical example of using ``with`` is when processing files.
|
|
|
|
|
``with`` can bind context to an argument or ignore it completely, as shown below:
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2015-08-13 16:37:56 +02:00
|
|
|
|
(with [arg (expr)] block)
|
2013-11-10 19:00:01 +01:00
|
|
|
|
|
2015-08-13 16:37:56 +02:00
|
|
|
|
(with [(expr)] block)
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
2015-08-13 16:37:56 +02:00
|
|
|
|
(with [arg (expr) (expr)] block)
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
The following example will open the ``NEWS`` file and print its content to the
|
|
|
|
|
screen. The file is automatically closed after it has been processed.
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2015-08-13 16:37:56 +02:00
|
|
|
|
(with [f (open "NEWS")] (print (.read f)))
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
2017-08-19 16:35:41 +02:00
|
|
|
|
``with`` returns the value of its last form, unless it suppresses an exception
|
|
|
|
|
(because the context manager's ``__exit__`` method returned true), in which
|
|
|
|
|
case it returns ``None``. So, the previous example could also be written
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
(print (with [f (open "NEWS")] (.read f)))
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
|
|
|
|
with-decorator
|
|
|
|
|
--------------
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``with-decorator`` is used to wrap a function with another. The function
|
2014-12-06 21:28:28 +01:00
|
|
|
|
performing the decoration should accept a single value: the function being
|
2014-12-07 07:05:52 +01:00
|
|
|
|
decorated, and return a new function. ``with-decorator`` takes a minimum
|
2014-12-06 21:28:28 +01:00
|
|
|
|
of two parameters: the function performing decoration and the function
|
|
|
|
|
being decorated. More than one decorator function can be applied; they
|
2014-05-23 19:33:38 +02:00
|
|
|
|
will be applied in order from outermost to innermost, ie. the first
|
2014-12-06 21:28:28 +01:00
|
|
|
|
decorator will be the outermost one, and so on. Decorators with arguments
|
2014-05-23 19:33:38 +02:00
|
|
|
|
are called just like a function call.
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
(with-decorator decorator-fun
|
|
|
|
|
(defn some-function [] ...)
|
|
|
|
|
|
|
|
|
|
(with-decorator decorator1 decorator2 ...
|
|
|
|
|
(defn some-function [] ...)
|
|
|
|
|
|
|
|
|
|
(with-decorator (decorator arg) ..
|
|
|
|
|
(defn some-function [] ...)
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
2014-05-23 19:33:38 +02:00
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
In the following example, ``inc-decorator`` is used to decorate the function
|
|
|
|
|
``addition`` with a function that takes two parameters and calls the
|
2014-05-23 19:33:38 +02:00
|
|
|
|
decorated function with values that are incremented by 1. When
|
2014-12-07 07:05:52 +01:00
|
|
|
|
the decorated ``addition`` is called with values 1 and 1, the end result
|
|
|
|
|
will be 4 (``1+1 + 1+1``).
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2014-05-23 19:33:38 +02:00
|
|
|
|
=> (defn inc-decorator [func]
|
2013-07-06 22:43:47 +02:00
|
|
|
|
... (fn [value-1 value-2] (func (+ value-1 1) (+ value-2 1))))
|
2014-05-23 19:33:38 +02:00
|
|
|
|
=> (defn inc2-decorator [func]
|
|
|
|
|
... (fn [value-1 value-2] (func (+ value-1 2) (+ value-2 2))))
|
|
|
|
|
|
2013-07-06 22:43:47 +02:00
|
|
|
|
=> (with-decorator inc-decorator (defn addition [a b] (+ a b)))
|
|
|
|
|
=> (addition 1 1)
|
|
|
|
|
4
|
2014-05-23 19:33:38 +02:00
|
|
|
|
=> (with-decorator inc2-decorator inc-decorator
|
|
|
|
|
... (defn addition [a b] (+ a b)))
|
|
|
|
|
=> (addition 1 1)
|
|
|
|
|
8
|
2013-07-06 22:43:47 +02:00
|
|
|
|
|
2015-07-26 23:19:10 +02:00
|
|
|
|
#@
|
|
|
|
|
~~
|
|
|
|
|
|
|
|
|
|
.. versionadded:: 0.12.0
|
|
|
|
|
|
2017-06-21 05:48:54 +02:00
|
|
|
|
The tag macro ``#@`` can be used as a shorthand for ``with-decorator``. With
|
2017-04-21 17:07:48 +02:00
|
|
|
|
``#@``, the previous example becomes:
|
2015-07-26 23:19:10 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> #@(inc-decorator (defn addition [a b] (+ a b)))
|
|
|
|
|
=> (addition 1 1)
|
|
|
|
|
4
|
|
|
|
|
=> #@(inc2-decorator inc-decorator
|
|
|
|
|
... (defn addition [a b] (+ a b)))
|
|
|
|
|
=> (addition 1 1)
|
|
|
|
|
8
|
|
|
|
|
|
|
|
|
|
|
2013-12-30 22:42:55 +01:00
|
|
|
|
.. _with-gensyms:
|
|
|
|
|
|
|
|
|
|
with-gensyms
|
|
|
|
|
-------------
|
|
|
|
|
|
|
|
|
|
.. versionadded:: 0.9.12
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``with-gensym`` is used to generate a set of :ref:`gensym` for use in a macro.
|
|
|
|
|
The following code:
|
2013-12-30 22:42:55 +01:00
|
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
|
.. code-block:: hy
|
2013-12-30 22:42:55 +01:00
|
|
|
|
|
|
|
|
|
(with-gensyms [a b c]
|
|
|
|
|
...)
|
|
|
|
|
|
|
|
|
|
expands to:
|
|
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
|
.. code-block:: hy
|
2013-12-30 22:42:55 +01:00
|
|
|
|
|
2017-02-09 20:33:09 +01:00
|
|
|
|
(do
|
|
|
|
|
(setv a (gensym)
|
|
|
|
|
b (gensym)
|
|
|
|
|
c (gensym))
|
2013-12-30 22:42:55 +01:00
|
|
|
|
...)
|
|
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
|
|
Section :ref:`using-gensym`
|
|
|
|
|
|
|
|
|
|
|
2015-08-02 22:57:46 +02:00
|
|
|
|
xor
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
.. versionadded:: 0.12.0
|
|
|
|
|
|
2017-01-31 23:31:27 +01:00
|
|
|
|
``xor`` performs the logical operation of exclusive OR. It takes two arguments.
|
|
|
|
|
If exactly one argument is true, that argument is returned. If neither is true,
|
|
|
|
|
the second argument is returned (which will necessarily be false). Otherwise,
|
|
|
|
|
when both arguments are true, the value ``False`` is returned.
|
2015-08-02 22:57:46 +02:00
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
2017-01-31 23:31:27 +01:00
|
|
|
|
=> [(xor 0 0) (xor 0 1) (xor 1 0) (xor 1 1)]
|
|
|
|
|
[0, 1, 1, False]
|
2015-08-02 22:57:46 +02:00
|
|
|
|
|
|
|
|
|
|
2013-06-30 15:39:06 +02:00
|
|
|
|
yield
|
|
|
|
|
-----
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``yield`` is used to create a generator object that returns one or more values.
|
2013-07-06 22:43:47 +02:00
|
|
|
|
The generator is iterable and therefore can be used in loops, list
|
|
|
|
|
comprehensions and other similar constructs.
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
The function ``random-numbers`` shows how generators can be used to generate
|
2013-07-06 22:43:47 +02:00
|
|
|
|
infinite series without consuming infinite amount of memory.
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
|
|
=> (defn multiply [bases coefficients]
|
2015-11-14 22:56:27 +01:00
|
|
|
|
... (for [(, base coefficient) (zip bases coefficients)]
|
2013-07-06 22:43:47 +02:00
|
|
|
|
... (yield (* base coefficient))))
|
|
|
|
|
|
|
|
|
|
=> (multiply (range 5) (range 5))
|
|
|
|
|
<generator object multiply at 0x978d8ec>
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
2013-07-06 22:43:47 +02:00
|
|
|
|
=> (list-comp value [value (multiply (range 10) (range 10))])
|
|
|
|
|
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
|
2013-06-30 15:39:06 +02:00
|
|
|
|
|
2013-07-06 22:43:47 +02:00
|
|
|
|
=> (import random)
|
|
|
|
|
=> (defn random-numbers [low high]
|
|
|
|
|
... (while True (yield (.randint random low high))))
|
2015-11-14 22:18:53 +01:00
|
|
|
|
=> (list-comp x [x (take 15 (random-numbers 1 50))])
|
2013-07-06 22:43:47 +02:00
|
|
|
|
[7, 41, 6, 22, 32, 17, 5, 38, 18, 38, 17, 14, 23, 23, 19]
|
2014-02-20 03:27:49 +01:00
|
|
|
|
|
2014-02-23 17:48:33 +01:00
|
|
|
|
|
|
|
|
|
yield-from
|
|
|
|
|
----------
|
|
|
|
|
|
|
|
|
|
.. versionadded:: 0.9.13
|
|
|
|
|
|
|
|
|
|
**PYTHON 3.3 AND UP ONLY!**
|
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
|
``yield-from`` is used to call a subgenerator. This is useful if you
|
2014-02-23 17:48:33 +01:00
|
|
|
|
want your coroutine to be able to delegate its processes to another
|
2014-12-06 21:28:28 +01:00
|
|
|
|
coroutine, say, if using something fancy like
|
2017-02-07 20:25:40 +01:00
|
|
|
|
`asyncio <https://docs.python.org/3.4/library/asyncio.html>`_.
|