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.

Labels:


Comments: Post a Comment



Links to this post:

Create a Link



<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]