Including Libraries

Adding dependencies in Leinningen

Requiring namespaces

See the clojure docs for require. Here's an example where the namespace is given the alias r so that you can shorten the names.

(require [interview.prompts.rawlist :as r])

You can also refer to the full name at any time too:


There are many ways to use require, e.g. with refer if you don't want a prefix. Say you have 3 functions f1,f2,f3, then you can refer to them in these ways:

(require [interview.prompts.rawlist :refer [f1 f2] :as r)

Notice in third case, because it wasn't in the refer list, you have to use the r prefix.

For test folders, read the documentation for leiningen where it is explained, but basically boils down to:

+ /interview
|--+ src/
|--+ test/

and the sub-folder structure follows exactly the same pattern as the src dir.

You can add additional folders in project.clj using :source-paths and :test-paths keys in the main project macro. See the sample project for more information.

I recommend you absorb everything in the sample project above, and also read the leiningen tutorial.

Finally, when referring to functions in the ns declaration in other sources (e.g. in your tests) you use the same format, but using the :require form:

  (:require [interview.prompts.rawlist :as r]))


Elaborated from a stack exchange answer on the use of namespaces by Mark Fisher

Using Libraries

The use function also makes another libraries namespace accessible to the current one. However use includes everything from the other library, it also makes everything part of the same namespace (ie. as if the functions, macros, name bindngs, etc were written in your library).

Therefore the use function is usually avoided and require is used instead. However, it can be convenient to make use of the 'use' function within the REPL as a way to include your project within the user namespace. Being in the user namespace gives you access to the REPL tools (eg. doc, source, etc)


Lets make use of the use function to include a library called really-useful-code.core

(use 'really-useful-code.core)

results matching ""

    No results matching ""