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!
This is a really cool post nate! I haven’t seen cloJure in action before seeing this.
It is however frustrating that every new language has a new weird syntax. I don’t really care about the syntax, I care about the speed. I think most Java programmers know what they don’t like about the language syntax so why not just improve on those points?!
Anyway I want to see rewrites after you finish the book! :{
The syntax is actually fairly old (it’s a Lisp after all), and there’s a reason it’s like that. Clojure programs are Clojure data structures. This means they can be manipulated at runtime using all the same tools you use to manipulate any other Clojure data structure. This allows syntactic abstract. For an example of why this is cool check out this presentation: http://www.slideshare.net/pcalcado/lisp-macros-in-20-minutes-featuring-clojure-presentation
Sorry this is a comment to an old post (to which I couldn’t comment) http://nathanpowell.org/blog/archives/585 .
Anyway, I think you have a typo:
array = [foo, bar, baz]
puts array[rand(a.size)]
should read:
array = [foo, bar, baz]
puts array[rand(array.size)]