Log in

No account? Create an account
entries friends calendar profile Elf Sternberg's Pendorwright Projects Previous Previous Next Next
I used to write Python, but then I got Hy - Elf M. Sternberg
I used to write Python, but then I got Hy

This weekend I had a project to write.  I have a huge archive of MP3s left over from the mid-90s through mid-00′s, and they’re a bit of a mess and moving them onto my iPod is always a pain in the neck because they might be inconsistently labeled or have proto-Unicode issues or whatever.  So I decided to write a little command line utility that would help me sanify my files.  Since I’m old-school and organize my songs by album and genre, my tool  would pick the most frequent album and genre names from the ID3 files, while taking in alternative sources for those if they weren’t complete, such as the name of the directory (which also would give artist, if not otherwise available).  It would take the song title from the filename if it weren’t available in ID3.

And I decided to write it in Hy.  Hy is a Lisp parser that produces Python AST, and since the Python VM accepts and executes raw AST, it’s possible to write performant Python using a completely Lisp-like syntax.  Having not written Lisp in many years, this sounded like fun.

You can find the entire source code at my elfsternberg/mp_suggest repository on Github.

I like the feel of this code.  It’s a series of let statements that gets the MP3s and then creates a new list of details (genre, album, artist, title, position), then searches through the command line options and details for genre and album names.  Each let statement creates a new object; it looks mostly like immutable code, although that fantasy is blown by my frequent use of ap-reduce.  Some of the objects created are anonymous functions containing closures, so they’re new functions with pre-created knowledge of things they care about, such as the legal commands and whether or not to override the ID3 title with a modified copy of the filename.

Overall, it doesn’t feel much like Python.  It doesn’t feel like Javascript, either.  It feels most like RistrettoReginald Braithwaite’s version of Coffeescript, chock full of functional declarations rather than procedures.  The threading macros and anaphoric macros are huge time-savers, and the organizational principles of Lisp’s parenthetical syntax gave me plenty of vertical room to work with, a luxury I don’t usually have with Python’s whitespace-delimited syntax.

I’m not sure I’ll ever write in Hy again.  I’m not sure I have a need to; I know I’d never be able to talk my employer into using Hy; they don’t even want to discuss preprocessors for web apps like Haml, Less, or Coffeescript.  I’d love to have something like this that I really enjoyed using for Javascript, but I haven’t found anything beyond Coffeescript that really has lispy expressiveness and pythonic power.  Still, if I were doing more Python at home, Hy is really the language I’d be doing it in.  Hy for Python, Clojure for Java, and Coffeescript for Javascript.  Because Clojurescript is still not good enough.

Leave a comment