06Dec

I used to write Python, but then I got Hy

Posted by Elf Sternberg as programming, python

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.

4 Responses to I used to write Python, but then I got Hy

Rui Carmo

December 8th, 2014 at 2:18 pm

You might want to take a look at https://github.com/Gozala/wisp – it’s a pretty nice Clojure-like LISP for JS.

Elf Sternberg

December 31st, 2014 at 7:21 pm

Actually, I’m really familiar with Wisp. I started writing my own Lisp, and found Wisp recently. I have some different plans for mine, using Escodegen as a last-stage intermediate language, using lisp lists as the primary representation, and building on features like pattern matching, polymorphism, transducers, automatic currying, and a bunch of other things.

As much as I’d like to use Wisp, there are things about it that bug me. (The CLI really bugs me, and the FFI is just… shudder0) And I’ve never written my own language, so this is a learning exercise as much as anything.

Pyfoo

March 30th, 2016 at 5:32 am

A Lisp bottle necked by GIL? NTY I’ll stick to Clojure.

Elf Sternberg

April 2nd, 2016 at 9:49 pm

So your contribution to the conversation is “My highly successful VM is better than your highly succesful VM?”

Do try harder.

Comment Form

Subscribe to Feed

Categories

Calendar

December 2014
M T W T F S S
« Oct   Jan »
1234567
891011121314
15161718192021
22232425262728
293031