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 :)

Training Log 06.27.09

I haven’t posted a training log in a while, and thought it might be time.

I was up till about 1:30 last night and woke up at 6:30 this morning. So on 5 hours sleep I was feeling a little washed out today.

I knew I had a 4 miler that I needed to do, but I kept putting it off because it was so hot and I was so tired that I thought it might be miserable.

Nope. Best run I have had in weeks. I felt great the whole time. It was one of those runs that causes you to fall in love with running in the first place.

When I got back, a certain 2 year old was hogging the shower, so I had to cool my heels at the PC. I figured no better time than the present to switch to using connect.garmin.com.

I think it’s a pretty sweet little app now. Motionbased was such a dog.

Anyway, here is my run.

Happy running!

And the circle remains unbroken

About 2 (?) months ago I stopped wearing my orthotics to see if that would help my heel. I think I can say unequivocally that it has. I am running a consistent 20 miles per week, and my heel (though not entirely healed) feels better than in has in a year.

However (you just knew there was going to be a however didn’t you), my hip (remember my hip? the whole reason I started wearing orthotics in the first place?) is starting to feel the same way it did when I went to get orthotics in the first place.

It’s not quite as bad as it was then, but it’s bad enough that I know I have to start wearing the orthotics again.

I plan to try wearing them only when I am walking around, and not on runs, at least initially.

I also hope to put up a marathon training post soon, as well as my schedule.

Running, cats and hacking

I am working on a post about the wireless ap I built using FreeBSD, but in the mean time I thought I’d post on a few personal items.

First, I am hacking away on various at home projects, not the least of which is the aforementioned wireless access point. That was a blast, and I hope record the steps I took in decent detail for the post. In fact, I am connected to my lan via said ap right now.

Eventually it will be my only access point, and router, but I am planning a vacation for July (to a beach house with no net), and I want to take it with me. I plan to use the GSM modem I bought as the wan interface, and the atheros card I put in there on the lan side. To that end, I thought I should get the ap working first.

Running is going ok. I am not at my peak performance. I decided about a month ago to try an experiment. I stopped using my orthotics to see if that helped my heel pain. And while my heel does feel a little better (at least I think it does) my knees have been aching a lot in protest to the new gait I am sure I have developed.

So I am sitting here currently with ice packs on my knees, eating advil.

And one of my cats has flown the coop… No not gone off to college, though that would be great, he has gone to live with my girlfriend. I am slowly, moving my dudes to her place so they are not so alone all the time. And Alex needed dental surgery, so while he was under, I had him declawed. I don’t advocate declawing, and hadn’t gotten my guys declawed. However, Shelly has 2 cats, neither with claws and furiniature that doesn’t look like it came out of a frat house, so we decided to get my guys done. When Alex first came home I thought I had made a huge mistake and felt pretty awful about having it done, but a week later, he seems just as happy and healthy as ever. So in the end I am cool with it.

Well, it’s time to take the ice off my knees, so I will see you all later.

Happy catting!

I like postgres.

I really do. So after spending a pretty frustrating day trying to get a development environment in windows set up, I finally got to the part where I was going to put postgres on the machine.

Easy I thought. Those guys do good work. And they are cool enough to autogen you a password.

Um. wtf.

Getting into a box when virt-manager is sucking

Sometimes virt-manager (over a less than optimal link) will be unusable. Never fear, it’s simply creating an ssh tunnel, and then connecting you to vnc. No reason to let it have all the fun.

First find out what port the vnc server you want to talk to is listening on

root@vm.foo.com:~# virsh dumpxml dns | grep graph
Connecting to uri: qemu:///system

There we see it’s port 5904, now tunnel that port over ssh

ssh -L 5904:localhost:5904 root@vm.foo.com

Then open the vnc session in a viewer

npowell@malaise:~$ vncviewer localhost::5904

Cibatta bing. You in!

I guess I should blog

Hallo.

I haven’t said much as of late…but it’s because I have been busy…busy as a bee!

I am into so many things now it’s just sick, so many things in fact I don’t even have time to talk about them.

I bought a grill and have been hacking away at grilling recipes. While enjoying the fruits of this labor.

I have been chugging along with FreeBSD as my home server/Asterisk box and that’s been loads of fun.

I bought the book The Book of PF. What a great read. You can get most of the actual text here, but for text this long, I much prefer the book format.

In my career I have always managed to avoid networking, in favor of 10 million other things. What a mistake. PF is super interesting, as is networking in general.

I have made a bit of a truce with Java and have been hacking around a bit on that. I am sold on Ivy for dependency resolution. The power of code reuse via jars is awesome, maintaining the classpath is not…Ivy fixes that.

I still loathe xml.

Big changes on the personal front, as I am planning to start moving my cats to my girlfriends house. She already has 2 cats, so with my 3 along for the ride, we’ll have 5 cats under her roof. That should be interesting.

I am running about 20 miles a week. I continue to have heel pain, but I run anyway. The pain isn’t bad enough to change my gait, so I just run anyway. I might go to PT…we’ll see.

Ok, that’s enough for now.

« Previous PageNext Page »