Archive for the ‘Clojure’ tag
Enclojure
I have a love hate relationship with emacs, for the past few months working with clojure and emacs has been fun. As my programs start to grow in size I feel the need for a more integrated IDE.
My eye turned to enclojure, it is certainly in early stages and it seems the selection of keyboard shortcuts seems a little bit wrong to say the least. They map to the ubuntu change screen shortcut and some even map to keyboard shortcut already defined in netbeans.
It is also quite buggy.
Having said that is does work and has potential. After one night use if seems, err, usable. I look forward to some updates to it. Over the next couple of weeks I shall use it as my default editor and see how it goes.
I did actually try to download and compile the code for the plugin, with perhaps an eye to seeing if I could fix some of the bugs. They don’t seem to have placed all the dependencies into their subversion repository and tonight I was not in the mood for manually downloading jar files. Come on guys make it easy for others to build your code.
It is 1986 all over again.
It occurred to me in the past week or so I did not have a feel for what lisp is capable of. I have been learning all the components of Clojure and trying to figuring out how they hang together. But I really want to “stand on the shoulders of giants” and gain this insight as rapidly as possible rather than gradually absorbing it over the years.
Clojure is a dialect of lisp and there is a set of classic videos from1986 to help out. These videos are basically the old MIT course that introduced programming to undergrads at MIT, it was replaced only a few years ago. The lectures where videoed when they were given it to a bunch of people at HP in 1986.
I particularly liked the symbolic differentiation lecture. This used to be an example program I would code when learning a new language. At the end of one lecture they had reached a point where I had often reached. The following lecture they explained that they could go further and create a pattern matching interpreter for trees and then just supply the rules for symbolic differentiation. This same interpreter could then be re-used for equation simplification and
other things. Great stuff.
I am just over halfway through and to be honest some parts are a little slow and this really due to the fact some of the ideas they are arguing for are now common place and they are really aimed as an introduction to programming.
I suppose because of this the focus is not on lisp in itself but to use lisp to express the ideas they are trying to convey as precisely as possible, I often find myself considering how the same ideas could be expressed in C++/Java and it seem in terms of code size and ease of implementation lisp is a winner. I don’t feel qualified to comment on how other high level languages compare with lisp.
In all there are well over 20 hours of lectures and assuming you know a little bit of a lisp like language there is plenty of insight to be gained from watching them. You can also find them on YouTube and Google Video.
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.