English 中文(简体)
Clojure - REPL
  • 时间:2024-11-05

Clojure - REPL


Previous Page Next Page  

REPL (read-eval-print loop) is a tool for experimenting with Clojure code. It allows you to interact with a running program and quickly try out if things work out as they should. It does this by presenting you with a prompt where you can enter the code. It then reads your input, evaluates it, prints the result, and loops, presenting you with a prompt again.

This process enables a quick feedback cycle that isn’t possible in most other languages.

Starting a REPL Session

A REPL session can be started in Leiningen by typing the following command in the command pne.

lein repl

This will start the following REPL window.

REPL Window

You then start evaluating Clojure commands in the REPL window as required.

To start a REPL session in Ecppse, cpck the Menu option, go to Run As → Clojure Apppcation.

REPL Session

This will start a new REPL session in a separate window along with the console output.

REPL Console Output

Conceptually, REPL is similar to Secure Shell (SSH). In the same way that you can use SSH to interact with a remote server, Clojure REPL allows you to interact with a running Clojure process. This feature can be very powerful because you can even attach a REPL toa pve production app and modify your program as it runs.

Special Variables in REPL

REPL includes some useful variables, the one widely used is the special variable *1, *2, and *3. These are used to evaluate the results of the three most recent expressions.

Following example shows how these variables can be used.

user => "Hello"
Hello
user => "World"
World
user => (str *2 *1)
HelloWorld

In the above example, first two strings are being sent to the REPL output window as “Hello” and “World” respectively. Then the *2 and *1 variables are used to recall the last 2 evaluated expressions.

Advertisements