2013-04-05 20:56:59 +02:00
|
|
|
========
|
2013-04-03 23:21:31 +02:00
|
|
|
Tutorial
|
|
|
|
========
|
|
|
|
|
2013-05-21 01:26:36 +02:00
|
|
|
.. TODO
|
2014-12-06 08:13:40 +01:00
|
|
|
..
|
2013-05-21 01:26:36 +02:00
|
|
|
.. - How do I index into arrays or dictionaries?
|
|
|
|
.. - Blow your mind with macros!
|
|
|
|
.. - Where's my banana???
|
|
|
|
|
2013-04-03 23:27:01 +02:00
|
|
|
Welcome to the Hy tutorial!
|
2013-03-14 00:43:19 +01:00
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
In a nutshell, Hy is a Lisp dialect, but one that converts its
|
|
|
|
structure into Python ... literally a conversion into Python's abstract
|
2013-04-03 01:55:59 +02:00
|
|
|
syntax tree! (Or to put it in more crude terms, Hy is lisp-stick on a
|
2014-12-06 08:13:40 +01:00
|
|
|
Python!)
|
2013-04-03 01:55:59 +02:00
|
|
|
|
|
|
|
This is pretty cool because it means Hy is several things:
|
2013-04-01 16:27:30 +02:00
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
- A Lisp that feels very Pythonic
|
|
|
|
- For Lispers, a great way to use Lisp's crazy powers but in the wide
|
2013-04-01 16:27:30 +02:00
|
|
|
world of Python's libraries (why yes, you now can write a Django
|
2014-12-06 08:13:40 +01:00
|
|
|
application in Lisp!)
|
|
|
|
- For Pythonistas, a great way to start exploring Lisp, from the
|
|
|
|
comfort of Python!
|
2013-04-01 17:11:07 +02:00
|
|
|
- For everyone: a pleasant language that has a lot of neat ideas!
|
2013-04-01 16:27:30 +02:00
|
|
|
|
2017-08-23 20:12:53 +02:00
|
|
|
Now this tutorial assumes you're running Hy on Python 3. So know things
|
|
|
|
are a bit different if you're still using Python 2.
|
|
|
|
|
2013-03-14 00:43:19 +01:00
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
Basic intro to Lisp for Pythonistas
|
2013-04-05 20:56:59 +02:00
|
|
|
===================================
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
Okay, maybe you've never used Lisp before, but you've used Python!
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
A "hello world" program in Hy is actually super simple. Let's try it:
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2013-04-01 18:02:24 +02:00
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
(print "hello world")
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
See? Easy! As you may have guessed, this is the same as the Python
|
2013-04-01 14:10:40 +02:00
|
|
|
version of::
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2017-08-23 20:12:53 +02:00
|
|
|
print("hello world")
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2013-04-01 18:02:24 +02:00
|
|
|
To add up some super simple math, we could do:
|
|
|
|
|
|
|
|
.. code-block:: clj
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2013-04-01 18:02:24 +02:00
|
|
|
(+ 1 3)
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2013-04-01 18:02:24 +02:00
|
|
|
Which would return 4 and would be the equivalent of:
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2013-04-01 18:02:24 +02:00
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
1 + 3
|
2013-03-31 23:46:56 +02:00
|
|
|
|
|
|
|
What you'll notice is that the first item in the list is the function
|
|
|
|
being called and the rest of the arguments are the arguments being
|
2014-12-06 08:13:40 +01:00
|
|
|
passed in. In fact, in Hy (as with most Lisps) we can pass in
|
2013-04-01 18:02:24 +02:00
|
|
|
multiple arguments to the plus operator:
|
|
|
|
|
|
|
|
.. code-block:: clj
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2013-04-01 18:02:24 +02:00
|
|
|
(+ 1 3 55)
|
2013-03-31 23:46:56 +02:00
|
|
|
|
|
|
|
Which would return 59.
|
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
Maybe you've heard of Lisp before but don't know much about it. Lisp
|
|
|
|
isn't as hard as you might think, and Hy inherits from Python, so Hy
|
|
|
|
is a great way to start learning Lisp. The main thing that's obvious
|
|
|
|
about Lisp is that there's a lot of parentheses. This might seem
|
2013-03-31 23:46:56 +02:00
|
|
|
confusing at first, but it isn't so hard. Let's look at some simple
|
|
|
|
math that's wrapped in a bunch of parentheses that we could enter into
|
2014-12-06 08:13:40 +01:00
|
|
|
the Hy interpreter:
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2013-04-01 18:02:24 +02:00
|
|
|
.. code-block:: clj
|
|
|
|
|
2013-04-01 23:51:28 +02:00
|
|
|
(setv result (- (/ (+ 1 3 88) 2) 8))
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2017-08-23 20:12:53 +02:00
|
|
|
This would return 38.0 But why? Well, we could look at the equivalent
|
2013-03-31 23:46:56 +02:00
|
|
|
expression in python::
|
2014-12-06 08:13:40 +01:00
|
|
|
|
2013-03-31 23:46:56 +02:00
|
|
|
result = ((1 + 3 + 88) / 2) - 8
|
|
|
|
|
|
|
|
If you were to try to figure out how the above were to work in python,
|
|
|
|
you'd of course figure out the results by solving each inner
|
2014-12-06 08:13:40 +01:00
|
|
|
parenthesis. That's the same basic idea in Hy. Let's try this
|
|
|
|
exercise first in Python::
|
2013-03-31 23:46:56 +02:00
|
|
|
|
|
|
|
result = ((1 + 3 + 88) / 2) - 8
|
|
|
|
# simplified to...
|
|
|
|
result = (92 / 2) - 8
|
|
|
|
# simplified to...
|
2017-08-23 20:12:53 +02:00
|
|
|
result = 46.0 - 8
|
2013-03-31 23:46:56 +02:00
|
|
|
# simplified to...
|
2017-08-23 20:12:53 +02:00
|
|
|
result = 38.0
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
Now let's try the same thing in Hy:
|
2013-04-01 18:02:24 +02:00
|
|
|
|
|
|
|
.. code-block:: clj
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2013-04-01 23:51:28 +02:00
|
|
|
(setv result (- (/ (+ 1 3 88) 2) 8))
|
2013-04-01 18:02:24 +02:00
|
|
|
; simplified to...
|
2013-04-01 23:51:28 +02:00
|
|
|
(setv result (- (/ 92 2) 8))
|
2013-04-01 18:02:24 +02:00
|
|
|
; simplified to...
|
2017-08-23 20:12:53 +02:00
|
|
|
(setv result (- 46.0 8))
|
2013-04-01 18:02:24 +02:00
|
|
|
; simplified to...
|
2017-08-23 20:12:53 +02:00
|
|
|
(setv result 38.0)
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
As you probably guessed, this last expression with ``setv`` means to
|
2017-08-23 20:12:53 +02:00
|
|
|
assign the variable "result" to 38.0.
|
2013-03-31 23:46:56 +02:00
|
|
|
|
|
|
|
See? Not too hard!
|
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
This is the basic premise of Lisp. Lisp stands for "list
|
|
|
|
processing"; this means that the structure of the program is
|
|
|
|
actually lists of lists. (If you're familiar with Python lists,
|
2013-03-31 23:46:56 +02:00
|
|
|
imagine the entire same structure as above but with square brackets
|
2018-05-16 08:36:15 +02:00
|
|
|
instead, and you'll be able to see the structure above as both a
|
2016-02-11 18:45:01 +01:00
|
|
|
program and a data structure.) This is easier to understand with more
|
2014-12-06 08:13:40 +01:00
|
|
|
examples, so let's write a simple Python program, test it, and then
|
|
|
|
show the equivalent Hy program::
|
2013-03-31 23:46:56 +02:00
|
|
|
|
|
|
|
def simple_conversation():
|
2017-08-23 20:12:53 +02:00
|
|
|
print("Hello! I'd like to get to know you. Tell me about yourself!")
|
|
|
|
name = input("What is your name? ")
|
|
|
|
age = input("What is your age? ")
|
|
|
|
print("Hello " + name + "! I see you are " + age + " years old.")
|
2014-12-06 08:13:40 +01:00
|
|
|
|
2013-03-31 23:46:56 +02:00
|
|
|
simple_conversation()
|
2014-12-06 08:13:40 +01:00
|
|
|
|
2013-03-31 23:46:56 +02:00
|
|
|
If we ran this program, it might go like::
|
|
|
|
|
|
|
|
Hello! I'd like to get to know you. Tell me about yourself!
|
|
|
|
What is your name? Gary
|
|
|
|
What is your age? 38
|
|
|
|
Hello Gary! I see you are 38 years old.
|
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
Now let's look at the equivalent Hy program:
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2013-04-01 18:02:24 +02:00
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
(defn simple-conversation []
|
|
|
|
(print "Hello! I'd like to get to know you. Tell me about yourself!")
|
2017-08-23 20:12:53 +02:00
|
|
|
(setv name (input "What is your name? "))
|
|
|
|
(setv age (input "What is your age? "))
|
2013-04-01 18:02:24 +02:00
|
|
|
(print (+ "Hello " name "! I see you are "
|
|
|
|
age " years old.")))
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2013-04-02 00:59:26 +02:00
|
|
|
(simple-conversation)
|
|
|
|
|
2013-03-31 23:46:56 +02:00
|
|
|
If you look at the above program, as long as you remember that the
|
|
|
|
first element in each list of the program is the function (or
|
|
|
|
macro... we'll get to those later) being called and that the rest are
|
|
|
|
the arguments, it's pretty easy to figure out what this all means.
|
2014-12-06 08:13:40 +01:00
|
|
|
(As you probably also guessed, ``defn`` is the Hy method of defining
|
2013-03-31 23:46:56 +02:00
|
|
|
methods.)
|
|
|
|
|
|
|
|
Still, lots of people find this confusing at first because there's so
|
|
|
|
many parentheses, but there are plenty of things that can help make
|
|
|
|
this easier: keep indentation nice and use an editor with parenthesis
|
|
|
|
matching (this will help you figure out what each parenthesis pairs up
|
|
|
|
with) and things will start to feel comfortable.
|
|
|
|
|
|
|
|
There are some advantages to having a code structure that's actually a
|
2014-12-06 08:13:40 +01:00
|
|
|
very simple data structure as the core of Lisp is based on. For one
|
2013-03-31 23:46:56 +02:00
|
|
|
thing, it means that your programs are easy to parse and that the
|
|
|
|
entire actual structure of the program is very clearly exposed to you.
|
2016-02-11 18:45:01 +01:00
|
|
|
(There's an extra step in Hy where the structure you see is converted
|
2014-12-06 08:13:40 +01:00
|
|
|
to Python's own representations ... in "purer" Lisps such as Common
|
|
|
|
Lisp or Emacs Lisp, the data structure you see in the code and the
|
2013-03-31 23:46:56 +02:00
|
|
|
data structure that is executed is much more literally close.)
|
|
|
|
|
|
|
|
Another implication of this is macros: if a program's structure is a
|
|
|
|
simple data structure, that means you can write code that can write
|
|
|
|
code very easily, meaning that implementing entirely new language
|
2014-12-06 08:13:40 +01:00
|
|
|
features can be very fast. Previous to Hy, this wasn't very possible
|
|
|
|
for Python programmers ... now you too can make use of macros'
|
2013-03-31 23:46:56 +02:00
|
|
|
incredible power (just be careful to not aim them footward)!
|
|
|
|
|
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
Hy is a Lisp-flavored Python
|
2014-02-24 16:54:29 +01:00
|
|
|
============================
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2014-02-24 16:54:29 +01:00
|
|
|
Hy converts to Python's own abstract syntax tree, so you'll soon start
|
2013-04-01 14:10:40 +02:00
|
|
|
to find that all the familiar power of python is at your fingertips.
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2014-02-24 16:54:29 +01:00
|
|
|
You have full access to Python's data types and standard library in
|
|
|
|
Hy. Let's experiment with this in the hy interpreter::
|
2013-03-31 23:46:56 +02:00
|
|
|
|
|
|
|
=> [1 2 3]
|
|
|
|
[1, 2, 3]
|
|
|
|
=> {"dog" "bark"
|
|
|
|
... "cat" "meow"}
|
|
|
|
{'dog': 'bark', 'cat': 'meow'}
|
2013-05-09 22:35:47 +02:00
|
|
|
=> (, 1 2 3)
|
|
|
|
(1, 2, 3)
|
2015-06-26 23:47:35 +02:00
|
|
|
=> #{3 1 2}
|
|
|
|
{1, 2, 3}
|
2015-06-26 23:10:22 +02:00
|
|
|
=> 1/2
|
|
|
|
Fraction(1, 2)
|
|
|
|
|
|
|
|
Notice the last two lines: Hy has a fraction literal like Clojure.
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2017-03-24 17:03:12 +01:00
|
|
|
If you start Hy like this (a shell alias might be helpful)::
|
|
|
|
|
|
|
|
$ hy --repl-output-fn=hy.contrib.hy-repr.hy-repr
|
|
|
|
|
2017-03-25 18:13:44 +01:00
|
|
|
the interactive mode will use :ref:`hy-repr-fn` instead of Python's
|
2017-03-24 17:03:12 +01:00
|
|
|
native ``repr`` function to print out values, so you'll see values in
|
|
|
|
Hy syntax rather than Python syntax::
|
|
|
|
|
|
|
|
=> [1 2 3]
|
|
|
|
[1 2 3]
|
|
|
|
=> {"dog" "bark"
|
|
|
|
... "cat" "meow"}
|
|
|
|
{"dog" "bark" "cat" "meow"}
|
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
If you are familiar with other Lisps, you may be interested that Hy
|
2014-03-13 03:20:58 +01:00
|
|
|
supports the Common Lisp method of quoting:
|
2013-04-01 18:02:24 +02:00
|
|
|
|
|
|
|
.. code-block:: clj
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2014-03-13 03:20:58 +01:00
|
|
|
=> '(1 2 3)
|
2017-08-23 20:12:53 +02:00
|
|
|
(1 2 3)
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
You also have access to all the built-in types' nice methods::
|
2013-03-31 23:46:56 +02:00
|
|
|
|
|
|
|
=> (.strip " fooooo ")
|
|
|
|
"fooooo"
|
|
|
|
|
|
|
|
What's this? Yes indeed, this is precisely the same as::
|
|
|
|
|
|
|
|
" fooooo ".strip()
|
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
That's right---Lisp with dot notation! If we have this string
|
2013-04-01 18:02:24 +02:00
|
|
|
assigned as a variable, we can also do the following:
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2013-04-01 18:02:24 +02:00
|
|
|
.. code-block:: clj
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2013-04-01 23:51:28 +02:00
|
|
|
(setv this-string " fooooo ")
|
2013-04-01 18:02:24 +02:00
|
|
|
(this-string.strip)
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2013-04-01 18:02:24 +02:00
|
|
|
What about conditionals?:
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
(if (try-some-thing)
|
|
|
|
(print "this is if true")
|
|
|
|
(print "this is if false"))
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
As you can tell above, the first argument to ``if`` is a truth test, the
|
|
|
|
second argument is the body if true, and the third argument (optional!)
|
|
|
|
is if false (ie. ``else``).
|
2013-04-01 17:49:04 +02:00
|
|
|
|
2013-04-01 14:10:40 +02:00
|
|
|
If you need to do more complex conditionals, you'll find that you
|
2014-12-06 08:13:40 +01:00
|
|
|
don't have ``elif`` available in Hy. Instead, you should use something
|
|
|
|
called ``cond``. In Python, you might do something like::
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2013-04-01 14:14:23 +02:00
|
|
|
somevar = 33
|
|
|
|
if somevar > 50:
|
2017-08-23 20:12:53 +02:00
|
|
|
print("That variable is too big!")
|
2013-04-01 14:14:23 +02:00
|
|
|
elif somevar < 10:
|
2017-08-23 20:12:53 +02:00
|
|
|
print("That variable is too small!")
|
2013-04-01 14:14:23 +02:00
|
|
|
else:
|
2017-08-23 20:12:53 +02:00
|
|
|
print("That variable is jussssst right!")
|
2013-04-01 14:14:23 +02:00
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
In Hy, you would do:
|
2013-04-01 14:14:23 +02:00
|
|
|
|
2013-04-01 18:02:24 +02:00
|
|
|
.. code-block:: clj
|
|
|
|
|
2016-02-11 18:45:01 +01:00
|
|
|
(setv somevar 33)
|
2013-04-01 18:02:24 +02:00
|
|
|
(cond
|
2013-10-16 18:31:18 +02:00
|
|
|
[(> somevar 50)
|
|
|
|
(print "That variable is too big!")]
|
|
|
|
[(< somevar 10)
|
|
|
|
(print "That variable is too small!")]
|
2016-11-24 03:35:17 +01:00
|
|
|
[True
|
2013-10-16 18:31:18 +02:00
|
|
|
(print "That variable is jussssst right!")])
|
2013-04-01 14:14:23 +02:00
|
|
|
|
2016-06-25 00:24:20 +02:00
|
|
|
What you'll notice is that ``cond`` switches off between a statement
|
2013-04-01 14:14:23 +02:00
|
|
|
that is executed and checked conditionally for true or falseness, and
|
|
|
|
then a bit of code to execute if it turns out to be true. You'll also
|
2014-12-06 08:13:40 +01:00
|
|
|
notice that the ``else`` is implemented at the end simply by checking
|
2016-11-24 03:35:17 +01:00
|
|
|
for ``True`` -- that's because ``True`` will always be true, so if we get
|
2014-12-07 07:05:52 +01:00
|
|
|
this far, we'll always run that one!
|
2013-04-01 14:14:23 +02:00
|
|
|
|
2013-04-01 18:02:24 +02:00
|
|
|
You might notice above that if you have code like:
|
|
|
|
|
|
|
|
.. code-block:: clj
|
2013-04-01 17:49:04 +02:00
|
|
|
|
2013-04-01 18:02:24 +02:00
|
|
|
(if some-condition
|
|
|
|
(body-if-true)
|
|
|
|
(body-if-false))
|
2013-04-01 17:49:04 +02:00
|
|
|
|
2014-01-30 05:01:32 +01:00
|
|
|
But wait! What if you want to execute more than one statement in the
|
2013-04-01 17:49:04 +02:00
|
|
|
body of one of these?
|
|
|
|
|
2013-04-01 18:02:24 +02:00
|
|
|
You can do the following:
|
2013-04-01 17:49:04 +02:00
|
|
|
|
2013-04-01 18:02:24 +02:00
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
(if (try-some-thing)
|
|
|
|
(do
|
|
|
|
(print "this is if true")
|
2016-09-22 02:32:35 +02:00
|
|
|
(print "and why not, let's keep talking about how true it is!"))
|
2013-04-01 18:02:24 +02:00
|
|
|
(print "this one's still simply just false"))
|
2013-04-01 17:49:04 +02:00
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
You can see that we used ``do`` to wrap multiple statements. If you're
|
|
|
|
familiar with other Lisps, this is the equivalent of ``progn``
|
2013-04-01 17:49:04 +02:00
|
|
|
elsewhere.
|
|
|
|
|
2013-04-01 22:06:19 +02:00
|
|
|
Comments start with semicolons:
|
|
|
|
|
|
|
|
.. code-block:: clj
|
2013-04-01 17:58:13 +02:00
|
|
|
|
|
|
|
(print "this will run")
|
|
|
|
; (print "but this will not")
|
|
|
|
(+ 1 2 3) ; we'll execute the addition, but not this comment!
|
|
|
|
|
2015-12-09 11:48:44 +01:00
|
|
|
Hashbang (``#!``) syntax is supported:
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
#! /usr/bin/env hy
|
|
|
|
(print "Make me executable, and run me!")
|
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
Looping is not hard but has a kind of special structure. In Python,
|
2013-04-01 17:58:13 +02:00
|
|
|
we might do::
|
|
|
|
|
|
|
|
for i in range(10):
|
2017-08-23 20:12:53 +02:00
|
|
|
print("'i' is now at " + str(i))
|
2013-04-01 17:58:13 +02:00
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
The equivalent in Hy would be:
|
2013-04-01 22:06:19 +02:00
|
|
|
|
|
|
|
.. code-block:: clj
|
2013-04-01 17:58:13 +02:00
|
|
|
|
2014-01-30 05:01:32 +01:00
|
|
|
(for [i (range 10)]
|
|
|
|
(print (+ "'i' is now at " (str i))))
|
2013-04-01 17:58:13 +02:00
|
|
|
|
2019-03-09 08:43:41 +01:00
|
|
|
Python's collections indexes and slices are implemented
|
|
|
|
by the ``get`` and ``cut`` built-in:
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
(setv array [0 1 2])
|
|
|
|
(get array 1)
|
|
|
|
(cut array -3 -1)
|
|
|
|
|
|
|
|
which is equivalent to::
|
|
|
|
|
|
|
|
array[1]
|
|
|
|
array[-3:-1]
|
2013-04-01 17:59:13 +02:00
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
You can also import and make use of various Python libraries. For
|
2013-04-01 18:02:24 +02:00
|
|
|
example:
|
|
|
|
|
|
|
|
.. code-block:: clj
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2013-04-01 18:02:24 +02:00
|
|
|
(import os)
|
2014-12-06 08:13:40 +01:00
|
|
|
|
2013-04-01 18:02:24 +02:00
|
|
|
(if (os.path.isdir "/tmp/somedir")
|
|
|
|
(os.mkdir "/tmp/somedir/anotherdir")
|
|
|
|
(print "Hey, that path isn't there!"))
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Python's context managers (``with`` statements) are used like this:
|
2013-04-21 16:03:19 +02:00
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
.. code-block:: clj
|
|
|
|
|
2015-08-13 16:37:56 +02:00
|
|
|
(with [f (open "/tmp/data.in")]
|
2014-01-30 05:01:32 +01:00
|
|
|
(print (.read f)))
|
2013-04-21 16:03:19 +02:00
|
|
|
|
|
|
|
which is equivalent to::
|
|
|
|
|
2014-01-30 05:01:32 +01:00
|
|
|
with open("/tmp/data.in") as f:
|
2017-08-23 20:12:53 +02:00
|
|
|
print(f.read())
|
2014-12-06 08:13:40 +01:00
|
|
|
|
2015-01-14 17:46:26 +01:00
|
|
|
And yes, we do have List comprehensions! In Python you might do::
|
2013-04-03 02:12:33 +02:00
|
|
|
|
|
|
|
odds_squared = [
|
|
|
|
pow(num, 2)
|
|
|
|
for num in range(100)
|
|
|
|
if num % 2 == 1]
|
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
In Hy, you could do these like:
|
2013-04-03 02:12:33 +02:00
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
(setv odds-squared
|
2018-06-12 20:13:06 +02:00
|
|
|
(lfor
|
|
|
|
num (range 100)
|
|
|
|
:if (= (% num 2) 1)
|
|
|
|
(pow num 2)))
|
2013-03-31 23:46:56 +02:00
|
|
|
|
2013-04-03 02:56:10 +02:00
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
; And, an example stolen shamelessly from a Clojure page:
|
|
|
|
; Let's list all the blocks of a Chessboard:
|
2014-12-06 08:13:40 +01:00
|
|
|
|
2018-06-12 20:13:06 +02:00
|
|
|
(lfor
|
|
|
|
x (range 8)
|
|
|
|
y "ABCDEFGH"
|
|
|
|
(, x y))
|
2014-12-06 08:13:40 +01:00
|
|
|
|
2013-04-03 02:56:10 +02:00
|
|
|
; [(0, 'A'), (0, 'B'), (0, 'C'), (0, 'D'), (0, 'E'), (0, 'F'), (0, 'G'), (0, 'H'),
|
|
|
|
; (1, 'A'), (1, 'B'), (1, 'C'), (1, 'D'), (1, 'E'), (1, 'F'), (1, 'G'), (1, 'H'),
|
|
|
|
; (2, 'A'), (2, 'B'), (2, 'C'), (2, 'D'), (2, 'E'), (2, 'F'), (2, 'G'), (2, 'H'),
|
|
|
|
; (3, 'A'), (3, 'B'), (3, 'C'), (3, 'D'), (3, 'E'), (3, 'F'), (3, 'G'), (3, 'H'),
|
|
|
|
; (4, 'A'), (4, 'B'), (4, 'C'), (4, 'D'), (4, 'E'), (4, 'F'), (4, 'G'), (4, 'H'),
|
|
|
|
; (5, 'A'), (5, 'B'), (5, 'C'), (5, 'D'), (5, 'E'), (5, 'F'), (5, 'G'), (5, 'H'),
|
|
|
|
; (6, 'A'), (6, 'B'), (6, 'C'), (6, 'D'), (6, 'E'), (6, 'F'), (6, 'G'), (6, 'H'),
|
2014-01-18 12:22:33 +01:00
|
|
|
; (7, 'A'), (7, 'B'), (7, 'C'), (7, 'D'), (7, 'E'), (7, 'F'), (7, 'G'), (7, 'H')]
|
2013-04-03 02:56:10 +02:00
|
|
|
|
|
|
|
|
2013-05-09 22:35:47 +02:00
|
|
|
Python has support for various fancy argument and keyword arguments.
|
2014-12-06 08:13:40 +01:00
|
|
|
In Python we might see::
|
2013-05-09 22:35:47 +02:00
|
|
|
|
|
|
|
>>> def optional_arg(pos1, pos2, keyword1=None, keyword2=42):
|
|
|
|
... return [pos1, pos2, keyword1, keyword2]
|
2014-12-06 08:13:40 +01:00
|
|
|
...
|
2013-05-09 22:35:47 +02:00
|
|
|
>>> optional_arg(1, 2)
|
|
|
|
[1, 2, None, 42]
|
|
|
|
>>> optional_arg(1, 2, 3, 4)
|
|
|
|
[1, 2, 3, 4]
|
|
|
|
>>> optional_arg(keyword1=1, pos2=2, pos1=3, keyword2=4)
|
|
|
|
[3, 2, 1, 4]
|
|
|
|
|
2013-05-09 22:48:02 +02:00
|
|
|
The same thing in Hy::
|
2013-05-09 22:35:47 +02:00
|
|
|
|
2014-12-23 21:27:38 +01:00
|
|
|
=> (defn optional-arg [pos1 pos2 &optional keyword1 [keyword2 42]]
|
2013-05-09 22:35:47 +02:00
|
|
|
... [pos1 pos2 keyword1 keyword2])
|
2014-12-23 21:27:38 +01:00
|
|
|
=> (optional-arg 1 2)
|
2013-05-09 22:35:47 +02:00
|
|
|
[1 2 None 42]
|
2014-12-23 21:27:38 +01:00
|
|
|
=> (optional-arg 1 2 3 4)
|
2013-05-09 22:35:47 +02:00
|
|
|
[1 2 3 4]
|
2014-12-23 22:05:36 +01:00
|
|
|
|
2017-07-19 20:00:43 +02:00
|
|
|
You can call keyword arguments like this::
|
2014-12-23 22:05:36 +01:00
|
|
|
|
2014-12-23 21:27:38 +01:00
|
|
|
=> (optional-arg :keyword1 1
|
2014-12-23 21:39:40 +01:00
|
|
|
... :pos2 2
|
|
|
|
... :pos1 3
|
2014-12-22 22:52:34 +01:00
|
|
|
... :keyword2 4)
|
2013-05-09 22:35:47 +02:00
|
|
|
[3, 2, 1, 4]
|
|
|
|
|
2017-07-19 20:00:43 +02:00
|
|
|
You can unpack arguments with the syntax ``#* args`` and ``#** kwargs``,
|
|
|
|
similar to `*args` and `**kwargs` in Python::
|
2014-12-22 22:52:34 +01:00
|
|
|
|
|
|
|
=> (setv args [1 2])
|
|
|
|
=> (setv kwargs {"keyword2" 3
|
|
|
|
... "keyword1" 4})
|
2017-07-19 20:00:43 +02:00
|
|
|
=> (optional-arg #* args #** kwargs)
|
2014-12-22 22:52:34 +01:00
|
|
|
[1, 2, 4, 3]
|
2013-05-09 22:35:47 +02:00
|
|
|
|
2017-07-19 20:00:43 +02:00
|
|
|
Hy also supports ``*args`` and ``**kwargs`` in parameter lists. In Python::
|
2013-05-09 22:35:47 +02:00
|
|
|
|
|
|
|
def some_func(foo, bar, *args, **kwargs):
|
|
|
|
import pprint
|
|
|
|
pprint.pprint((foo, bar, args, kwargs))
|
|
|
|
|
|
|
|
The Hy equivalent:
|
|
|
|
|
2013-05-09 22:40:32 +02:00
|
|
|
.. code-block:: clj
|
|
|
|
|
2014-12-23 21:27:38 +01:00
|
|
|
(defn some-func [foo bar &rest args &kwargs kwargs]
|
2013-05-09 22:35:47 +02:00
|
|
|
(import pprint)
|
|
|
|
(pprint.pprint (, foo bar args kwargs)))
|
|
|
|
|
2014-12-06 08:13:40 +01:00
|
|
|
Finally, of course we need classes! In Python, we might have a class
|
2013-05-09 23:00:30 +02:00
|
|
|
like::
|
|
|
|
|
2014-01-30 05:01:32 +01:00
|
|
|
class FooBar(object):
|
2014-02-23 22:20:43 +01:00
|
|
|
"""
|
|
|
|
Yet Another Example Class
|
|
|
|
"""
|
|
|
|
def __init__(self, x):
|
|
|
|
self.x = x
|
2013-05-09 23:00:30 +02:00
|
|
|
|
2014-02-23 22:20:43 +01:00
|
|
|
def get_x(self):
|
|
|
|
"""
|
|
|
|
Return our copy of x
|
|
|
|
"""
|
|
|
|
return self.x
|
2018-05-16 05:29:07 +02:00
|
|
|
|
2017-03-09 05:39:41 +01:00
|
|
|
And we might use it like::
|
|
|
|
|
|
|
|
bar = FooBar(1)
|
2017-08-23 20:12:53 +02:00
|
|
|
print(bar.get_x())
|
2013-05-09 23:00:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
In Hy:
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
(defclass FooBar [object]
|
2014-02-23 22:20:43 +01:00
|
|
|
"Yet Another Example Class"
|
2014-12-06 08:13:40 +01:00
|
|
|
|
2015-08-04 16:43:07 +02:00
|
|
|
(defn --init-- [self x]
|
2015-10-17 14:07:32 +02:00
|
|
|
(setv self.x x))
|
2015-08-04 16:43:07 +02:00
|
|
|
|
|
|
|
(defn get-x [self]
|
|
|
|
"Return our copy of x"
|
|
|
|
self.x))
|
2018-05-16 05:29:07 +02:00
|
|
|
|
2017-03-09 05:39:41 +01:00
|
|
|
And we can use it like:
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
(setv bar (FooBar 1))
|
|
|
|
(print (bar.get-x))
|
2018-05-16 05:29:07 +02:00
|
|
|
|
2017-03-09 05:39:41 +01:00
|
|
|
Or using the leading dot syntax!
|
|
|
|
|
|
|
|
.. code-block:: clj
|
2013-05-09 23:00:30 +02:00
|
|
|
|
2017-03-09 05:39:41 +01:00
|
|
|
(print (.get-x (FooBar 1)))
|
2018-05-16 05:29:07 +02:00
|
|
|
|
2013-05-09 23:00:30 +02:00
|
|
|
|
|
|
|
You can also do class-level attributes. In Python::
|
|
|
|
|
|
|
|
class Customer(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
address = models.TextField()
|
|
|
|
notes = models.TextField()
|
|
|
|
|
|
|
|
In Hy:
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
(defclass Customer [models.Model]
|
2015-08-04 16:43:07 +02:00
|
|
|
[name (models.CharField :max-length 255})
|
|
|
|
address (models.TextField)
|
|
|
|
notes (models.TextField)])
|
2013-05-09 22:35:47 +02:00
|
|
|
|
2015-10-03 12:24:43 +02:00
|
|
|
Macros
|
|
|
|
======
|
|
|
|
|
2015-12-09 01:17:16 +01:00
|
|
|
One really powerful feature of Hy are macros. They are small functions that are
|
2015-10-03 12:24:43 +02:00
|
|
|
used to generate code (or data). When program written in Hy is started, the
|
2015-12-09 01:17:16 +01:00
|
|
|
macros are executed and their output is placed in the program source. After this,
|
2015-10-03 12:24:43 +02:00
|
|
|
the program starts executing normally. Very simple example:
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
=> (defmacro hello [person]
|
|
|
|
... `(print "Hello there," ~person))
|
2015-11-14 21:48:39 +01:00
|
|
|
=> (hello "Tuukka")
|
2015-10-03 12:24:43 +02:00
|
|
|
Hello there, Tuukka
|
|
|
|
|
|
|
|
The thing to notice here is that hello macro doesn't output anything on
|
|
|
|
screen. Instead it creates piece of code that is then executed and prints on
|
2015-12-09 01:17:16 +01:00
|
|
|
screen. This macro writes a piece of program that looks like this (provided that
|
|
|
|
we used "Tuukka" as parameter):
|
2015-10-03 12:24:43 +02:00
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
2017-06-12 15:36:47 +02:00
|
|
|
(print "Hello there," "Tuukka")
|
2015-10-03 12:24:43 +02:00
|
|
|
|
|
|
|
We can also manipulate code with macros:
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
=> (defmacro rev [code]
|
2017-02-09 20:33:09 +01:00
|
|
|
... (setv op (last code) params (list (butlast code)))
|
|
|
|
... `(~op ~@params))
|
2015-10-03 12:24:43 +02:00
|
|
|
=> (rev (1 2 3 +))
|
|
|
|
6
|
|
|
|
|
2015-12-09 01:17:16 +01:00
|
|
|
The code that was generated with this macro just switched around some of the
|
|
|
|
elements, so by the time program started executing, it actually reads:
|
2015-10-03 12:24:43 +02:00
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
(+ 1 2 3)
|
|
|
|
|
2017-04-21 17:07:48 +02:00
|
|
|
Sometimes it's nice to be able to call a one-parameter macro without
|
2017-06-21 05:48:54 +02:00
|
|
|
parentheses. Tag macros allow this. The name of a tag macro is typically
|
2017-04-21 17:07:48 +02:00
|
|
|
one character long, but since Hy operates well with Unicode, we aren't running
|
|
|
|
out of characters that soon:
|
2015-10-03 12:24:43 +02:00
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
2017-06-21 05:48:54 +02:00
|
|
|
=> (deftag ↻ [code]
|
2017-02-09 20:33:09 +01:00
|
|
|
... (setv op (last code) params (list (butlast code)))
|
|
|
|
... `(~op ~@params))
|
2015-10-03 12:24:43 +02:00
|
|
|
=> #↻(1 2 3 +)
|
|
|
|
6
|
|
|
|
|
2015-12-09 01:17:16 +01:00
|
|
|
Macros are useful when one wishes to extend Hy or write their own
|
2015-10-03 12:24:43 +02:00
|
|
|
language on top of that. Many features of Hy are macros, like ``when``,
|
|
|
|
``cond`` and ``->``.
|
|
|
|
|
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
|
|
|
What if you want to use a macro that's defined in a different
|
|
|
|
module? The special form ``import`` won't help, because it merely
|
|
|
|
translates to a Python ``import`` statement that's executed at
|
|
|
|
run-time, and macros are expanded at compile-time, that is,
|
|
|
|
during the translate from Hy to Python. Instead, use ``require``,
|
|
|
|
which imports the module and makes macros available at
|
|
|
|
compile-time. ``require`` uses the same syntax as ``import``.
|
2015-10-17 13:32:01 +02:00
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
=> (require tutorial.macros)
|
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
|
|
|
=> (tutorial.macros.rev (1 2 3 +))
|
2015-10-17 13:32:01 +02:00
|
|
|
6
|
|
|
|
|
2014-12-06 13:13:57 +01:00
|
|
|
Hy <-> Python interop
|
2014-12-06 17:10:35 +01:00
|
|
|
=====================
|
2014-12-06 13:13:57 +01:00
|
|
|
|
2017-02-11 02:11:21 +01:00
|
|
|
Using Hy from Python
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
You can use Hy modules in Python!
|
2014-12-06 13:13:57 +01:00
|
|
|
|
2014-12-06 17:10:35 +01:00
|
|
|
If you save the following in ``greetings.hy``:
|
2014-12-06 13:13:57 +01:00
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
(defn greet [name] (print "hello from hy," name))
|
|
|
|
|
2017-02-11 02:11:21 +01:00
|
|
|
Then you can use it directly from Python, by importing Hy before importing
|
2014-12-06 13:13:57 +01:00
|
|
|
the module. In Python::
|
|
|
|
|
2014-12-06 17:10:35 +01:00
|
|
|
import hy
|
|
|
|
import greetings
|
|
|
|
|
|
|
|
greetings.greet("Foo")
|
2014-12-06 13:13:57 +01:00
|
|
|
|
2017-02-11 02:11:21 +01:00
|
|
|
Using Python from Hy
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
You can also use any Python module in Hy!
|
2014-12-06 13:13:57 +01:00
|
|
|
|
2014-12-06 17:10:35 +01:00
|
|
|
If you save the following in ``greetings.py`` in Python::
|
2014-12-06 13:13:57 +01:00
|
|
|
|
2014-12-06 17:10:35 +01:00
|
|
|
def greet(name):
|
|
|
|
print("hello, %s" % (name))
|
2014-12-06 13:13:57 +01:00
|
|
|
|
2017-02-11 02:11:21 +01:00
|
|
|
You can use it in Hy (see :ref:`import`):
|
2014-12-06 13:13:57 +01:00
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
2014-12-06 17:10:35 +01:00
|
|
|
(import greetings)
|
|
|
|
(.greet greetings "foo")
|
2014-12-06 13:13:57 +01:00
|
|
|
|
2017-02-11 02:11:21 +01:00
|
|
|
More information on :doc:`../language/interop`.
|
2014-12-06 13:13:57 +01:00
|
|
|
|
2013-04-03 02:56:10 +02:00
|
|
|
|
2013-04-02 03:20:44 +02:00
|
|
|
Protips!
|
2013-04-05 20:56:59 +02:00
|
|
|
========
|
2013-04-02 03:20:44 +02:00
|
|
|
|
|
|
|
Hy also features something known as the "threading macro", a really neat
|
2014-12-07 07:05:52 +01:00
|
|
|
feature of Clojure's. The "threading macro" (written as ``->``) is used
|
2013-04-02 03:20:44 +02:00
|
|
|
to avoid deep nesting of expressions.
|
|
|
|
|
|
|
|
The threading macro inserts each expression into the next expression's first
|
|
|
|
argument place.
|
|
|
|
|
|
|
|
Let's take the classic:
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
2017-08-23 20:12:53 +02:00
|
|
|
(require [hy.contrib.loop [loop]])
|
|
|
|
|
2013-04-02 03:20:44 +02:00
|
|
|
(loop (print (eval (read))))
|
|
|
|
|
2014-05-05 20:17:14 +02:00
|
|
|
Rather than write it like that, we can write it as follows:
|
2013-04-03 23:27:20 +02:00
|
|
|
|
2013-04-02 03:20:44 +02:00
|
|
|
.. code-block:: clj
|
|
|
|
|
2017-08-23 20:12:53 +02:00
|
|
|
(require [hy.contrib.loop [loop]])
|
|
|
|
|
2013-04-02 03:20:44 +02:00
|
|
|
(-> (read) (eval) (print) (loop))
|
|
|
|
|
|
|
|
Now, using `python-sh <http://amoffat.github.com/sh/>`_, we can show
|
|
|
|
how the threading macro (because of python-sh's setup) can be used like
|
|
|
|
a pipe:
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
2013-04-20 16:06:32 +02:00
|
|
|
=> (import [sh [cat grep wc]])
|
2013-04-02 03:20:44 +02:00
|
|
|
=> (-> (cat "/usr/share/dict/words") (grep "-E" "^hy") (wc "-l"))
|
|
|
|
210
|
|
|
|
|
|
|
|
Which, of course, expands out to:
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
(wc (grep (cat "/usr/share/dict/words") "-E" "^hy") "-l")
|
|
|
|
|
2013-05-09 22:11:42 +02:00
|
|
|
Much more readable, no? Use the threading macro!
|