Suppose files
is a list of Javas File
in Clojure and I want to obtain a list of file names. In the Leiningen REPL I can use, say,
(.getName (nth files 4))
=> "image.jpg"
but if I use
(map .getName files)
the REPL throws a CompilerException java.lang.RuntimeException: Unable to resolve symbol: .getName in this context
. Why is that?
I can work around this problem with
(defn gn [file] (.getName file))
(map gn files)
but I would like to now if there is a more elegant way to map Java methods to lists in Clojure.
You could either use memfn
(which some say you should not use, dont remember why, comments welcome) or simply a lambda:
(map (memfn getName) files) ;;#1
(map #(.getName %) files) ;;#2
as for why, well because these are methods and not functions.