Using your code file from within REPL, classpath, deps.edn, help doc

I am brand new to Clojure, but not programming…

I couldn’t quite see how to load my code file into the REPL.
I expect there are multiple ways to do it, but I found one solution:

In the deps.edn file, add an entry to the top level map such as:

  :paths ["src" :src]

Then from your project directory (e.g., .../Exercism/clojure/annalyns-infiltration/) you start clj and then issue:

(load "annalyns_infiltration")  ; NB: underscore ('_')
(ns annalyns-infiltration)  ; NB: dash ('-')
can-spy?
#object[annalyns_infiltration$can_spy_QMARK_ 0x6a10b263 "annalyns_infiltration$can_spy_QMARK_@6a10b263"]

I sort of had to hack my way through this by trial and error.

It would be nice if:

  • the :paths value were standard boilerplate in deps.edn
  • the help file had some assistance about how to use your file from the REPL for beginners
  • there were some sort of standard defn for a function to view the names that were loaded from a file?

Between online books, the Clojure site, Google search, I think I can deal with most of the core language issue, but environment setup and usage can be rather tricky.

If you know a better way to go about loading and reloading a source file from within the REPL, please clue me in! ;)

Thanks,
-ej

Hey @ejohnso9. Welcome!

I’m guessing the reason there are no instructions on using the REPL from the command line is that beginners aren’t really expected to do so when developing locally. The recommended approach is to use an editor or IDE with a built-in REPL plugin or extension. Two free options are:

  1. Calva for VS Code
  2. Cursive for IntelliJ

That said, here’s a typically workflow if you insist on not using the above tools:

  1. Switch to project root directory and start the REPL with clj

  2. Load the file with (load-file "src/annalyns_infiltration.clj")

    Alternatively, you can require the namespace with (require 'annalyns-infiltration).

    Check that the namespace is loaded with (find-ns 'annalyns-infiltration). You should see something like #object[clojure.lang.Namespace 0x77cb9cd1 "annalyns-infiltration"]

  3. Write function can-fast-attack? in the editor, then reload the changes in the REPL:

    (require 'annalyns-infiltration :reload)

  4. Check if the function is available:

    (ns-resolve 'annalyns-infiltration 'can-fast-attack?)

    This should return #'annalyns-infiltration/can-fast-attack?

  5. Call the function:

    (annalyns-infiltration/can-fast-attack? true)

  6. See namespace contents with:

    (dir annalyns-infiltration)

That’s pretty much it. Please let me know If there are any errors in the above instructions.

Thank you for the reply, and thank you for the welcome.

Yeah, your instructions seem perfectly valid - I just don’t know some of the methods like (load-file "some/file/path/file.clj") yet. ;)

But I can see that works (and without any changes to deps.edn).

Sophisticated IDEs can be very nice, but when I am first learning a language, I just want to see simple stuff work in a simple environment: put some sort of source in a text file, then compile it or run it somehow (from within an REPL or otherwise).

“Clojure for the Brave and True” opens with an entire chapter on the wonders of EMACS. I wanted to learn and use EMACS on work systems 25 years ago, but getting sysadmins to install it was a major hassle. So, I’ve been using vi/vim for 30+ years.

I have used PyCharm and IntelliJ (basically same product), as well as VSCode a little bit. I may look into the other products you mention a bit later. ;)

Again, thanks for your reply ;)
-ej