I am new to Clojure and I am currently stuck with below code which throws a NullPointerException when I run it like this:
(mapset inc [1 1 2 2])
(defn mapset
[fn v]
(loop [[head & tail] v result-set (hash-set)]
(if (nil? head)
result-set)
(conj result-set (fn head))
(recur tail result-set)))
When I print the result-set in the if block it prints an empty set whereas I expect a set like this: #{2 3}
After trying to interpret the stacktrace I guess that the NullPointerException has something to do with the following line:
(conj result-set (fn head)).
The stacktrace and the fact that the resulting set is empty leads me to believe that the inc operation somehow gets called with nil as input.
I am glad about any explanation of why this error happens
The mentioned (shortend) stacktrace looks like this:
java.lang.NullPointerException
Numbers.java: 1013 clojure.lang.Numbers/ops
Numbers.java: 112 clojure.lang.Numbers/inc
core.clj: 908 clojure.core/inc
core.clj: 903 clojure.core/inc
REPL: 6 user/mapset
REPL: 1 user/mapset