Clojure REPL Guide — Interactive Development and Workflow
In this tutorial, you will learn about Clojure REPL Guide. We cover key concepts, practical examples, and best practices to help you master this topic.
Clojure's REPL is central to the development workflow -- you connect to a running Process, evaluate code changes instantly, and see results without restarting, enabling a level of interactivity unmatched by most languages.
Starting the REPL
# Basic REPL
lein repl
# With project dependencies
lein repl :headless # for editor integration
;; REPL session
user=> (+ 1 2 3)
6
user=> (def x 42)
#'user/x
user=> x
42
user=> (println "Hello REPL!")
Hello REPL!
nil
REPL-Driven Development
;; In a REPL session:
;; 1. Define functions interactively
user=> (defn greet [name] (str "Hello, " name "!"))
#'user/greet
;; 2. Test immediately
user=> (greet "Clojure")
"Hello, Clojure!"
;; 3. Redefine on the fly
user=> (defn greet [name] (str "Hi, " name "!"))
#'user/greet
user=> (greet "Clojure")
"Hi, Clojure!"
Useful REPL Commands
;; Documentation
(doc map) ;; show doc for map
(source map) ;; show source for map
(find-doc "reduce") ;; search docs
;; Namespace introspection
(ns-publics 'clojure.string) ;; all public vars
(dir clojure.string) ;; list functions
;; System info
(System/getProperty "java.version")
(pr-str *clojure-version*)
REPL Workflow
;; Load file into REPL
(load-file "src/my_project/core.clj")
;; Require namespace
(require '[my-project.core :as core])
(core/my-function 42)
;; Reload changed code
(require '[my-project.core :as core] :reload)
;; Connect editor to REPL
;; CIDER (Emacs), Cursive (IntelliJ),
;; or Calva (VS Code) connect to running REPL
Common Mistakes
1. Not connecting editor to REPL
Typing at the terminal REPL is useful, but connecting your editor gives you function-level evaluation.
2. State leak between evaluations
Defining vars in the REPL pollutes the namespace. Use let for temporary bindings.
3. Loading stale code
Always reload namespaces with :reload when you change files: (require 'my.ns :reload).
FAQ
{{< faq question="Can I connect multiple clients to one REPL?" >}} Yes. Leiningen's nREPL server supports multiple concurrent connections. This is useful for pair programming. {{< /faq >}}
{{< faq question="What is nREPL?" >}} A networked REPL protocol that allows Clojure REPLs to be accessed remotely. Most editor plugins use nREPL to connect. {{< /faq >}}
{{< faq question="How do I exit the REPL?" >}} Ctrl+D or (exit) or (System/exit 0). {{< /faq >}}
What's Next
Now learn about Clojure's syntax and data structures.
| Topic | Description | Link |
|---|---|---|
| Syntax | Core syntax and forms | {{< ref "04-syntax" >}} |
| Sequences | Seq API and lazy seqs | {{< ref "05-sequences" >}} |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro