Archive for April, 2009
Clojure Fractal Tree
The first time I implemented the classic fractal tree brings back good memories of messing around with AMOS and reading articles about it in Amiga Shopper. How times change. I had not thought about those trees for ages but for some unknown reason they seemed to surface in my mind as a fun bit of code to write in Clojure. Below is the source for it. If you arrive at this page via google please bear in mind I am currently learning Clojure so this code could contain non-idiomatic Clojure and other harmful stuff. Here’s the code
(import ‘(javax.swing JFrame JPanel )
(import '(javax.swing JFrame JPanel )
'(java.awt Color Graphics Graphics2D)
)
(def branch-angle 30.0)
(def max-depth 10)
(def init-lendth 80.0)
(run)
(defn draw-tree [g2d angle x y length depth]
(if (> depth 0)
(let [new-x (+ x (* -1 length (Math/sin (Math/toRadians angle))))
new-y (+ y (* -1 length (Math/cos (Math/toRadians angle))))
new-length (fn [] (* length (+ 0.75 (rand 0.1))))
new-angle (fn [op] (op angle branch-angle (- (rand 20) 10)))
]
(. g2d drawLine x y new-x new-y)
(draw-tree g2d (new-angle +) new-x new-y (new-length) (- depth 1))
(draw-tree g2d (new-angle -) new-x new-y (new-length) (- depth 1))
)))
(defn render [g w h ]
(doto g
(.setColor (Color/BLACK))
(.fillRect 0 0 w h)
(.setColor (Color/GREEN)))
(draw-tree g 0.0 320.0 400.0 init-lendth max-depth)
)
(defn create-panel []
"Create a panel with a customised render"
(proxy [JPanel] []
(paintComponent [g]
(proxy-super paintComponent g)
(render g (. this getWidth) (. this getHeight))
)))
(defn run []
(let [
frame (JFrame. "Fractal Tree")
panel (create-panel)
]
(doto frame
(.add panel)
(.setSize 640 400)
(.setVisible true)
)))
Swank/Slime/Emacs, adding things to your classpath
Just in case anyone is struggling in emacs to add jars/dirs to your classpath in Slime/Swank for clojure. You need to call
(setq swank-clojure-extra-classpaths (list “bla” “bla2″))
before you call
(require ’swank-clojure-autoload)
in you .emacs file.
Rather embarrassingly it took me quite a while to figure out my error. Also you need to restart slime to have this take effect as it only checks the values at start up. I just restart emacs.
Programming Clojure Book
I planned on purchasing this book in dead tree format. Alas it has been delayed until June and remains in beta until then, last night I went ahead and brought the pdf version. I have got a little way into the book and it reads exactly like all the other pragmatic programmer books, that is it is a nicely produced tutorial style book that is quite accessiable to programmers without much experience in the topic of the book. Sort of fits me perfectly. It is something like Beta version 9 and other than a few layout glitches I have not spotted any serious problems.
While the tutorials on line have been good, I felt the need for a slightly more in depth/complete text on Clojure. This book seems to provide it. I may still pick up the printed edition when released but
that all depends on whether the Clojure language has become ingrained in my mind by that point.
Clojure – A Little Bit Further Along The Curve.
Ok I am now at the stage where everything seems simple. It is stage I always seem to go through when learning a new language. The new things are still new but understandable and the old things looks simple, not enough code has been written to find the warts. I admit it is a nice stage of learning a new language it gives the feeling that anything is possible in the language.
Obviously that feeling is wrong but it is nice to have. A warning sign for me would be if I did get that feeling about a language at some point.
So what have I noted about Clojure so far.
- I do slightly miss static typing.
- Clojure seem quite concise
- Interactivity is still great
- Interop with Java is quite easy
- Clojure as a language feels small
Yep I miss static typing, all my life I have been taught to check your units and static typing has always been the equivalent, in my mind, for programming. It is sometimes to move away from the comfort zone.
Clojure does feel like a small langauge as in the core is easy to fit inside you head. This contrasts well with C++ and Scala both of which feel like large languages. I am undecided on whether big or small is better. There are so many different view points meaning no simple answer is to be found. In an attempt to not get side tracked I don’t intend to start to investigate the literature on that topic.
Where from here in the learning process. Well I need to start producing more code. I am not sure I have broken the 500 line mark of my own code, I keep refactoring as I learn better ways so a simple line count is not possible. Unit testing frameworks are still an unknown and learning more about using emacs/slime more efficiently with Clojure is certainly needed.
Thoughts on Starting to Learn Clojure
Following on from my previous post about trying out Clojure I have spend a few days looking at it. I quite like what I see.
I first watched a couple of videos by the creator of Clojure entitled Clojure for Java Programmers. They were quite long but interesting although lacking in hard details. Google for links, I am typing this offline at the moment.
YouTube has a set of 10 short videos that give a quick overview of Clojure and these are the set I hastily took down notes on the language as I watched them. I then forced myself to type them up in order to further absorb the information. I figured if I am going to take a look at a new language I may as well really study and learn the language.
Why? You ask. Well Clojure is a Dynamically typed language and that means I loose my safety net of the compiler. I want to try and understand how people develop in Clojure effectively and learn the tricks.
Apparently Clojure is a modern form of Lisp. Other than knowing the brackets are in the wrong place in lisp, it has always been a bit of mystery to me. I have never taken the time to learn it as, like Haskell, it seems to be living in its own world. Perhaps this is partly why Clojure appealed a chance to learn Lisp without feeling like I turned left when everyone else turned right.
That needs last bit needs some explanation. I like the java libraries and I like being able to deploy apps with webstart and as applets. I like being able to compile my app on window and have it run on Linux and perhaps macs. Yep I have been brainwashed by Java marketing and modern ideas… I know things never quite work correctly in the real world but I like aspiring to an ideal! This is why I struggle to motivate myself to learn Haskell, I know what I have to give up. Ok hopefully you won’t flame me too bad for that.
So I have started to write some code in Clojure. Emacs and Slime provide a reasonable development environment (I am still learning emacs). The interactive nature is great. Altering a rendering function, evaluating it, then going to an already running app to have my alterations effect the app straight away is, well, excellent. Will this give a good productivity boost? Well it is too soon to say from direct experience but most people say it does.
Performance wise I have heard figures of as fast as Java to within a factor of three. I suspect it depends on the constructs and programming style you are using. From some simple test it seem pretty quick for a dynamic language. I recall the time I wrote an A-Star algorithm in python and saw a little game I was working on grind to a halt. It made the association in my mind that dynamic languages are slow. Clojure does not seem to be the case.
So I am continuing my investigation of Clojure. Other things on the list to do are re-read the Android book. This time taking some notes in the hope that some of the information sticks in my mind this time round. I seem to have an endless number of ideas for Android.