Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous function returning 1 using #()

Tags:

clojure

Using defn or fn it's easy to create a function that taking one argument that ignores it and returns 1:

(defn ret1 [arg] 1)
(fn [arg] 1)

Is it possible to do this with the #() macro? I don't mean using something ugly or "cheating" like

#(/ % %)  or 
#(if (nil? %) 1 1)

I mean literally ignoring the parameter and returning 1. I can't find a clean syntax that works.

like image 700
Markc Avatar asked Feb 15 '11 15:02

Markc


2 Answers

#(do %& 1) ... but (constantly 1) is better.

like image 127
fogus Avatar answered Sep 30 '22 05:09

fogus


The #() syntax can't be used to create functions that have unused parameters in the way your description requires. This is a limitation of the #() reader macro.

I would recommend not using #() and instead just writing (constantly 1) which is a very brief way to create a function that ignores a parameter and instead always returns 1.

like image 26
drcode Avatar answered Sep 30 '22 06:09

drcode