This is a bit dancy. Check it out.
I just spent a few hours trying to debug my movie vote GWT application failing to render a grid in Safari but working in other browsers. It turned out to be a Safari bug. Sigh...
The past week I've been learning about Clojure, a really neat Lisp language with easy Java interoperability. I've always been a bit turned off by Lisp simply due do it being so esoteric and difficult to find useful libraries to do practical work. Clojure gives you easy access to Java classes so it's much more appealing to me. There is pretty much a Java library out there to do anything you want. After experimenting with it, it makes writing Java code as easy as writing Python.
I wrote a system for doing movie voting, and part of that includes some Python scripts for determining review scores on Rotten Tomatoes and Metacritic. These seemed like great examples to try in Clojure. Clojure's documentation is rather terse, and it took me awhile to put all the pieces together. There are 2 scripts I made in Clojure: one that determines the Rotten Tomatoes or Metacritic URLs for a movie, and another that parses those HTML pages to determine the rating.
Here is the code to find the URLs using Google's Ajax API. Notice how ridiculously small this script is. Even using such libraries in Python results in much more code. What I especially liked is the arrow operator and how simple it was to parse JSON.
Here is the code to parse the HTML and determine the score/rating. What is amazing here is interacting with a Java htmlparser class. That class requires passing in an implementation of a NodeVisitor interface. So when each tag of an HTML page is visited, the NodeVisitor.visitTag is used. Clojure let's you define a proxy, which is your implementation of the interface in Clojure. Look again at how simple it is to traverse the HTML for the right tag.
The latter code also demonstrates the use of Clojure's run-time polymorphism. The method fetch-score is a multimethod that dispatches to the proper function depending on whether the argument url has 'rottentomatoes' or 'metacritic' within it. You can define arbitrary functions that determine how to dispatch to other functions. In this case it's simply based on a regex.
The code is missing some error handling, but in most cases an error results in an empty list that I don't really care about. You can also catch the proper exceptions if needed. The main thing I like about this is how short the code is. It takes awhile to understand Lisp, but it almost feels natural in some ways. Lisp seems to encourage minimal building-block functions and putting them all together, almost like putting together small Unix programs with pipes and filters. It also seems like there is a million ways to do something.
Donate to keep this site going!
| Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|
| << < | > >> | |||||
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | ||||