Fork me on GitHub

so I have this file

(ns snake)

(defn -main [& args]
  (println "Hello World here are my args")
  (println (apply str args)))
called snake.clj and I tried to invoke it from the CLI like so clj -M -m snake snake.clj, but I get the error
Execution error (FileNotFoundException) at clojure.main/main (
Could not locate snake__init.class, snake.clj or snake.cljc on classpath.


The current directory is not no the classpath.


You can: • Change the classpath directly or via deps.edn • Put the file in the src directory (it's by default on the classpath) • Not use -main and instead load the file as a script

👍 1

Apparently, resources are not considered files when accessed inside uberjars? I have a few calls to (io/file (io/resource ...)) since one consuming function in a library I’m using takes a File object. This works fine when I run the code in the REPL, but it errors out when I try to run my project from an uberjar. I have found this search result which leads me to believe I should use io/input-stream instead of io/file when accessing an io/resource inside an uberjar: Can someone explain why there’s a difference when accessing resources in a REPL vs doing it via an uberjar? It seems a bit odd to me.

p-himik13:06:22 is something that represents a file on a file system. A JAR by itself is such a file. Its contents are not files on a file system though.

Alex Miller (Clojure team)13:06:30

Files are loaded from the file system. Resources are loaded from the classpath (which can be files but it's an open URL-based system - theoretically the resources could come from a network location, or a database, or even be generated on the fly)

👍 1

I see. I think I never really looked into what it means to be a Resource.


Does anyone have a guide on how to build an uberjar for an executable clojure file with a -main function using ?

Alex Miller (Clojure team)13:06:21

you might need some of the setup at the top of that too for deps.edn


Thanks that worked perfectly!


is it possible to import clojure.lang.PersistentQueue and give it a different name so I don't have to use the fully qualified name? I tried (:require [clojure.lang.PersistentQueue :as queue]) in my ns macro, but it complains Could not locate clojure/lang/PersistentQueue__init.class, clojure/lang/PersistentQueue.clj or clojure/lang/PersistentQueue.cljc on classpath.


slack=> (import 'clojure.lang.PersistentQueue)
slack=> PersistentQueue/EMPTY


i don’t believe you can rename it but importing means you use it by its Classname instead of FQN (fully qualified name)


ah, and I have to use import because it's a java class right?


import classes, require namespaces

Alex Miller (Clojure team)15:06:08

and technically, you never need to import, it is just a convenience to let you use unqualified class names in your code


even for classes from third party java libraries?


slack=> (java.time.LocalDateTime/now)
slack=> (import 'java.time.LocalDateTime)
slack=> (LocalDateTime/now)
all the same up to convenience

👍 1
Kris C16:06:17

Is it still ok to start a web project with Ring? So many options (luminus, pedestal, yada)...


It's still OK. And probably the preferred option when you're just starting.

😃 1

(ns snake.core
  (:import (clojure.lang PersistentQueue)
           (java.awt Color Graphics Dimension Font)
           (javax.swing JFrame JPanel)
           (java.awt.event KeyEvent KeyAdapter))

(def size 50)
(def scale 15)
(def start-length 5)
(def tick-speed 50) ; ms delay between each render loop
(def screen (Dimension. (* scale size) (* scale size)))
(def food-color Color/GREEN)
(def snake-color Color/WHITE)

(defn new-world []
  (let [head [40 25]
        body (take (dec start-length) (iterate (fn [[x y]] [(inc x) y]) head))]
       {:body  (apply conj PersistentQueue/EMPTY body)
        :head  head
        :dir   :w
        :size  3
        :dead? false}
     :food #{[50 32]}}))

(def next-point
  {:n [0 -1]
   :s [0 1]
   :e [1 0]
   :w [-1 0]})

(defn tick [{:keys [snake food] :as world}]
  (let [new-head (map + (next-point (:dir snake)) (:head snake))
        new-body (conj (.pop (:body snake)) (:head snake))]
    (-> world
        (assoc-in [:snake :head] new-head)
        (assoc-in [:snake :body] new-body))))

(defn draw-square [#^Graphics g [x y] c]
  (doto g
    (.setColor c)
    (.fillRect (* scale x) (* scale y) scale scale)
    (.setColor Color/BLACK)
    (.drawRect (* scale x) (* scale y) scale scale)))

(defn draw [#^Graphics g {:keys [snake food]}]
  (doto g
    (.setColor Color/BLACK)
    (.fillRect 0 0 (* scale size) (* scale size)))
  (doseq [snake (conj (:body snake) (:head snake))]
    (draw-square g snake snake-color)))

(def code->key
  {KeyEvent/VK_UP    :n
   KeyEvent/VK_DOWN  :s
   KeyEvent/VK_LEFT  :w
   KeyEvent/VK_RIGHT :e})

(defn draw-window []
  (let [world (atom (new-world))
        key-listener (proxy [KeyAdapter] []
                       (keyPressed [e]
                         (when-let [dir-key (code->key (.getKeyCode e))]
                           (swap! world #(assoc-in % [:snake :dir] dir-key)))))
        panel (doto (proxy [JPanel] [] (paint [#^Graphics g] (draw g @world)))
                  (.setMinimumSize screen)
                  (.setMaximumSize screen)
                  (.setPreferredSize screen))]
      (doto (JFrame. "Snaaaaaaaaaaake!")
          (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
          (.add panel)
          (.setResizable false)
          (.setLocationRelativeTo nil)
          (.setVisible true)
          (.addKeyListener key-listener))
      (loop []
        (swap! world tick)
        (.repaint panel)
        (Thread/sleep tick-speed))

(defn -main [& _]
So I have this snake game that uses java swing for the UI with a very basic render loop that uses thread sleep to manage how often I repaint the JPanel. However the animation isn't very smooth and so I was wondering if there is a way to make the animation smoother.

👍 1

draw intermediate frames


How could I do that?


so you draw things in frame A and you draw different things in frame B and that change is animation, but the change is too abrupt, so figure out something that is halfway between A and B, and draw it in the middle


A couple of other things to consider: • 50 ms means 20 FPS - quite low • If a single repaint and all associated computations take a lot of time, with your current code it'll be on top of those 50 ms, reducing FPS even further


the process of doing this will disentangle scene state and your data model, you'll likely end up ticking through your model generating some kind of scene graph and throwing that on a queue to be rendered whenever


Hi there, I was looking at and I was wondering if there is a way to use it with the clojure tools cli

Alex Miller (Clojure team)17:06:12

the real question is whether you can use it at all :)

Alex Miller (Clojure team)17:06:44

unfortuntely, autodoc has some glorious stuff wrapped in many layers of pain


oh ok thanks for the heads up 😄

Alex Miller (Clojure team)17:06:10

if you want to go spelunking, is the code that is used to build contrib api docs and it (kind of) uses the clojure cli (a very old version is vendored into that repo under cli/)

❤️ 1

@richiardiandrea if you aren’t already in love with autodoc, you might also consider, or or hosting your docs on


thank you, and yes I tried both of them yesterday and they were both having some issues (opened now against the repos) in our code base


@richiardiandrea I just replied on one of the issues. The other issue can easily be solved (source link)


Yep great thanks a lot for answering!


@richiardiandrea Implemented the source link template now