tisdag 24 april 2012

Leiningen on Windows/Cygwin

I've started to experiment with Noir which is a web framework for Clojure that seems really cool. In doing so I also made my first close encounter with Leiningen which seems really convenient.
Since I'm using Windows (strange huh?) I decide to run Leiningen through Cygwin (you also have the option of running a batch file but I haven't tried that yet). In doing so I found small problem with running Leiningen plugins.

Downloading and installing the plugin seemed to work as planned:

$ lein plugin install lein-noir 1.2.1
[INFO] Unable to find resource 'lein-noir:lein-noir:jar:1.2.1' in repository central (http://repo1.maven.org/maven2)
Copying 1 file to C:\cygwin\tmp\lein-db2bc8b7-7c28-4c97-a2f1-71caaa04d00c\lib
Including lein-noir-1.2.1.jar
Including clojure-1.2.1.jar
Created lein-noir-1.2.1.jar


When trying to run the plugin it doesn't seem to be installed however:

$ lein noir new first-site
That's not a task. Use "lein help" to list all tasks.

Seems like the plugin isn't installed after all... It turns out that Leiningen, Cygwin and Windows do not seem have the same idea of where your home directory is located. All plugins should be installed under $HOME/.lein/plugins/.


What you need to do is locate where Leiningen stored your new plugin. For example by doing:
$ find . -name 'lein-noir-1.2.1.jar'

Once you know where it is you simply have to copy it to $HOME/.lein/plugins/:

$ cp <Location from previous step>/lein-noir-1.2.1.jar $HOME/.lein/plugins/



That's it! You should now be able to use you're newly acquired plugin.

$ lein noir new first-site
Creating noir project:  first-site
Creating new dirs at:  D:\Development\noir\first-site
Adding files...
Project created!


söndag 8 april 2012

Project Euler


So, how are things going with my Clojure? Pretty good I think.

I've started out writing solutions to some of the problems (about 10 so far) presented Project Euler (the site is currently down, that's how I got the time to log a post here. :-)). I've also done a Kata or two that I've previously done in other languages. There's also a new project taking shape in my head that I've started writing some code for but I'll write more about that some other time.

I think that there are a number of aspects on learning a new language through Project Euler (PE) that may be of interest.

Project Euler is not designed to teach you programming

As stated at the start page at project Euler:
Project Euler exists to encourage, challenge, and develop the skills and enjoyment of anyone with an interest in the fascinating world of mathematics.
The primary objective is hence (from my interpretation) to dig into the world of mathematics rather than into the world of programming. You simply happen to do some programming along the way to solve the problems presented to you. This is usually done with small snippets of code that (so far) haven't exceeded ten lines.

Since the form of the problem is always the same (create a function which calculates a number which is the answer to a question) and the domain is always mathematics (in particular discrete mathematics so far) there are many aspects of real world programming that are left out. Examples of such include I/O-handling, user interaction, database access and all other stuff that's not hardcore algorithm development. In my experience this caters for at least 90% of all programming time in my professional Telecom/ERPish life.

Furthermore no other feedback is given to you regarding other quality aspects of your solution apart from the correctness of the particular case that it is designed to solve. Unless you really focus on finding weak spots and smells in the code that you're producing your not likely to find them since the solutions are usually not complicated, or large enough, to make that smell stink.

The size of the solutions is also another problem if you were to use PE as your only source for programming experience. As I mentioned earlier I haven't yet seen solutions in any languages stretching more than 20 lines of code. This means that you never get close to features of object oriented languages (and Clojure) such as polymorphism which should be considered an essential feature. You usually get away with one or two loop constructs, knowing a list type and a bit of array juggling and are never really pushed to move beyond those features. One sign of this are the many (often short and elegant) solutions to the problems presented in x86 assembler by people. It shouldn't be possible to write a short and elegant solution to a programming problem using assembler if it's to push your limits as a high level programmer! ;-)

I think that the sizing problem is a bigger issue for someone trying to learn many aspects of a mainstream OO language such as Java rather than for someone trying to learn a functional language such as Lisp/Clojure. My impression is that design in the small (sub-function level) and design in the large (class and package composition) has more in common in functional languages than it does in the mainstream OO languages.

How you still can learn a lot of programming from Project Euler
Given my above findings you might think that I think PE sucks if you want to learn programming. On the contrary! I think it's great! You just have to take some things into consideration when using it as a tool for learning programming. These are a few things that have worked out really well for me so far.

Allow yourself to rush to the solution

I usually feel a bit stressed to find the solution to a problem once I've read and understood it. As soon as I've opened a new problem I&nbsp;immediately want to start throwing code at it to find the solution and get that big green check that you get when entering the correct solution.

The way I'm trying to cope with this is to force myself to take at least one or two minutes to contemplate on the mathematics behind the problem. After that i allow myself to start hacking away until I reach green knowing that I made it (with respect to the only criteria measured at PE).

But then stop, reflect and redo

Once I have a confirmed solution to a problem i force myself to go back and look it over to see if I can find a better (cleaner, faster, more idiomatic) or different (using a different technique) solution to the problem.

Once I've done that I usually hit the PE forum for the problem at hand to see what other persons have come up with (in other languages than Clojure). That's usually a good source of inspiration for another one or two solutions to the problem.

When that's is done I usually hit Clojure-Euler to browse other solutions to the problem that are written in Clojure. That's a great way to pick up some really nice idiomatic Clojure and learn a couple of new functions or macros that you're likely not to have heard of before.

Do more programming than PE

Do not use PE as your only source for exercises when learning a new language. Start a toy project that complements the things that you learn from PE.
An ideal such projects would not require a lot of algorithmic though, that you will get from PE for sure. Instead try to focus on other areas (I/O handling, user interaction, some multi-threading, etc.). A chat client/server is one example of a program that would fulfill a number of the above criterias.

Stop when you feel like

There will probably come a time when you discover that you learn more mathematics from the problems than programming. Then you should stop and consider if continued work on PE is really the best way forward.

Closing Notes

All in all doing PE problems have been a great experience so far. Every new problem is like opening a small present. I know that I still have a lot more to do (about 360 I think :-)) and that some of the points I'm trying to make above may be proven wrong once I move up the ladder. I'll write a new post when I reach 100 solved problems (if I ever)! :-)

onsdag 21 mars 2012

What's on my bedside and in my head

I'm always reading two or three development related books in parallel. I'm trying to keep the number down and aim at never working more than two books simultaneously. More than two will cause a tempo that's too slow and a momentum that's too low. That's usually what make me quit books. In a way I guess it's self adjusting but I usually want to finish the books I start.

Anyway...

Programming Pearls
I just finished reading the second edition of Programming Pearls by Jon Bentley. The first edition was written  1985 while this edition was written 1999. In other words, this is a real classic. :-)
The reason I got interested in the book was an anagram coding kata described by Dave Thomas. After doing the kata a few times with various approaches Goggled the problem to see what alternative solutions there were to the problem. I think that a post on Stack Overflow pointed me in the direction of this book which presents a complete and very elegant solution in less than 20 lines of C-code. In addition to being very elegant it's also very fast.

When reading that solution I felt I wanted to see more code like that and figured that there might be more of interest in the book. The book is focused on classic (but of course still very relevant) programming. By that I mean that it has a high focus on algorithms and data structures rather than program structures and and code organization (a.k.a Design Patterns et. al).
There are a lot of discussions covering various sorting and searching algorithms and suitable data structures therefor. There are also about a zillion references to Donald Knuths The Art of Computer Programming which is really dear to the author.
Other topics covered in the book include program verification, back of the envelope estimations and code tuning.
The verification parts were nice to read and (obviously) not fully aligned to today's TDD trends. Rather the author leans on setting up invariants describing facts that must hold at various places in the code to allow reasoning about the correctness of the code. To me this looks like something not too far away from what has been tried in formal program verification.
Back of the envelope estimations is a practice used far to seldomly when assessing if the system we've built (or are about to build) is up for the task it's meant for. The book gives some good estimates on which you can lean when doing these estimates as well as some good techniques and advises.

Overall the book was a nice read and there were some really nice pearls in there although I have to confess that I didn't find any other of them as nice as the anagram program.

Spring in Action
The other book I've been reading lately (and still am) is the third edition of Spring in Action by Craig Walls. I'm reading it more to try to grasp what the Enterprise Frameworks that are used in Java are all about rather than for Spring specific knowledge. I figured that since Spring is such a widespread and almighty framework an introduction to it would give me the introduction to the "Enterprise World" that I needed.

Even though I'm not a huge fan of these heavy duty frameworks with a ton of XML, annotations and even some byte code manipulation the book is a nice read. It serves my purposes quite well.
Spring is a topic far to wide for one book if you want to dig really deep but this introduction will show you the basics and allow you to attain the knowledge needed to search more information on you own.

What's next?
Now I will (finally!) allow myself to go Clojure in my reading but I'll tell you more about that in another post...

onsdag 22 februari 2012

(Re)Starting with Clojure

After attending JFokus last week I was inspired to alot of things. That's usually the way things go for me when I attend conferences.

One of the things I got really keen on doing was picking up Clojure again. I spent some time trying to pick up parts of the language about a year ago but not enough for it to really stick.

This time I went for the Eclipse plugin Counterclockwise. Eclipse is the IDE I currently use at work and I try to spend as much time in it as possible to get fluent. My heart tells me that I should use Emacs when programming LISP but Counterclockwise has worked out really well. It's quite minimal but functional. Just in line with LISP I guess.

After installing Counterclockwise and getting the REPLF running I started looking for test frameworks for Clojure. I'm a TDD fan and feel most at comfort when being able to write tests before writing the code to make them pass.

What I found (in addition to clojure-test which is what ships with the Clojure distribution) was Midje which seems to have some really nice features. I've just gotten it up and running, written my first tests and watching them both fail and succeed. I'll have to play around with it a bit more to get to know it better.

My plan now is to do a couple of katas to get my Clojure skills up to a level where I can actually produce a couple of lines code without having to Google each and every function and functionality.

I also bought The Joy of Clojure which was not available last time I spent time with Clojure. My expectations on the book are quite high as I've read some really positive comments on it.

Back to Clojure!



torsdag 16 februari 2012

Hi!

My plan for this blog is to write down my thoughts on various software issues and to document some of the software activities that I involve myself in.