Sunday, 31 July 2011

Lazy Sequences

I like this a lot:
;; define a function with a side effect to show when it is called
(defn square [x]
  (do
    (println (str "Processing: " x))
    (* x x)))

;; define map-result which is result of calling square for every item in the list
(def map-result (map square '( 1 2 3 4 5 6 7 8)))
First time, enough of the lazy sequence is evaluated to generate the result:
practical-clojure.chpt5> (nth map-result 2)
Processing: 1
Processing: 2
Processing: 3
9
The next time, with the same args, the previous calculation was cached, so nothing to evaluate:
practical-clojure.chpt5> (nth map-result 2)
9
The next call it needs to evaluate a couple more of the lazy sequence:
practical-clojure.chpt5> (nth map-result 4)
Processing: 4
Processing: 5
25
And again, this is cached:
practical-clojure.chpt5> (nth map-result 4)
25
This is possible because a sequence looks like:
first --> (rest)
first --> (first --> (rest))
first --> (first --> (first --> rest))
Rest is only evaluated when it is required.

Auto Save a File in Emacs

Download http://www.litchie.net/programs/real-auto-save.html to .emacs.d, and then:

;; real auto-save - forces auto save mode
(require 'real-auto-save)
(add-hook 'text-mode-hook 'turn-on-real-auto-save)
(add-hook 'muse-mode-hook 'turn-on-real-auto-save)
(add-hook 'slime-mode-hook 'turn-on-real-auto-save)
(add-hook 'emacs-list-mode-hook 'turn-on-real-auto-save)
(setq real-auto-save-interval 5) ;; in seconds

Fixing Git Remote Reference

Did a 'git pull' and saw this:

Your configuration specifies to merge with the ref 'master'
from the remote, but no such ref was fetched.

A quick bit of googling turned up http://stackoverflow.com/questions/4297795/configure-git-so-that-git-pull-instead-of-git-pull-origin-master, which indicated I needed to edit my .git/config file to contain this:

[branch "master"]
        remote = origin


The remote = origin line was missing, therefore git didn't know where to pull from.

Saturday, 30 July 2011

Auto Complete in Clojure

This is awesome. My one concern about switching from Eclipse to Emacs for Clojure was losing the handy auto-complete stuff in Eclipse. I no longer have that concern:



Install the auto-complete mode (M-x package-install, auto-complete if using ELPA).

Download ac-slime from here: https://github.com/purcell/ac-slime

Here is the config required to set it up:

;; auto complete
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict")
(ac-config-default)
(setq ac-delay 0.5) ;; eclipse uses 500ms

;; configure auto complete to work in slime
(add-to-list 'load-path "~/.emacs.d/ac-slime")
(require 'ac-slime)
(add-hook 'slime-mode-hook 'set-up-slime-ac)
(add-hook 'slime-repl-mode-hook 'set-up-slime-ac)

Tuesday, 5 July 2011

Counterclockwise Eclipse Plugin

I'm going to run with Counterclockwise for now, since I'm already very comfortable with Eclipse. Having had a quick play with it this evening it seems to do everything I want, and will only get better.

The list of key bindings is defined at: http://code.google.com/p/counterclockwise/wiki/EditorKeyBindingsFeatures

Balanced brackets only seem to work in strict mode on Ubuntu.

Unfortunately Ubuntu 11.04 has some system key bindings which interfere with the Counterclockwise bindings:
  • Shift-Alt-Up: bound to Compiz scale windows. Install compizconfig-settings-manager, run "ccsm", and find Scale under Window Management, and disable the key binding. 
  • Ctrl-Alt-T: launches a terminal in Eclipse. Disable via System -> Preferences -> Keyboard Shortcuts

Sunday, 3 July 2011

Cljr

Well this is new: https://github.com/liebke/cljr. A very simple way to move from zero to a working Clojure REPL. All it took was:
wget http://incanter.org/downloads/cljr-installer.jar
java -jar cljr-installer.jar
export PATH=/home/neill/.cljr/bin:$PATH
cljr repl

Clojure Revisited

In the first half of 2010 I did a bit of programming in Clojure, using it to write a simple debug proxy for SOAP. Although I wrote many lines of Clojure code for that project, by the end I had the nagging feeling that I hadn't written idiomatic Clojure code.

And so, to stop that feeling from nagging, I am going to try to approach Clojure once again with beginners mind. My weapon of choice for this new assault is The Joy of Clojure. I have high hopes that this will help my understanding of the language to move up a level. I hope to be thinking in Clojure by the end of it. And I'm going to record the journey for posterity here.