I tried to get started with Hy

This article is like Lisp, but for the time being Python Part 2 Advent Calendar 2015 is the second day article.

What is Hy?

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.

Installation

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

Call a python module

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.

Define a function

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

Finally

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

I tried to get started with Hy
I tried to get started with Hy ・ Define a class
I tried to get started with blender python script_Part 01
I tried to get started with blender python script_Part 02
[Python] A memo that I tried to get started with asyncio
I tried to get CloudWatch data with Python
I tried to get started with Bitcoin Systre on the weekend
What I did to get started with Linux commands
Link to get started with python
How to get started with Scrapy
How to get started with Python
How to get started with Django
I tried to implement Autoencoder with TensorFlow
I tried to visualize AutoEncoder with TensorFlow
I tried to implement CVAE with PyTorch
I tried to solve TSP with QAOA
How to get started with laravel (Linux)
A memorandum when I tried to get it automatically with selenium
I wrote a script to get you started with AtCoder fast!
I started to analyze
I tried to debug.
Get started with MicroPython
I tried to paste
Get started with Mezzanine
I tried to predict next year with AI
I tried to detect Mario with pytorch + yolov3
I tried to implement reading Dataset with PyTorch
I tried to use lightGBM, xgboost with Boruta
The easiest way to get started with Django
I tried to learn logical operations with TF Learn
I tried to move GAN (mnist) with keras
I tried to get an image by scraping
I tried to save the data with discord
I tried to detect motion quickly with OpenCV
I tried to integrate with Keras in TFv1.1
I tried to output LLVM IR with Python
I tried to detect an object with M2Det!
I tried to automate sushi making with python
I tried to predict Titanic survival with PyCaret
I tried to operate Linux with Discord Bot
I tried to study DP with Fibonacci sequence
I tried to start Jupyter with Amazon lightsail
I tried to judge Tsundere with Naive Bayes
A layman wants to get started with Python
I tried to get the authentication code of Qiita API with Python.
I tried to get the movie information of TMDb API with Python
I tried to learn the sin function with chainer
I tried fp-growth with python
I tried scraping with Python
I tried to move machine learning (ObjectDetection) with TouchDesigner
I tried to create a table only with Django
Get started with Django! ~ Tutorial ⑤ ~
I tried to extract features with SIFT of OpenCV
I tried to move Faster R-CNN quickly with pytorch
I tried to learn PredNet
I tried to read and save automatically with VOICEROID2 2
I tried to implement and learn DCGAN with PyTorch
Get started with influxDB + Grafana
I tried Learning-to-Rank with Elasticsearch!
I tried to implement Minesweeper on terminal with python
I tried to touch the CSV file with Python