Logo

dev-resources.site

for different kinds of informations.

Clojure REPL-Driven Development with VS Code

Published at
12/2/2024
Categories
clojure
functional
Author
dumplingbyte
Categories
2 categories in total
clojure
open
functional
open
Author
12 person written this
dumplingbyte
open
Clojure REPL-Driven Development with VS Code

In Software Development, fast feedback loops are essential to validating that code does what we intend. One of my favorite things about the Clojure ecosystem is the emphasis on REPL-driven development (Read Eval Print Loop).

This article explains at a basic level, how the REPL server communicates with the text editor and how to set up a Clojure REPL with VS Code.

Exploring the REPL

There are many ways to REPL, but this article focuses on exploring Clojure REPL with Leiningen, VS Code and the Calva extension.

What powers the REPL? The Calva extension uses nREPL under the hood, nREPL being a REPL server/client API for evaluating code remotely at run time. See more about it at nREPL.

What is REPL useful for?

The REPL is useful for getting fast feedback on code by evaluating code blocks and getting the response printed on a terminal session or output file. This allows one to see the expected return values and experiment with different types of inputs. We can initiate REPL sessions with any environment variable, we can mock code dependencies at run time, etc.

Set Up

  • Make sure Leiningen installed on your environment. If this is your first time installing Clojure, Leiningen will also install Clojure for you.
  • Clojure.
  • VS Code.
  • The VS Code Calva Extension

Create a Demo App

Use the lein command to create a new Clojure project. This command creates a new project with the necessary configurations to run Clojure with Leiningen:

lein new app my-app-name

Once the command runs, you will see the following file structure:

.
β”œβ”€β”€ CHANGELOG.md
β”œβ”€β”€ doc
β”‚   └── intro.md
β”œβ”€β”€ LICENSE
β”œβ”€β”€ project.clj
β”œβ”€β”€ README.md
β”œβ”€β”€ resources
β”œβ”€β”€ src
β”‚   └── repl_demo
β”‚       └── core.clj
└── test
    └── repl_demo
        └── core_test.clj
Enter fullscreen mode Exit fullscreen mode

The REPL Workflow

The typical REPL workflow looks like this:

  • Start the REPL session
  • Evaluate a Clojure form
  • See the output

Start the REPL

The demo project we created prints a string to standard output when we run it with lein run. Let's see how we can run the main function with the REPL:

On you terminal, run lein repl. You should see something like the below text, indicating that the REPL session has started.

nREPL server started on port 42815 on host 127.0.0.1 - nrepl://127.0.0.1:42815
REPL-y 0.5.1, nREPL 1.0.0
Clojure 1.11.1
OpenJDK 64-Bit Server VM 17.0.8.1+1-Ubuntu-0ubuntu122.04
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")
  Source: (source function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
    Exit: Control+D or (exit) or (quit)
 Results: Stored in vars *1, *2, *3, an exception in *e

repl-demo.core=> 
Enter fullscreen mode Exit fullscreen mode

Env Vars

To start a REPL session with environment variables, simply declare them before running the command:
MY_ENV_VAR=this-is-cool lein repl

Calva REPL Actions

All of these have a corresponding shortcut. I’m not including this here, due to OS and key-binding differences.

Connect to a running REPL

Open the VS Code command palette and search for connect to a running repl server . On the next prompt select Leiningen.

Load Current File and Its Deps

From the command palette, search form load/evaluate current file. This will evaluate all the code and imports.

Evaluate a Code Block

From the command palette, search for evaluate top level form. This will evaluate the code block where your editor cursor is.

Sample Calva Code Evaluation

functional Article's
30 articles in total
Favicon
A monad is a monoid in the category of endofunctors...
Favicon
Rust-like Iteration in Lua
Favicon
Transducer: A powerful function composition pattern
Favicon
πŸ—οΈ `Is` Methods
Favicon
7.bet’s Bold Move: Play Smarter, Play Safer, Play Better!
Favicon
Harnessing the Power of Object-Oriented and Functional Programming Paradigms in Software Development
Favicon
Lambda vs. Named Functions: Choosing the Right Tool for the Job
Favicon
Object-Oriented vs Functional Programmingβ€”Why Not Both?
Favicon
From C# to Haskell and Back Again: My Journey into Functional Programming
Favicon
Comprehensive Guide to Automated Functional Testing
Favicon
Functional Programming in Go with IBM fp-go: Error Handling Made Explicit
Favicon
Razumevanje funkcija viΕ‘eg reda (Higher-Order Functions) u JavaScript-u
Favicon
What is Functional Programming, and How Can You Do It in JavaScript?
Favicon
Parallel Testing: Best Practice for Load Testing & Functional Testing
Favicon
For loops and comprehensions in Elixir - transforming imperative code
Favicon
Advent of Code and Aesthetics
Favicon
PureScript for Scala developers
Favicon
Clojure REPL-Driven Development with VS Code
Favicon
Combining Object-Oriented and Functional Programming in Large Projects
Favicon
Non-Functional Requirements: A Comprehensive Guide
Favicon
Unpacking Lambda Expressions: What They Are and Why They Matter
Favicon
Functional Programming: A Misfit for Backend Engineering
Favicon
Scope progression
Favicon
JavaScript Functions for Beginners: Quick Guide
Favicon
Tech Watch #2
Favicon
Either Algebraic Data Type
Favicon
Functional Programming in C#: The Practical Parts
Favicon
A 20-liner Drag'n'Drop feat using ObservableTypes
Favicon
On β€œsuperiority” of (functional) programming and other labels
Favicon
Top Open Source functional programming technologies

Featured ones: