Returns the data type of an fn literal

Following is the expression -

((fn[n][(take n (range))]) 10)

      

While below error is thrown -

(#([(take % (range))]) 10)

      

Why can't I return the datatype from a function literal?

+3


source to share


2 answers


If you absolutely want to return "data" from an anonymous function using a read macro #

, just use do

.

#(do [1 2])

      



As @Mars says you also have an alternative to using the function vector

.

#(vector 1 2)

      

+3


source


Anonymous function macro #

expands into a form fn

eg.

#([1 2])

      

decomposes into (fn* [] ([1 2]))

as you can see, when it is called, the vector you are trying to return will be called as a function which will not work as no arguments will be provided. This is the same problem you have:



#([(take % (range))])

      

decomposes into

(fn* [x] ([(take x (range))]))

      

+4


source







All Articles