December 01, 2012

Closures in Clojure in five minutes

I created the following for a coaching excercise. If you step through the code one part at a time you actually may have learned something about closures and Clojure.

If you do the exercise, I suggest running it in Light Table. This is an IDE being built around the concept of live editing. That is, the code you're writing is running is you're writing it. So you get to immediately see what you're doing. (I set up the exercise for Light Table 0.1; there is an 0.2 out now; not sure if everything still works as expected.)

;; Function application
(+ 3 4)
(- 10 20)

;; Function definition
(defn add [a b]
  (+ a b))

(add 3 45)

;; Local binding
(let [i 0]
  i)

(let [i 0]
  (add i 1))

;; Everything is a constant.
;; For mutable state we have to use references.
;; Following shows how to declare and dereference a reference.
(let [i (ref 0)]
  (add (deref i) 1))

;; Updating of reference values requires transactions.
;; 'dosync' evaluates its argument inside of a transaction.
;; 'alter' takes a function which does the updating.
(let [i (ref 0)]
  (dosync (alter i (fn [x] (add x 1))))
  (deref i))

;; Functions are also values...
(defn inc [i] (add i 1))

;; ... and so can be passed as arguments...
(let [i (ref 0)]
  (dosync (alter i inc))
  (deref i))

;; ... and returned as values.
(defn foo []
  (let [i (ref 0)]
    (fn []
      (dosync (alter i inc))
      (deref i))))

;; Global bindings.
(def f (foo))
(def g (foo))

;; Add these as needed/wanted...
(f)
(g)

;; Congrats! You just used closures in Clojure.

Bonus points if you understand what the following piece of code is actually doing!

(defn new-calculator []
  (let [i (ref 1)]
    (fn [msg]
      ;; 'cond' is an if-elsif kinda thing...
      (cond
        (= msg "inc")
          (fn [d]
         (dosync
              (alter i
                     (fn [x] (+ x d))))
         (deref i))
        (= msg "dec")
          (fn [d]
         (dosync
              (alter i
                     (fn [x] (- x d))))
         (deref i))
       ))))

(def o (new-calculator))

((o "inc") 4)
((o "dec") 1)

October 13, 2012

Six stages of debugging

Found this quote by Ergo^ at bash.org:
Six Stages of Debugging
1. That can't happen.
2. That doesn't happen on my machine.
3. That shouldn't happen.
4. Why does that happen?
5. Oh, I see.
6. How did that ever work?
 Very true to life. :-)

October 02, 2012

What I do all day (reprise)

I was lucky enough to get my paper, "Automated Architectural Reviews with Semmle", accepted at ICSM 2012, and got to present it last week in Italy. It's actually about a good part of my daily work. And it's just plain interesting. :-) So go read it already !

(If anyone has a good tip for where I might host the paper I'll upload it and link to to it from here. Or you can always just e-mail me and ask for a copy.)

August 26, 2012

Koopa WIP: Cobol Preprocessor

I just added an experimental Cobol preprocessor to Koopa. So far all it does is expand copy books. There is no support for replacements of tokens in copy books, or of any other preprocessing statements. Still, even this basic support was enough for Koopa to be able to parse another three of the five remaining test files which are part of the testsuite.

If you're interested in playing with this (or in expanding it) grab the source code from the project page and start with the Cobol85PreprocessingTest class.

August 17, 2012

Everything's a remix

Maybe you've seen this TED talk by Kirby Ferguson on how everything's a remix ? If you did, did you also spontaneously think of the following video ?


This is Gotye remixing remixes by other netizens of what was originally his own song. And as much as I loved the original there is something much more incredible about this remixed remixes version...

I have said it before and I'll say it again: the web knows how to bring out the best in people by connecting their passions.

August 16, 2012

Now Software geeks can have Dremels too!

If this article on Wired has got it right the Google's Dremel will be nothing short of amazing. Complex queries on petabytes of data with results in seconds ? Yes please!

August 06, 2012

Curiosity

This has got to be the most amazing picture I ever saw. It's Curiosity descending towards Mars, being slowed down by its parachute.
Congrats to the team for the successful landing!