For example, solving the following problem
http://projecteuler.net/problem=5
I came up with the following solution
(defn div [n] (= 0 (reduce + (map #(mod n %) (range 1 21)))))
(take 1 (filter #(= true (div %)) (range 20 1e11 20)))
Suppose for some golfing fun I wish to merge the first line as an anonymous function into the second line. Does the language support this?
Yes it does, but you cannot nest the
#()reader-macro forms, you have to use the(fn)form.For example:
does not work, because there’s no way to refer to the arguments of the outer anonymous functions. This is read as the outer function taking two arguments and the inner function taking zero arguments.
But you can write the same thing with
(fn...)s:You can also use the
#()form for one of the two anonymous functions, e.g:So you can inline your
divfunction like this (notice that we had to change the#()form passed tomapto a(fn)form):