Tuesday, 22 November 2011

Learning Clojure: Part 7

Victory!

Today I plotted my first Clojure Fractal.

It took a while. And I doubt that the quality of the code is anything to marvel at.

I have a complex number struct, three functions to perform basic operations on them (add, multiply & magnitude), two functions (one called from the other) to create a grid of complex numbers, one to create an iterator and three to piece it all together by passing all the entries in the grid to the iterator and printing the result line by line.

I'm pretty pleased with the result. Luckily I seem to have picked on an interesting fractal.
The formula is: z -> z2 + c
where c is 0.9 - 0.1i

Here's a better look at it after I tweaked some parameters:

Friday, 18 November 2011

All Code Is Evil

It's true. I know it's not always a comfortable truth, especially for those managers who like to measure achievement in lines of code. But it is true.

The best solution? Re-use existing code.
The most satisfying refactor? Less code than you started with.
The easiest bugs? Scoped in the least code.

All Code Is Evil. As someone paid to create it that isn't so good. Unless you're paid to be smart and only create as much new code as you need to to solve the problem at hand.

All Code Is Evil. The more I have dwelt on this simple premise, the more sense it has made. The more other coding advice seems to fit with it: You Aren't Going To Need It so don't write it because it's Evil! Don't Repeat Yourself that's doubly Evil! Test Driven Development you can't trust that Evil code to do what you think it does!

So remember: All Code Is Evil

Learning Clojure: Part 6

Well I'm about 1/6 of the way into Programming Clojure and liking what I'm reading.

Incidentally, "1/6" would naturally evaluate to a value of a special ratio type. Not something I'm aware of in any other language. It all seems to be a part of Clojure's determination to provide a numerical system that allows effortless correct results. Like automagically switching between Java's Integer and BigDecimal types as required.

On the downside, that precision may come at a cost: http://vimeo.com/11644722

Progress

I finally feel like I'm making some real progress. After a few days under the weather I've been playing at the REPL and writing functions. The syntax is looking much less alien. And I feel like I'm taking more in. I defined a complex number struct today. And a function to multiply them together. These will hopefully be useful in plotting fractals in due course.

Monday, 14 November 2011

Learning Clojure: Part 5

Programming Clojure arrived. So far its looking pretty good. A lot more structured, with better "as-we-are-going-along" explanations. It promises development of a real working program by the end of the book. And maybe even a useful one. We shall see.

Thursday, 10 November 2011

Learning Clojure: Part 4

I decided to buy a book on Clojure. I attempted to get a recommendation out of someone who had encouraged me to learn Clojure in the first place. He said he was working through Programming Clojure, but mostly wanted to re-recommend the language.

There still seems to be a lot to take in. There are a lot of basics. This is in no way helped by the fact that many of the fundaments can be written in two different ways. Why oh why did anyone think that this was a good idea? Perhaps later in my learning I will be glad of this, but for now it just makes learning twice as hard. (As a parallel aside, having two ideas in my head about where the keys should be on my keyboard also confuses things (and by things, I mean me!))

If I were to write a Clojure tutorial I would stick to one way until the reader had grasped the basics quite thoroughly. Until I do, I shall stick with the tutorial I have. And wait for my book to arrive. And charitably hope that what makes a tutorial a little overwhelming to begin with, makes it a better reference resource in the long run.

P.S. I am no longer referring to the Dvorak cheat sheet that I printed out to get going. And as I'm on a physical QWERTY keyboard, there's no temptation to look at what's printed on the keys. In fact I'd cite that as a major advantage. The downside is that it can take a while to hunt around for the more obscure keys. For example, it is quicker for me to type "Square Bracket" than "[".

Tuesday, 8 November 2011

Learning Clojure: Part 3

So I'm learning Closure.

But I've not yet written much myself. It would be nice to have some kind of runnable program. One that did a little more than say "Hello World". Something cool and a little geeky, like plotting fractals. For this I will need more than the tools I already have. I will need some way of drawing to the screen or a file for a start. I will also need to be able to write my script to a file so I can test and amend it as I learn. But I have now got the hang of that.


Monday, 7 November 2011

Learning Clojure: Part 2

Well I feel like I've read quite a lot today.

To be fair, I was warned:  http://java.ociweb.com/mark/clojure/article.html#Collections. I don't know how much about Collections I have taken in, but I think I'm getting a feel for them. I suspect its going to turn out to be quite a powerful language, with a lot of potential for combining methods/functions. However the syntax is still looking quite alien.

I've also learnt a bit about scoping, and the difference between a "let" and a "binding". I still feel a little like I'm swimming in the deep end. I like the tutorial I'm following. But it isn't quite perfect. If I think of a way to improve it then maybe I'll email the author. Or perhaps the document I'm after is radically different in form?

Friday, 4 November 2011

Learning Clojure: Part 1

So I downloaded the REPL (read-eval-print-loop) from http://clojure.org/getting_started. And amazingly it all worked as described. The two examples on the page work straight away, even the one that calls a Java Swing method (which is reassuring) when suddenly I realise that I haven't got a clue what I'm doing.

I have no prior knowledge of Lisp or any other functional language. The syntax is quite alien. I have no idea what to type.

Time to learn.

A couple of clicks later and I'm here: http://java.ociweb.com/mark/clojure/article.html, learning that Clojure is “less noisy” than Java both when defining and calling functions. I'm learning that it has “prefix notation” - making sense of the examples I just ran. And I'm learning that the article I'm reading seems to be aimed squarely at Java programmers. Which is nice. (And I'm making some progress with the Dvorak key layout)

Thursday, 3 November 2011

Learning Clojure: Part 0

I have decided to learn Clojure. And I have decided to Blog about it. Using a Dvorak key layout. (Should I be doing something beginning with 'A' as well?) “Why?” you might ask. Well, I've got a little time off work between jobs, so I thought I might as well do something useful, and fun. Yes, definitely something fun. And what could be more fun than a new programming language?


Where am I coming from?

I'm a Java programmer. I have four years professional experience. But I've been programming in various languages since I was about ten. Anyway, its about time I learnt a functional language. And people have been recommending Clojure to me. So I'm going to give it a go. And Blog about it. On a Dvorak key layout.