How does Clojure determine how many arguments an anonymous function (created with the #... notation) expect?
user=> (#(identity [2]) 14)
java.lang.IllegalArgumentException: Wrong number of args (1) passed to: user$eval3745$fn (NO_SOURCE_FILE:0)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
#(println "Hello, world!")-> no arguments#(println (str "Hello, " % "!"))-> 1 argument (%is a synonym for%1)#(println (str %1 ", " %2 "!"))-> 2 argumentsand so on. Note that you do not have to use all
%ns, the number of arguments expected is defined by the highest n. So#(println (str "Hello, " %2))still expects two arguments.You can also use
%&to capture rest args as in(#(println "Hello" (apply str (interpose " and " %&))) "Jim" "John" "Jamey").From the Clojure docs: