Welcome back to the 2nd day of Devember. Today I finished up going through the exercises in Chapter 3 of Clojure for the Brave and True. The topic that I struggled the most with in this section was the bit on building a symmetrical hobbit. This section did introduce some new, interesting concepts, but I feel that the example used was overly complex and in trying to use one example to teach many new concepts, the author over-reached a bit.

Interesting new features learned out of this section included some pretty easy to use regular expression syntax, loop which allows you to simplify some of the recursive structures it seems I’ll be using a lot, and the reduce function*. The map and reduce functions remind me a lot of using Linq in the .Net environment in that it allows you to concisely apply some function across a collection without dealing with the ceremonies of for/foreach code.

After finishing up the examples and excercises in Chapter 3, I felt like I had enough of a foundation to go through some of the tests in the clojure-koans project. My latest updates are committed and pushed to my GitHub repository. clojure-koans is a set of Clojure files with tests that need to be filled in somehow to make the tests pass. For example, one of the tests from the 02-strings.clf file starts out looking like this:

"What if you only wanted to get part of a string?"

(= "World" (subs "Hello World" __ __))

In order to get the test to pass, I changed the code to:

"What if you only wanted to get part of a string?"

(= "World" (subs "Hello World" 6 11))

I am using the leininger tools to run my code and at first I was a little annoyed at the way it was working. I was changing the code, then running the tests. It would tell me I failed an assertion and seemed like it locked up with no prompt or response. I used Ctrl-C to exit, then made changes and started the loop over again. Run, fail, Ctrl-C, edit code, rinse, repeat. I was a little annoyed at this behavior. Then, one time I just left leininger running as I was changing the code and noticed that as soon as I saved my Clojure file, it reran the tests! Awesome! It reminded me of using NCrunch in Visual Studio where you get that immediate red to green/failure to success feedback.

The only thing out of the first 4 koans I did that made me stumble was the way the conj function works differently on a list versus a vector. If you conjoin something into a list, it adds it at the front, whereas if you conjoin something onto a vector, it adds it at the end. It makes me wonder if the list is backed by a linked-list where the vector is backed by an array. It would make sense then as it would be much faster to just make the new element the head of the list than it would be to walk through the linked-list to get it onto the end.

I think I’ll spend some time tomorrow looking more into all of the capabilities of the leininger tooling and continuing into the clojure-koans exercises.

* Originally here I had used the term keyword. Yenda1 pointed out my incorrect usage here. The term keyword has a special meaning in Clojure, so I have changed my post to use the correct term, function.