A(n) (un)comfortable introduction to Hy

Hy!

= Lisp + Python

But w(hy)?

Features

  • Lisp dialect. 
  • Hy <-> Python
  • It's actually a nice mix.

..but

  • No immutability, beyond what Python provides
  • No monads - not built-in, anyway.

But that doesn't mean it isn't useful...

pip install hy
=> (print "Hy!")
Hy!

=> (defn sayHy [name] (print (+ "Hy " name "!")))

=> (sayHy "Rudi")
Hy Rudi!

Damn, that's something.

Let's dive into Hy's syntax.

=> (+ 1 3 55)

=> (setv result (- (/ (+ 1 3 88) 2) 8))

 
=> (.strip " fooooo   ")

=> (setv this-string " fooooo   ")

=> (this-string.strip)
=> (if (try-some-thing)
      (print "this is if true")
      (print "this is if false"))

=> (cond
 [(> somevar 50)
  (print "That variable is too big!")]
 [(< somevar 10)
  (print "That variable is too small!")]
 [true
  (print "That variable is jussssst right!")])

=> (for [i (range 10)]
      (print (+ "'i' is now at " (str i))))
=> (import os)

=> (if (os.path.isdir "/tmp/somedir")
      (os.mkdir "/tmp/somedir/anotherdir")
      (print "Hey, that path isn't there!"))

=> (import datetime)

=> (datetime.datetime.now)
=> ; shameless plug from clojure's samples!

=> (list-comp
      (, x y)
      (x (range 8)
       y "ABCDEFGH"))
=> (loop (print (eval (read))))

=> (-> (read) (eval) (print) (loop))

=> (import [sh [cat grep wc]])

=> (wc (grep (cat "/usr/share/dict/words") "-E" "^hy") "-l")

=> (-> (cat "/usr/share/dict/words") (grep "-E" "^hy") (wc "-l"))
=> (defclass FooBar [object]
        "Yet Another Example Class"

      (defn --init-- [self x]
          (setv self.x x)
        None)

      (defn get-x [self]
        "Return our copy of x"
        self.x))
=> (defclass Customer [models.Model]
      [name (models.CharField :max-length 255})
       address (models.TextField)
       notes (models.TextField)])

Demos

(import [sympy.crypto.crypto [encipher_vigenere]])

(setv key "encrypt")

(setv pt "meet now.")

(encipher_vigenere pt key)
(import [sympy.crypto.crypto [rsa_private_key 
  encipher_rsa rsa_public_key]])

(setv (, p q e) (, 3 5 7))

(rsa_private_key p q e)

(setv puk (rsa_public_key p q e))

(setv pt 12)

(encipher_rsa pt puk)
(import [sympy [symbols]])

(setv (, x y) (symbols "x y"))

(setv expr (+ (* 2 y) x))
(+ expr 1)

(* expr x)

(import [sympy [expand factor]])

(setv expanded_expr (expand (* expr x)))

(factor expanded_expr)
(import [astropy.constants :as const])

(print const.c)
(import [flask [Flask]])
(import [flask [render_template]])

(require hy.contrib.meth)

(setv app (Flask "__main__"))

(route get-index "/" []
  (str "Hy world!"))

(post-route post-index "/post" []
  (str "Hy post world!"))

(route-with-methods both-index "/both" ["GET" "POST"] []
  (str "Hy to both worlds!"))

(apply app.run [] {"port" 5151})

I think I like Hy.

Not as much as Julia. But it's getting there.

Hail (Hy)dra!

=> (setv me (whoami))

=> (me.name)
Rudraksh MK

=> (me.bio)
Mathematician, coder, historian, amateur linguist. 
I love Carnatic progressive rock, especially Agam.
I build interesting communities. 
And I'm the community evangelist at Wingify, where I'm building a platform 
for product enthusiasts in India, called notCRUD.

=> (me.twitter)
@vectormein

=> (me.web)
http://rudimk.github.io
http://notcrud.com

=> (me.blog)
https://medium.com/@rudimk

=> (me.questions)
...

hy-lang

By Rudraksh M.K.

hy-lang

  • 4,206