Ответ 1
#(println "Hello, world!")
→ нет аргументов
#(println (str "Hello, " % "!"))
→ 1 аргумент (%
является синонимом %1
)
#(println (str %1 ", " %2 "!"))
→ 2 аргумента
и т.д. Обратите внимание, что вам не нужно использовать все %n
s, количество ожидаемых аргументов определяется наивысшим n. Поэтому #(println (str "Hello, " %2))
все еще ожидает два аргумента.
Вы также можете использовать %&
для захвата аргументов покоя, как в
(#(println "Hello" (apply str (interpose " and " %&))) "Jim" "John" "Jamey")
.
Из Clojure docs:
Anonymous function literal (#())
#(...) => (fn [args] (...))
where args are determined by the presence of argument literals taking the
form %, %n or %&. % is a synonym for %1, %n designates the nth arg (1-based),
and %& designates a rest arg. This is not a replacement for fn - idiomatic
used would be for very short one-off mapping/filter fns and the like.
#() forms cannot be nested.