<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: dumpling byte</title>
    <description>The latest articles on DEV Community by dumpling byte (@dumplingbyte).</description>
    <link>https://dev.to/dumplingbyte</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2508839%2F76304169-794b-4594-8828-39e96e06f58d.png</url>
      <title>DEV Community: dumpling byte</title>
      <link>https://dev.to/dumplingbyte</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dumplingbyte"/>
    <language>en</language>
    <item>
      <title>Clojure REPL-Driven Development with VS Code</title>
      <dc:creator>dumpling byte</dc:creator>
      <pubDate>Mon, 02 Dec 2024 02:45:42 +0000</pubDate>
      <link>https://dev.to/dumplingbyte/clojure-repl-driven-development-with-vs-code-133</link>
      <guid>https://dev.to/dumplingbyte/clojure-repl-driven-development-with-vs-code-133</guid>
      <description>&lt;p&gt;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).&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exploring the REPL
&lt;/h2&gt;

&lt;p&gt;There are many ways to REPL, but this article focuses on exploring Clojure REPL with &lt;a href="https://leiningen.org" rel="noopener noreferrer"&gt;Leiningen&lt;/a&gt;, VS Code and the &lt;a href="https://calva.io" rel="noopener noreferrer"&gt;Calva extension&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What powers the REPL? The Calva extension uses &lt;code&gt;nREPL&lt;/code&gt; under the hood, nREPL being a REPL server/client API for evaluating code remotely at run time. See more about it at &lt;a href="https://nrepl.org/nrepl/index.html" rel="noopener noreferrer"&gt;nREPL&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is REPL useful for?
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  Set Up
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Make sure &lt;a href="https://leiningen.org/" rel="noopener noreferrer"&gt;Leiningen&lt;/a&gt; installed on your environment. If this is your first time installing Clojure, Leiningen will also install Clojure for you.&lt;/li&gt;
&lt;li&gt;Clojure.&lt;/li&gt;
&lt;li&gt;VS Code.&lt;/li&gt;
&lt;li&gt;The VS Code &lt;a href="https://calva.io/" rel="noopener noreferrer"&gt;Calva Extension&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Create a Demo App
&lt;/h3&gt;

&lt;p&gt;Use the &lt;code&gt;lein&lt;/code&gt; command to create a new Clojure project. This command creates a new project with the necessary configurations to run Clojure with Leiningen:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;lein new app my-app-name&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once the command runs, you will see the following file structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── CHANGELOG.md
├── doc
│   └── intro.md
├── LICENSE
├── project.clj
├── README.md
├── resources
├── src
│   └── repl_demo
│       └── core.clj
└── test
    └── repl_demo
        └── core_test.clj
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The REPL Workflow
&lt;/h2&gt;

&lt;p&gt;The typical REPL workflow looks like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start the REPL session&lt;/li&gt;
&lt;li&gt;Evaluate a Clojure form&lt;/li&gt;
&lt;li&gt;See the output&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Start the REPL
&lt;/h2&gt;

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

&lt;p&gt;On you terminal, run &lt;code&gt;lein repl&lt;/code&gt;. You should see something like the below text, indicating that the REPL session has started.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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=&amp;gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Env Vars
&lt;/h3&gt;

&lt;p&gt;To start a REPL session with environment variables, simply declare them before running the command:&lt;br&gt;
&lt;code&gt;MY_ENV_VAR=this-is-cool lein repl&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Calva REPL Actions
&lt;/h2&gt;

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

&lt;h3&gt;
  
  
  Connect to a running REPL
&lt;/h3&gt;

&lt;p&gt;Open the VS Code command palette and search for &lt;code&gt;connect to a running repl server&lt;/code&gt; . On the next prompt select &lt;code&gt;Leiningen&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Load Current File and Its Deps
&lt;/h3&gt;

&lt;p&gt;From the command palette, search form &lt;code&gt;load/evaluate current file&lt;/code&gt;. This will evaluate all the code and imports.&lt;/p&gt;

&lt;h3&gt;
  
  
  Evaluate a Code Block
&lt;/h3&gt;

&lt;p&gt;From the command palette, search for &lt;code&gt;evaluate top level form&lt;/code&gt;. This will evaluate the code block where your editor cursor is.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwqkhfpti4jann2ya8776.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwqkhfpti4jann2ya8776.gif" alt="Sample Calva Code Evaluation" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>clojure</category>
      <category>functional</category>
    </item>
  </channel>
</rss>
