I started another tutorial on Purely Functional. One thing I learned is you should put a “:main” option in your lein project.clj.
When you start a Clojure REPL, by default you will go into the “user” namespace. From here you could call “require” on any namespace in your project.clj or any namespace that comes with Clojure, and use their functions. But to go to another namespace and have access to everything, you need to specify a “:main” option.
One of his lessons is about using Clojure to work with JSON, using a repo called AnApiOfIceAndFire, after the book series “Game Of Boobs” is based on. His project is a-game-of-json. He puts the JSON into some maps in the a-game-of-json.core namespace using the core Clojure function defonce. I went into that namespace, and I was unable to access that map. I couldn’t even call core functions like “+”.
Here is an example session:
REPL-y 0.3.7, nREPL 0.2.13 Clojure 1.8.0 Java HotSpot(TM) 64-Bit Server VM 9.0.1+11 Docs: (doc function-name-here) (find-doc "part-of-name-here") Source: (source function-name-here) Javadoc: (javadoc java-object-or-class-here) Exit: Control+D or (exit) or (quit) Results: Stored in vars *1, *2, *3, an exception in *e user=> (+ 2 3) 5 user=> (in-ns 'a-game-of-json.core) #namespace[a-game-of-json.core] a-game-of-json.core=> (+ 2 3) CompilerException java.lang.RuntimeException: Unable to resolve symbol: + in this context, compiling:(/tmp/form-init15513125615002385176.clj:1:1) a-game-of-json.core=> (exit) Bye for now!
This was not a problem in the “Introduction To Clojure” lesson, so I looked at that repo’s project.clj.
Here are the last two items in the project.clj file’s map:
:repl-options {:init (do (use 'bakery.core) (use 'introduction-to-clojure.core)) :skip-default-init true} :main introduction-to-clojure.core
After some editing, I figured out that the way to get access to all the functions is to add the “:main” key/value pair. After that, everything seemed fine.
I did this with twitter-retriever, and I was able to call core functions in all the namespaces I defined. I was even able to access clj-time.local, and call a few core functions in that namespace. I used “in-ns” to make a new namespace, but I was not able to call any of the core functions. Maybe my config is a bit off.
But “user” was available to me without any problems.
You’re welcome.
Image from “Evangelia quattuor [Évangiles de Saint-Médard de Soissons] (1v-221v). Capitulare evangeliorum (223r-235v)”, a 9th century manuscript housed at the Bibliothèque nationale de France. Source gallica.bnf.fr / BnF; image assumed allowed under Fair Use.