This article is like Lisp, but for the time being Python Part 2 Advent Calendar 2015 is the second day article.
Hy is a Lisp interpreter implemented in Python.
All the programming paths lead to Lisp, so let's get started with Hy as a step along the way.
Easy to install with pip
.
pip install hy
I tried to start it.
% hy
hy 0.11.0 using CPython(default) 2.7.6 on Linux
=> (print "Hello, World")
Hello, World
The first thing to worry about is how to call the Python standard module and Python's standard library, so I will try it.
Get the datetime.datetime
object.
=> (import datetime)
=> (datetime.datetime.now)
datetime.datetime(2015, 12, 2, 3, 25, 37, 20456)
Regular expression match with re
.
=> (re.findall r"([a-zA-Z]+)" "Spam Egg Ham")
[u'Spam', u'Egg', u'Ham']
=> (.groups (re.match r"([a-zA-Z]+)" "Spam Egg Ham"))
(u'Spam',)
Send HTTP requests with requests
. You need to pip install requests
in advance.
=> (import requests)
=> (requests.get "https://ticketcamp.net/")
<Response [200]>
If you're used to Lisp syntax, you can use it normally.
Use defn
.
=> (defn plus2 [x](+ 2 x))
=> (plus2 1)
3L
map, filter
Often used in Lisp introductory books [ʻinc](http://docs.hylang.org/en/latest/language/core.html#inc), [ʻodd?
](http: // For docs.hylang.org/en/latest/language/core.html#odd), [ʻeven? ](Http://docs.hylang.org/en/latest/language/core.html#even) It was predefined. Use these to try
map,
filter`.
First of all, map
.
=> (map inc [1 2 3])
<itertools.imap object at 0x7fb7b0193f50>
=> (list (map inc [1 2 3]))
[2L, 3L, 4L]
Then filter
.
=> (filter odd? [1 2 3])
<itertools.ifilter object at 0x7fb7b13f59d0>
=> (list (filter odd? [1 2 3]))
[1L, 3L]
The ->>
macro seemed to work, so I'll try the process in Clojure Transducers Example.
=> (def coll [1 2 3])
=> (->> coll (filter odd?) (map inc) (take 1))
<itertools.islice object at 0x7fb7b0199cb0>
=> (list (->> coll (filter odd?) (map inc) (take 1)))
[2L]
dict
An object equivalent to Python's dict
. (What do you call it in Lisp?)
=> (def data {:spam 1 :egg 2 :ham 3})
=> (:spam data)
1L
After trying various things, I got the impression that I shouldn't touch it with the same feeling as Clojure, which I personally often touch recently. But even though all programming goes to Lisp, it's hard to imagine making anything with it.
Next time, I would like to consider how to use Hy.
Recommended Posts