Skip to content

Coding in the REPLλ︎

Clojure code can be typed into the REPL directly and the result instantly returned. Code can also be loaded from a project source code files, to run pre-written code.

Clojure Editors are the main tool for writing code

An editor connected to a Clojure REPL and evaluating from source code files is the most effective way for writing Clojure code.

Evaluating code in an editor automatically uses the correct namespace, avoiding the need to change namespaces or fully qualify function calls. Evaluation results can be shown in-line, as comments next to the code or in a data inspector.

Editors provide structural editing and Clojure syntax checking, along with general editor features. and

Using the REPLλ︎

Use the clojure command to start a REPL with Rebel, or the clj wrapper with the Clojure CLI REPL (requires rlwrap binary).

Start a Clojure REPL with Rebel terminal UI which also starts an nREPL server which a Clojure editor can connect too.

clojure -M:repl/rebel

A REPL prompt displays ready to evaluate a Clojure expression.

Clojure REPL rebel readline Clojure REPL rebel readline

Start a Clojure REPL with a basic UI which also starts an nREPL server which a Clojure editor can connect too.

clj -M:repl/basic

The clj wrapper requires rlwrap binary.

A REPL prompt displays ready to evaluate a Clojure expression.

Project dependencies automatically downloaded on REPL start

When a REPL is started from the root of a Clojure project the project dependencies are automatically downloaded (unless previously downloaded to the local maven cache, .m2/) and project specific paths are added, e.g. src tree.

Use REPL with a Clojure project

A REPL can run without a Clojure project, however, libraries and code are simpler to manage within project source and configuration files.

REPL start stateλ︎

The Clojure REPL always starts in the user namespace.

During startup the the clojure.core functions are required (made available) in the user namespace, so (map inc [1 2 3]) can be called without specifying the clojure.core namespace in which those functions are defined.

If clojure.core were not required, then the expression would be (clojure.core/map clojure.core/inc [1 2 3])

Evaluating codeλ︎

Type Clojure code at the => user REPL prompt

Press Enter to evaluate the code and see the result.

Clojure REPL clj evaluate map function Clojure REPL clj evaluate map function

Up and Down navigate the REPL history, providing an efficient way to evaluate the same code many times.

In Rebel, typing part of function name shows matches available, Tab to cycle through the choices, Enter to select.

Clojure REPL rebel readline - example of autocompletion Clojure REPL rebel readline - example of autocompletion

Load code from fileλ︎

Clojure code is usually saved in files and each file has a namespace definition that matches the file path, using the ns function. The file src/practicalli/playground.clj has the namespace practicalli.playground

(ns practicalli.playground)

Requiring the namespace of a file will evaluate (load) the code from that file in the REPL.

(require 'practicalli.playground)

Functions defined in that namespace can be called using their fully qualified names. e.g. if the namespace contains a function called main, that function can be called using (practicalli.playground/main).

Change namespaces

Change the namespace to practicalli.playground to call functions defined in that namespace by their unqualified function name, eg. (main), rather than the fully qualified name, e.g. (practicalli.playground/main)

in-ns will change change the current namespace to the one specified as an argument.

(in-ns 'practicalli.playground)

Now the (main) function can be called without having to include the full namespace name.

Typically it is more efficient to stay in the user namespace and require all other namespaces required.

Reload code changesλ︎

The :reload option to require will load in any changes to a namespace that happened outside of the REPL, eg. using an editor to change the source code in the file.

(require 'practicalli.playground :reload)

Use the :verbose option when issues occur loading a particular namespace. As the namespace being required may also require other namespaces, multiple namespaces may be loaded from one require expression.

:verbose shows a full list of the namespaces being loaded.

(require 'practicalli.playground :reload :verbose)
Reload in Terminal REPL for unconnected editor

When using an editor that is not connected to the Clojure REPL, then reloading is an effective way of updating the code with all the changes saved in the file.

Close REPLλ︎

:repl/quit at the REPL prompt will end the REPL session and all code not saved to a file will be lost.

Ctrl+c if the repl process does not return to the shell prompt.

Next stepsλ︎

Managing Library dependencies in REPL