Hypo As the title says, I made it so much that it is a promotion.
You may not be familiar with it in the first place, but Hy is a ** Lisp dialect that runs on a Python VM **. For example, if you read Squid Lisp: Hy written in Python, you can understand the atmosphere. You can understand it as ** Clojure's Python version ** for the JVM. Or rather, ** it looks like Clojure **, so you can highlight the syntax with markdown or ReST for the fucking minor language.
Hy itself can be executed with a dedicated interpreter, but the overhead of parsing is not good. So it's nice to use the attached command hyc
to convert it to a .pyc
file (byte-compiled Python script) before using it. It is also possible to convert it to a .py
file by using the same attached command hy2py
, but ** usually it cannot be operated as it is **. Therefore, it can only be used for debugging purposes. This is because the Python AST generated by Hy frequently has symbol names that cannot normally be used in Python. (For example, the variable name generated by gensym
starts with:
)
The above is not very good when you want to write a small application that simplifies daily tasks using Hy. It's a story that you only have to write a Makefile as soon as you hyc
, but it's not so cool **. Also, for Pythonista, it's kind of unpleasant to see the .pyc
file every day **.
Then, ** It's like putting all the Hy source code into an executable format **, so I made this article. ** Main subject **.
Please from PyPI.
$ sudo pip install hypo
$ hypo -o Output name Source file 1 Source file 2..
It is used like this. If the -o
option is omitted, the name will be ʻa. By the way, the entry point of the application is the
source file 1here. Note that input is only accepted for
.hy` files. The output is zipapp Regards, it is an archive file, but you can think of it as an execution binary because it has been given execution permission in advance. not.
For example, try building an application consisting of the following two files.
main.hy
(import [iota [iota]])
(defmain [&rest args]
(print (iota 10)))
iota.hy
(defn iota [m &optional [n 0] [step 1]]
(if (>= n m)
None
(cons n (iota m (+ n step) step))))
By the way, these codes are as follows in Python.
main.py
from iota import iota
if __name__ == '__main__':
print(iota(10))
iota.py
def iota(m, n=0, step=1):
if n >= m:
return ()
return (n, ) + iota(m, n + step, step)
Python doesn't normally use recursion, but functional languages are recursion. However, ** it is not tail recursion **, so I will refuse that it is a code that is not good for Lisp. By the way, when using tail recursion in Hy, [loop / recur
](http: // docs. hylang.org/en/latest/contrib/loop.html) Use macros.
So, let's get back to the main subject. Execute the following command from the terminal. This time, we will build with the name ʻexample`.
$ hypo -o example main.hy iota.hy
Then, a file called ʻexample` is created in the current directory, so try executing it.
$ ./example
(0L 1L 2L 3L 4L 5L 6L 7L 8L 9L)
The contents of main.hy
are executed. At this time, the file compiled to .pyc
is called inside the executable file, so it is a little faster than executing from the Hy interpreter. In addition, it is ** Hy specifications ** that the numerical output is not good.
I introduced how to use ** Hy source code is put together into an executable format ** with a miso-like feeling. It is not suitable for making very large-scale applications, but I hope you will use it. It is a package that I wrote in a few hours to kill time on holidays, so if you have a bug or request, please do pull request [issue](https: // github.com/koji-kojiro/hylang-hypo/issues) Please throw it. Thank you for reading this far.
Recommended Posts