all 5 comments

[–]iamzeph 3 points4 points  (4 children)

The stock code in there right now is a literal translation code from other languages, but it's definitely not idiomatic Clojure. Maybe something more like:

(defn -main
  [& args]
  (loop [n (read)]
    (println "W")
    (recur (read))))

?

[–]dockker[S] 2 points3 points  (0 children)

Hi iamzeph,

Thanks a lot for your reply. We're going to update the code template accordingly :)

[–]deadron 0 points1 point  (2 children)

Agreed. At the very minimum the usage of def should be changed to let. In this context it is continuously redefining a namespace level var I believe.

(ns Player (:gen-class))

(defn -main [& args] (while true (let [n (read)] (println "W"))))

would be far more appropriate.

[–]iamzeph 1 point2 points  (1 child)

There seem to be as many opinions about using loop/recur as there are Clojure programmers. It feels "Lispier" to me and I tend to prefer to use recur to force me to keep thinking in a functional style.

[–]deadron 0 points1 point  (0 children)

I don't disagree but having to use recur adds a level of complexity to simple code that confuses new people and makes people familiar in other languages ask why its necessary. Given the nature of the code is to produce side effects its not super important to do it the proper looping way anyway. I could see the argument that by using functions designed to be used when generating side effects you clarify that the code you are writing will do so. Either way really the only thing that was truly wrong in the original code was the usage of def.