In the accepted answer to another question, http://stackoverflow.com/questions/3997910/setting-clojure-constants-at-runtime the clojure function constantly
is used.
The definition of constantly
looks like so:
(defn constantly
"Returns a function that takes any number of arguments and returns x."
{:added "1.0"}
[x] (fn [& args] x))
The doc string says what it does but not why one would use it.
In the answer given in the previous question constantly is used as follows:
(declare version)
(defn -main
[& args]
(alter-var-root #'version (constantly (-> ...)))
(do-stuff))
So the function returned by constantly is directly evaluated for its result. I am confused as to how this is useful. I am probably not understanding how x
would be evaluated with and without being wrapped in `constantly'.
When should I use constantly
and why is it necessary?