Monday 8 December 2008

Passing functions around in Clojure

fn is the Clojure equivalent of lambda from Lisp.


(fn [x] (+ x 1)) ; #
((fn [x] (+ x 1)) 7) ; 8


These work as you'd expect when you pass them to functions such as map


(map (fn [x] (+ x 1)) (list 1 2 3 4)) ; (2 3 4 5)
(defn adder [x] (+ x 1)) ; #'user/adder
(map #'adder (list 1 2 3 4) ; (2 3 4 5)


Sharp quote (#') works same as it does in Lisp it appears.

No comments:

Post a Comment