I have a dictionary of words stored in a vector in one ns (ns dictionary.core) and I want to have access the vector in another namespace, for example, (ns clojure-project.core) How should I do it? I have been researching the concept of namespaces for some time and I am still confused as to how I can "import" variables defined in another file into my current project.
1 Answer
(ns clojure-project.core
(:require [dictionary.core :as dict]))
(defn choose-a-word []
(rand-nth (dict/words)))
Namespace declarations have many options, so it can be very confusing! Limiting yourself to the above form is in my opinion good style.
9 Comments
Mahmud Adam
I keep getting this error: java.io.FileNotFoundException: Could not locate dictionary/core__init.class or dictionary/core.clj on classpath. This is what my namespace looks like: (ns akarify.core (:require [clojure.string :as str]) (:require [dictionary.core :as dict]))
Timothy Pratley
Is there a file under src/dictionary/core.clj? Happy to take a look at a github repo if that helps?
Mahmud Adam
github.com/p-adams/dictionary It is a little disorganized as I just created the repo. core.clj and the corpus file are the files for my project.
Chris Murphy
Seems a bit confusing. Do you want to use
akarify or dictionary as package names? Stick to just one. Then in the package you can have core and corpus. So you might end up with dictionary.core requiring dictionary.corpus.Timothy Pratley
Hi Mahmud, You need your .clj files to be in a src directory. Also the
dictionary.core namespace must be in a file under src/dictionary/core.cljs (not in corups like it is now). |