Bienvenidos
Guillermo Vayá Pérez
pip install hy
pip install -e <github repo>
Paul Tagliamonte
@paultag
Introduccion a Lisp en general
(Y a Hy en particular)
listas
(primero resto) (primero (primero resto))
(+ 1 2)
"suma" "x" "y"
3 1 4 5 6
3.3
"Hy Python!"
Y variables
(setv who "people")
(saluda-a who)
[1 2 3]
{"Hy" "0.9.11" "Python" "3.3" }
(, 1 2 3)
Para invocarlas simplemente recolocamos los parentesis: notacion polaca
(len [1 2 3])
Para crear nuevas funciones:
(defun saluda-a [x] (+ "hy " x "!")) (saluda-a "people")
Lambdas
(fn [x] (+ x 1))
(defclass Persona [ [--init-- (fn [name] (setv self.name name) None) ] [name None] [saluda-a (fn [quien] (print (% "%s saluda a %s" (, self.name quien)))) ]]) (setv yo (Persona "Guillermo"))
(list-comp (, x y) (x (range 9) y "ABCDEFGH")) ; [(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'), ; (7, 'A'), (7, 'B'), (7, 'C'), (7, 'D'), (7, 'E'), (7, 'F'), (7, 'G'), (7, 'H'), ; (8, 'A'), (8, 'B'), (8, 'C'), (8, 'D'), (8, 'E'), (8, 'F'), (8, 'G'), (8, 'H')]
=> (defn inc-decorator [func] ... (fn [value-1 value-2] (func (+ value-1 1) (+ value-2 1)))) => (with-decorator inc-decorator (defn addition [a b] (+ a b))) => (addition 1 1) 4
(defn random-numbers [low high] ... (while True (yield (.randint random low high)))) => (list-comp x [x (take 15 (random-numbers 1 50))])]) [7, 41, 6, 22, 32, 17, 5, 38, 18, 38, 17, 14, 23, 23, 19]
Clojure en versión Python:
(import json) (import [datetime [datetime]])
Veamos un pequeño ejemplo
Python -> Ast -> Bytecode
^ Hy
ast.fix_missing_locations
(no sirve en Hy)
>>> import ast >>> ast.dump(ast.parse("len([])")) Module(body=[Expr( value=Call(func=Name(id='len', ctx=Load()), args=[List(elts=[], ctx=Load())], keywords=[], starargs=None, kwargs=None))])
Hy tiene sus propios tipos, que posteriormente se traducen a python
=> (defmacro show-your-type [x] (print (type x)) `~x) => (show-your-type {}) <class 'hy.models.dict.HyDict'> {} => (show-your-type 1) <class 'hy.models.integer.HyInteger'> 1
Algunos no son lo que parecen: HyDict
(require hy.contrib.meth)
Fuente: http://xkcd.com/1289/