Monday, January 11, 2010

clojure.core/str makes sense now

clojure.core/str makes sense

While reading excellent Programming Clojure by Stuart Halloway, instead just reading I started looking into how things are done by Clojure itself.
Yesterday it took me some time before I understood how things are done by clojure.core/str. This post is documentation of my findings.
Following modification of said function was very helpful in understanding how it works:
:tag metadata key is a symbol naming a class or a Class object that indicates the Java type of the object in the var, or its return value if the object is a fn..
Next are three parameters body pairs.
([] "")
No arguments (arity 0) will result in "" (empty string)

([#^Object x]
(if (nil? x) "" (. x (toString))))
One argument call (arity 1) will call java.lang.Object.toString method.
#^Object is a Type Hint.
if should not need much explanation.

([x & ys]
((fn [#^StringBuilder sb more]
(if more
(recur (. sb (append (str (first more)))) (next more))
(str sb)))
(new StringBuilder #^String (str x)) ys)))
This is where it starts to be interesting.
First unnamed function is defined that accepts StringBuilder sb and more params.
If more is true, that is not nil or false than call recursively self with new parameters
  • sb with appended result of calling str with first element of more,
  • more with elements after first.
If more is false, that in our context will happen only when more is nil, call sb's toString.
Next just defined function is getting called with new StringBuilder constructed from x as sb and ys as more.

It took me some time to realize that ys are turning into more.

So there you have it clojure.core/str explained.

Saturday, December 26, 2009

Clojure Start Again

Clojure start again

It's my other attempt to learn Clojure.
The landscape has changed a lot since last time I took a look.

Toolbox

Some tools that I used to play comfortably

Building and dependency tracking

Most popular is unfortunately Ant in building Clojure. I'm not looking forward to editing build.xml files again.
But there are two alternatives that keep track of dependencies, and do packaging.

Leiningen - build tool

Leiningen is a build tool for Clojure designed to not set your hair on fire.
That certainly sounds nice. Installation is straightforward as well, following Zefs instructions does the trick.
It boils down to this:
cd ~/bin
wget http://github.com/technomancy/leiningen/raw/stable/bin/lein
chmod +x lein
lein self-install
Leiningen is wrapping up Maven2 in nice Clojure syntax and gives you by default access not only to Maven Central Repository but also clojars.org.

clojure-maven-plugin - build tool

Another option is clojure-maven-plugin Maven2 plug-in to help you out with Clojure.
Adding following snippet should get you started:
<plugins>
<plugin>
<groupId>com.theoryinpractise</groupId>
<artifactId>clojure-maven-plugin</artifactId>
<version>1.3</version>
</plugin>
</plugins>

Clojuresque Third option

Responses I received after publishing this post showed third option that I've missed.
It is used by ClojureQL and certainly has supporters. It lacks a bit of documentation but ClojureQL - Where are we (going)? describes process quite well.

IDE

There is quite some options here, following one is most popular amongst Clojure developers.

Emacs + SLIME

Well normally I use VIM, but after watching Dynamic Interactive Webdevelopment I took for a spin Emacs, well Aquaemacs.
Following two posts were very helpful in setting up nice environment:
After some problems that got solved by setting up swank-clojure-classpath my new IDE was up.
And it welcomes me with message: Connected. Your hacking starts... NOW!

Lets get learning Clojure.