Take the 2-minute tour ×

How could one write the identity function in clojure using anonymous function literal (#())?

The following code doesn't work:

(#(%) 5)

It raises an exception because it is converted to:

((fn[x] (x)) 5)

The problem in that when using #(), the function body is enveloped with parentheses. Any idea, how to elegantly overcome this?

share|improve this question
add comment

1 Answer

up vote 18 down vote accepted

Well, first of all, there is the identity function.

But you can use

#(do %)

if you insist.

share|improve this answer
Nice answer. If you know the parameter is a number, you can also do #(+ %) and save a character :-) –  mikera Feb 5 '12 at 14:29
add comment

Your Answer


