Thursday, July 21, 2011

Learning clojure just in time

It looks like we've moved to clojure at just the right moment.

As mentioned, I started using emacs for the first time again in ages. While I still struggle remembering all the useful key combos I did get a little bit of help in the transition by installing aquamacs. It keeps buffers in tabs and lets you use cmd-c,x,v while you're trying to remember how to work the yank buffer.

I'm really liking clojure so far. I'm finally writing tests again (not sure I actually ever did for any of the other languages) and it's nice to have that inside emacs and also have the REPL to try things out.

So, Day 1 only had two excercises. I'm going to just point to aimee's entry on that, because we both came up with pretty similar code.

But let's look at the second exercise
Write a function called (collection-type col) that returns :list, :map, or :vector based on the type of collection col.
I got the following:

I wasn't quite happy with repeating the col argument for each condition. So I started to play around with using the various test methods (list? vector? map?) as keys in a map and then filtering the keys based on the collection argument given. I eventually gave up on it, because it is a rather silly approach. But it was an interesting opportunity to play around with the language.


Then I noticed multi-methods and became interested. Method dispatch based on the result of a function applied to the arguments of the method. This is pretty powerful and will probably take a while to get comfortable with. I used the rather simplistic approach of dispatching on the class to do the second exercise again.


This is silly, too. And it works pretty sloppily. The empty list creates an instance of PersistentList$EmptyList which doesn't seem to match PersistenList. But whatever, it's good enough and it got me thinking about multi-methods. There is more on those in this blog post.

And now it's on to day 2, where apparently we see the list comprehensions I was so impressed with in Erlang enter clojure, too.

Tuesday, July 19, 2011

Excuses, Excuses. Also, Emacs.

My, I've been a bit lazy, I guess. Has it really been that long?

Starting a new job and trying to find a new place to live has taken quite a bit of my time. So it was that during the Prolog week I started to struggle finding the time to do the examples. Then during the Scala week I didn't do much at all other than reading the chapter. But with Erlang I had some more time again and at least managed to do the day 1 and 2 stuff. With Clojure this week I hope to give it even more attention.

For all of it, I neglected writing about it. So to get this rolling again I'll do a quick rundown.

Prolog


Prolog is old and weird. The book compares it to Rain Man, which seems apt. Having never done any work with any logic languages this was very new to me. It was a very big shift in thinking. I appreciated that. It's one of the reasons to work through the book after all.

I think Prolog as a language is not very practical for me. But the chapter provided some really useful basics for understanding the syntax of Erlang and for pattern matching, which shows up in the Scala and Clojure chapters, too.

Scala


Scala was, as mentioned, somewhat neglected by me. Some of the syntax in the examples (especially for the actor stuff) seemed a bit weird to me and I really should actually work through them to get a feel for it. Minor inconsistencies in syntax are probably the main thing bugging me about what I read about Scala so far. And defining your default constructor inside the class definition while still having the option for actual init methods really rubbed me the wrong way.

Still, XML as a first class language construct looks very nice and getting around some of Java's noise is also charming. I reckon we'll cross paths again at some point and I can give it a closer look.

Erlang


Next came Erlang. And that was quite a bit of fun. Having been eased into the syntax by the Prolog chapter helped a great deal. I was quite fascinated by list comprehensions. I'm not sure if their power comes at the cost of maintainability. But it's hard for me to judge that given my lack of experience with the language and syntax.

I didn't even get to play with the actor stuff but I suppose people are happy with it and specifically with the Erlang implementation.

At some point I should really revisit Erlang. It's different enough to challenge the mind to take a new look at old habits. And it looks interesting in general with its focus on concurreny.

Clojure


I'm trying to dig a little deeper this week again. Clojure is interesting to me because for a long time I've wanted to try and make sense of a Lisp language.

In the process of reading up on it I noticed that a lot of people seem to prefer using emacs as an editor for clojure. So I installed that. (As a complete Mac noob let me point out that brew install emacs and brew install emacs --cocoa are two very different things. I have yet to learn why [cf. mac noob] but you probably want the latter.)

I had been using emacs (XEmacs, to be honest) for quite a while as my primary development environment. But that was some 7+ years ago and I never really touched it again. I'm incredibly amused that a lot of the key bindings are coming back to me anyway. And it feels kinda nice. Maybe I will rejoin the battle for the one true editor.

Sidenote: emacs installation notes for anyone who bothers. From someone who has no clue what he's doing:
  • brew install leiningen clojure clojure-contrib 
  • brew install emacs --cocoa
  • git clone https://github.com/technomancy/emacs-starter-kit.git ~/.emacs.d
  • alias emacs='/usr/local/Cellar/emacs/23.3/Emacs.app/Contents/MacOS/Emacs -nw'
  • lein new someproject
  • add :dev-dependencies [[swank-clojure "1.2.1"]] to the (def-project entry in someproject/project.clj
  • lein deps
  • lein swank
  • start emacs in another shell
  • (you may or may not need to install some elpa packages for slime and swank and paredit and whatever. See if it works with the just the stuff from the emacs-starter-kit or otherwise ask google. I did get annoying errors doing this and have no idea why it worked in the end. Sorry.)
  • M-x slime-connect
  • edit e.g. someproject/test/someproject/core.clj
  • execute tests with C-c C-,
  • you can also do other things with the REPL inside emacs but you best google for that
I hope that can be of help.
Back to the study group...

It seems like most of us have been struggling keeping up with the pace of the book, with everyone taking the odd week off from working through all the examples. But we were lucky that it seems to have mostly been different weeks, so there was always at least one person who could explain more details about a given language. So while I was very lazy there have been a lot of really interesting posts from others in the group.

We've also continued our weekly chats which are always a source of inspiration for me. As of recently we're using the Hangout feature of Google+ and it's really good. Since Skype keeps crashing my little Win7 netbook I'm happy to have found a better replacement.

Tl;dr

It's really nice to see how the chapters of the book build up on each other. I think the book has made me more adventurous to just play around in new languages. It is still nice to have this little virtual study group to help push me along. Technology is still awesome, too (git pushing from a train via tethering to the phone. Magic!) I reinstalled emacs after all those years.