Archive for July, 2009

Clojure/Java Interop

Hello again.

I picked up the book Programming Clojure and have made my way through the first two chapters which were great.

Last night I started the 3rd chapter, on Java Interoperability.

I was hoping to toss together a single Java class that I could then try to rewrite in Clojure to help me learn. Following is what I came up with. (Note, I am not a Java ninja by any stretch so beware).

This is not intended as any sort of comparison of the languages per se. It’s simply, “Given X, I had to do Y to get the same functionality”.

My idea was to find a Java library for parsing rss and then parse out the rss for this site, prining the Title of the Post, as well as the link (this is the lieberry I used).

First the Java:

And next the Clojure:

While I am not sure the Clojure code is as succinct as it could be since I am still pretty new, I do like the looks of it. I agree with the notion that it’s an expressive language, and that one can do quite a bit with a minimum of fuss. I think that practically the syntax is idiomatic, I am not entirely sold on the dot form, and dot dot macro. I don’t think they are as visually appealing as would be a symbol in this context. But they do make sense.

Happy learning!

n00b

I am a total Clojure n00b. But I am having a lot of fun learning it. It’s not like anything else I have learned to date.

I hadn’t really done much with it, other than watch presentations, mess around in the REPL, and with the help of Hexmode, get Clojure working with Slime in Emacs.

Yesterday and this morning during stolen minutes of solitude, I have been trying to get it to talk to a MySQL DB. Mostly, because I’d love to get to a point where I can, you know, actually do something with it :)

So this, for my fellow newbs that stumble here via google, is what I did.

For the purposes of demonstration, I created a 1 table db and gave it some lame data.

$ mysqladmin -uroot create hack
$ echo "CREATE TABLE hack(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY);" | mysql -u root hack
$ for i in $(seq 4);do echo "use hack; INSERT INTO hack VALUES()" | mysql -u root hack;done

I can assure you the following code, is likely not best practices, but I have to start somewhere. So I whipped this together by looking at the test.clj for the clojure.contrib.sql library on github (Open Source rocks like that). And a lot of googling. The comments are there for your benefit.

(comment Import the Clojure Contrib SQL Library)
(require '[clojure.contrib.sql :as ccsql])

(comment Set up the connection string)
(def db { :classname "com.mysql.jdbc.Driver"
:subprotocol "mysql"
:subname "//localhost/hack"
:user "root"
:create true })

(comment Define a function that does a SELECT * on whatever table you pass in)
(defn get-star
"Print all the rows in the table"
[table]
(ccsql/with-connection db
(ccsql/with-query-results res
[(str "SELECT * FROM " table)]
(doseq [rec res]
(println rec)))))

(comment Execute that function)
(get-star "hack")

Then to run that code, you have to add the mysql Driver jar to your classpath. (Obviously, manually doing all this won’t scale, and after a jar or two, you’ll want to build an executable jar of your own and adopt a build process, but for the purposes of demonstration, this is fine. In fact, I have my clojure script in /bin, so I don’t actually call this this way…but whatever)


$ java -cp "/opt/clojure/clojure.jar:/opt/clojure/clojure-contrib.jar:/opt/jars/mysql-connector-java-5.1.8-bin.jar" clojure.main test.clj
{:id 1}
{:id 2}
{:id 3}
{:id 4}

Ruby Hoedown

Signed up yesterday for the Ruby Hoedown, in Nashville.

Some friends and I (and a dude named Larry) are going to be heading down there in late August. Should be a blast.



In 5, 4, 3, 2, …

Starting Monday (albeit with a rest day) I’ll begin the long slow death march toward the Harrisburg Marathon.

I posted the schedule I am going to follow on my Current Schedule page. I am sure there are like 2 people who will actually want to see that, and both of you should know that I plan to change it a little after the first few weeks. I want to see how things go, but I plan to stick to it as tight as I can.

Anyway. I am pretty excited to be getting started. I have been running, slow, but healthy for a while, and I am really looking forward to giving it a shot.

I am under no illusions, I might get injured, I seem to be prone to it, but I will either get there, or get hurt, that much is certain :)