<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Clojure - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Clojure - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sun, 31 May 2026 05:05:48 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/clojure/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ An Animated Introduction to Clojure – Learn Clojure Programming Basics ]]>
                </title>
                <description>
                    <![CDATA[ This tutorial introduces the programming language, Clojure. Clojure is an awesome functional programming language that runs on Java's Virtual Machine (JVM). It is sometimes called a LISP, which is short for 'LISt Processing'. You'll need to have some... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/learn-clojure-programming-basics/</link>
                <guid isPermaLink="false">67f7044b8d28cad83fb095a2</guid>
                
                    <category>
                        <![CDATA[ Clojure ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Functional Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ jvm ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Mark Mahoney ]]>
                </dc:creator>
                <pubDate>Wed, 09 Apr 2025 23:35:39 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1744240649396/3e68ef22-3109-4b43-841e-e947f1821a76.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>This tutorial introduces the programming language, Clojure. <a target="_blank" href="https://clojure.org/">Clojure</a> is an awesome functional programming language that runs on Java's Virtual Machine (JVM). It is sometimes called a LISP, which is short for '<strong>LIS</strong>t <strong>P</strong>rocessing'.</p>
<p>You'll need to have some previous programming experience in another language to get the most out of this tutorial. Clojure is so different from most imperative programming languages that it doesn't really matter where you are coming from as long as you know the basics (variables, if statements, loops, and functions).</p>
<p>If you are looking for a complete beginner's guide to programming, you can find some of my other programming content below.</p>
<p>Here, I will cover functions, data structures, immutability, closures, tail recursion, lazy sequences, macros, and concurrency.</p>
<p>Clojure is a functional programming language. In it, the function is king and data is immutable. This may be a different paradigm than you are used to, but there are some compelling reasons to use it. Clojure's immutability is particularly good for programs that need to take advantage of modern hardware on laptops and mobile phones (multiple processors sharing a single memory).</p>
<p>Clojure runs on the <a target="_blank" href="https://en.wikipedia.org/wiki/Java_virtual_machine">JVM</a>. It can be difficult to set up Clojure because it involves installing Java, Leiningen, and sometimes an editor plugin. To make things easier, I recommend starting with a web-based IDE. There is no easier way to start programming in Clojure than using <a target="_blank" href="https://replit.com/">replit</a>. I recommend using it to write your first programs in Clojure. If you feel up to it, check out <a target="_blank" href="https://clojure.org/guides/install_clojure">these docs</a> to get started on your own machine.</p>
<p>Even if you don't use Clojure every day, learning it will change how you think about programming. It will help you understand recursion, higher-order functions, and data transformations in a new way. These ideas transfer well to other languages like JavaScript, Python, or Rust. In short, learning Clojure will make you a better programmer.</p>
<h3 id="heading-code-playbacks"><strong>Code Playbacks</strong></h3>
<p>This material will not be delivered like traditional online tutorials or video series. Each section includes links to interactive <strong>code playbacks</strong> that visually animate changes made to a program in a step-by-step manner, helping you understand how it was written.</p>
<p>A code playback shows how a program evolves by replaying all the steps in its development. It has an author-supplied narrative, screenshots, whiteboard-style drawings, and self-grading multiple choice questions to make the learning process more dynamic and interactive.</p>
<p>Here's a short YouTube video explaining how to view a code playback:</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/uYbHqCNjVDM" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<p> </p>
<h3 id="heading-playback-press"><strong>Playback Press</strong></h3>
<p><a target="_blank" href="https://playbackpress.com/books">Playback Press</a> is a platform for sharing interactive code walkthroughs that I created. I’m Mark, by the way, a professor of computer science. These books provide interactive programming lessons through step-by-step animations, AI tutoring, and quizzes.</p>
<p>If you want to see the full Clojure 'book', you can go here:</p>
<blockquote>
<p><a target="_blank" href="https://playbackpress.com/books/cljbook">An Animated Introduction to Clojure</a>, by Mark Mahoney</p>
</blockquote>
<p>I also built <a target="_blank" href="https://markm208.github.io/">Storyteller</a>, the free and open-source tool that powers code playbacks.</p>
<h3 id="heading-ai-tutor"><strong>AI Tutor</strong></h3>
<p>When viewing a code playback, you can ask an AI tutor about the code. It answers questions clearly and patiently, making it a helpful resource for learners. You can also ask the AI tutor to generate new self-grading multiple-choice questions to test your knowledge of what you are learning.</p>
<p>To access the AI tutor and self-grading quizzes, simply create a free account on Playback Press and add the <a target="_blank" href="https://playbackpress.com/books/cljbook">book</a> to your bookshelf.</p>
<h3 id="heading-table-of-contents"><strong>Table of Contents</strong></h3>
<ul>
<li><p><a class="post-section-overview" href="#heading-introduction-to-clojure">Introduction to Clojure</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-functions">Functions</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-closures">Closures</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-recursion">Recursion</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-lazy-sequences">Lazy Sequences</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-macros">Macros</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-concurrency">Concurrency</a></p>
</li>
</ul>
<h2 id="heading-introduction-to-clojure"><strong>Introduction to Clojure</strong></h2>
<p>These first few programs show how to print to the screen, perform basic arithmetic, and store some data. Go through each of these now:</p>
<ul>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/1/1">Hello World!!!</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/1/2">Readers/evaluators and simple arithmetic</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/1/3">Dog age converter</a></p>
</li>
</ul>
<p>This program shows how to use the Java capability that it built into to the JVM.</p>
<ul>
<li><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/1/4">Java interoperability</a></li>
</ul>
<p>These programs show some basic data structures in Clojure and how they are immutable.</p>
<ul>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/1/5">Clojure data structures</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/1/6">Efficient immutability (more with data structures)</a></p>
</li>
</ul>
<h3 id="heading-hands-on-practice"><strong>Hands-On Practice</strong></h3>
<p><strong>Problem 1</strong></p>
<p>Write a Clojure program that prompts the user for the length and width of a wooden board in inches. Then display the number of whole square feet that are in the board. For example, if the height is 27 inches and the width is 34 inches, then the number of square feet is 6.375.</p>
<p><strong>Problem 2</strong></p>
<p>Write a program that creates an empty list and use <code>def</code> to store a reference to it called <code>empty-list</code>. Use <code>cons</code> to add your name to it and store it in a new list called <code>my-name</code>.</p>
<p>Use <code>conj</code> to add all of your siblings to a list called <code>me-and-my-siblings</code> (if you don't have any or don't have that many you can use some of the <a target="_blank" href="https://en.wikipedia.org/wiki/Kardashian_family#Members_of_the_Kardashian-Jenner_Family">Kardashians</a>).</p>
<p>Print all the names in <code>me-and-my-siblings</code>. Then print the third name on the list <code>me-and-my-siblings</code>.</p>
<p>Create a map with all of your siblings' names as keys and their birth years as values. Use <code>assoc</code> to add your name and birth year to the map. Use the map to print everyone's name and their age in the year 2050.</p>
<p><strong>Problem 3</strong></p>
<p>Create a map with the number of days in each of the months called <code>days-in-months</code>. Use Clojure keywords like <code>:jan</code> and <code>:feb</code> as the keys and the number of days in the months as the values.</p>
<p>Create a second map from the first that has 29 days for February. Call this one <code>days-in-months-leapyear</code>. Make sure to do this efficiently, use <code>assoc</code> to create a new value for February. Finally, print each of the maps.</p>
<h2 id="heading-functions"><strong>Functions</strong></h2>
<p>Next, I'll discuss creating and calling functions in Clojure. Clojure is a functional programming language so this is a pretty important topic.</p>
<p>The first two programs show how to write functions in Clojure.</p>
<ul>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/2/1">Functions in Clojure</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/2/2">Fizz Buzz</a></p>
</li>
</ul>
<p>This program shows how to use a map to encapsulate data along with some functions that manipulate the data.</p>
<ul>
<li><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/2/3">Maps as objects</a></li>
</ul>
<p>These programs show how to read and write to a file using functions.</p>
<ul>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/2/4">Reading from a file (with CS poetry)</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/2/5">Writing to a file</a></p>
</li>
</ul>
<h3 id="heading-hands-on-practice-1"><strong>Hands-On Practice</strong></h3>
<p><strong>Problem 1</strong></p>
<p>Write three mathematical functions:</p>
<ul>
<li><p><code>square</code> squares a passed in parameter</p>
</li>
<li><p><code>cube</code> cubes a passed in parameter</p>
</li>
<li><p><code>pow</code> raises a base number to an exponent</p>
</li>
</ul>
<p>For this group of functions, do not use any built-in mathematical functions.</p>
<p>Hint: look at the Clojure function <code>repeat</code> and <code>reduce</code> for the <code>pow</code> function. Use the <code>let</code> function to hold temporary values so that they can be referred to later.</p>
<p><strong>Problem 2</strong></p>
<p>Write a function that takes a number and will calculate the factorial value for that number.</p>
<pre><code class="lang-cpp"><span class="hljs-number">5</span>! is <span class="hljs-number">5</span> * <span class="hljs-number">4</span> * <span class="hljs-number">3</span> * <span class="hljs-number">2</span> * <span class="hljs-number">1</span> = <span class="hljs-number">120</span>

<span class="hljs-number">10</span>! is <span class="hljs-number">10</span> * <span class="hljs-number">9</span> * <span class="hljs-number">8</span> * <span class="hljs-number">7</span> * <span class="hljs-number">6</span> * <span class="hljs-number">5</span> * <span class="hljs-number">4</span> * <span class="hljs-number">3</span> * <span class="hljs-number">2</span> * <span class="hljs-number">1</span> = <span class="hljs-number">3</span>,<span class="hljs-number">628</span>,<span class="hljs-number">800</span>
</code></pre>
<p>Hint: this type of problem is typically done with recursion. But for now, try to do it without recursion. Look at the Clojure <code>range</code> and <code>reduce</code> functions.</p>
<p><strong>Problem 3</strong></p>
<p>Write a function that determines whether a number is prime or not. Use the <code>range</code> and <code>filter</code> functions to filter out non-primes in a range of values.</p>
<p>Hint: look at the <code>not-any?</code> and <code>mod</code> functions for determining whether a number is prime or not.</p>
<p><strong>Problem 4</strong></p>
<p>Write a function that takes one or more string parameters, converts them to numbers, and then adds them together and returns the sum.</p>
<pre><code class="lang-cpp">(println (add-strings <span class="hljs-string">"10"</span>)) ;prints <span class="hljs-number">10</span>

(println (add-strings <span class="hljs-string">"10"</span> <span class="hljs-string">"20"</span>)) ;prints <span class="hljs-number">30</span>

(println (add-strings <span class="hljs-string">"10"</span> <span class="hljs-string">"20"</span> <span class="hljs-string">"30"</span>)) ;prints <span class="hljs-number">60</span>

(println (add-strings <span class="hljs-string">"10"</span> <span class="hljs-string">"20"</span> <span class="hljs-string">"30"</span> <span class="hljs-string">"40"</span>)) ;prints <span class="hljs-number">100</span>
</code></pre>
<p>Use Clojure's <code>reduce</code> function or <code>apply</code> to turn the strings into numbers and then add them together.</p>
<h2 id="heading-closures"><strong>Closures</strong></h2>
<p>A closure is a way to associate data permanently with a function.</p>
<p>The first program shows how to bind data to a function.</p>
<ul>
<li><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/3/1">Closures</a></li>
</ul>
<p>The second program shows a more complex example of using closures.</p>
<ul>
<li><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/3/2">Interest in an account</a></li>
</ul>
<h3 id="heading-hands-on-practice-2"><strong>Hands-On Practice</strong></h3>
<p><strong>Problem 1</strong></p>
<p>Write a function that returns a function. The Fibonacci numbers are a sequence of numbers that are generated by summing the previous two numbers together:</p>
<pre><code class="lang-cpp"><span class="hljs-number">0</span> <span class="hljs-number">1</span> <span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">3</span> <span class="hljs-number">5</span> <span class="hljs-number">8</span> <span class="hljs-number">13</span> <span class="hljs-number">21</span> <span class="hljs-number">34</span> ...
</code></pre>
<p>Usually, the first two numbers are assumed to be 0 and 1. But in this case, return a function that uses the specified first two numbers in the sequence. Then generate and print out the requested numbers in the sequence.</p>
<pre><code class="lang-cpp">(defn fibs-fn[firstFib secondFib] ...) ;;<span class="hljs-function">fill <span class="hljs-keyword">this</span> <span class="hljs-title">in</span>

<span class="hljs-params">(def another-fib (fibs-fn <span class="hljs-number">10</span> <span class="hljs-number">15</span>))</span> </span>;;create a fib function that prints starting with <span class="hljs-number">10</span> <span class="hljs-keyword">and</span> <span class="hljs-number">15</span>

(another-fib <span class="hljs-number">5</span>) ;;print first <span class="hljs-number">5</span> fibs: <span class="hljs-number">10</span> <span class="hljs-number">15</span> <span class="hljs-number">25</span> <span class="hljs-number">40</span> <span class="hljs-number">65</span>
</code></pre>
<h2 id="heading-recursion"><strong>Recursion</strong></h2>
<p>A recursive function is one that calls itself. Since every function call results in a stack frame being placed on the call stack, regular recursion runs the risk of 'blowing up the call stack'. Tail recursion, with <code>recur</code>, is an efficient way to simulate recursion without the downsides.</p>
<ul>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/4/1">Tail Recursion</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/4/2">Recursion and Fizz-Buzz</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/4/3">Recursion and Square Roots</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/4/4">Converting a String to an Integer in the Bases 2-16</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/4/5">Mortgage Scheduler</a></p>
</li>
</ul>
<h3 id="heading-hands-on-practice-3"><strong>Hands-On Practice</strong></h3>
<p><strong>Problem 1</strong></p>
<p>Write a recursive function to reverse the letters of a string.</p>
<pre><code class="lang-cpp">(reverse-<span class="hljs-built_in">string</span> <span class="hljs-string">"Mark Mahoney"</span>) ;;returns <span class="hljs-string">"yenohaM kraM"</span>
</code></pre>
<p>You may need to create a recursive 'helper' function that takes a different number of parameters than the non-recursive function or use the <code>loop</code> form. Make sure you use recursion with the <code>recur</code> form so that you do not 'blow the stack'.</p>
<p><strong>Problem 2</strong></p>
<p>Write a function that will join a series of strings together separated by another string. The function should create and return a new string. Use recursion with <code>recur</code>.</p>
<pre><code class="lang-cpp">(defn join [separator &amp; parts]
   ;;your code here)

(join <span class="hljs-string">", "</span> <span class="hljs-string">"Mark"</span> <span class="hljs-string">"Laura"</span> <span class="hljs-string">"Buddy"</span> <span class="hljs-string">"Patrick"</span> <span class="hljs-string">"Willy"</span>) ;;<span class="hljs-string">"Mark, Laura, Buddy, Patrick, Willy"</span>
</code></pre>
<p><strong>Problem 3</strong></p>
<p>Write a function that takes in an integer and converts it to a string. The user can specify the base that the string is in from 2-10. The program should use recursion and <code>recur</code> (either a recursive function or the <code>loop</code> form).</p>
<pre><code class="lang-cpp">(to-<span class="hljs-built_in">string</span> <span class="hljs-number">100</span> <span class="hljs-number">10</span>) ;;<span class="hljs-string">"100"</span> decimal
(to-<span class="hljs-built_in">string</span> <span class="hljs-number">7</span> <span class="hljs-number">2</span>) ;;<span class="hljs-string">"111"</span> binary
</code></pre>
<h2 id="heading-lazy-sequences"><strong>Lazy Sequences</strong></h2>
<p>A lazy sequence defers the cost of creating values until they are needed. The first program shows how to create a lazy sequence. The next two are more concrete examples.</p>
<ul>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/5/1">Lazy sequences</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/5/2">Lazy Prime Generator (Fizz Buzz part 3)</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/5/3">Poker Probabilities</a></p>
</li>
</ul>
<h3 id="heading-hands-on-practice-4"><strong>Hands-On Practice</strong></h3>
<p><strong>Problem 1</strong></p>
<p>Create a function that generates a lazy sequence of squares. For example, (1 4 9 16 ... to infinity). Use it to print the first 10 squared values.</p>
<p>Then write a function that generates a lazy sequence of values raised to a power:</p>
<pre><code class="lang-cpp">(defn lazy-<span class="hljs-built_in">pow</span> [start-val power]
  ...)

(take <span class="hljs-number">6</span> (lazy-<span class="hljs-built_in">pow</span> <span class="hljs-number">10</span> <span class="hljs-number">2</span>)) ;(<span class="hljs-number">100</span> <span class="hljs-number">121</span> <span class="hljs-number">144</span> <span class="hljs-number">169</span> <span class="hljs-number">196</span> <span class="hljs-number">225</span>)
(take <span class="hljs-number">6</span> (lazy-<span class="hljs-built_in">pow</span> <span class="hljs-number">10</span> <span class="hljs-number">3</span>)) ;(<span class="hljs-number">1000</span> <span class="hljs-number">1331</span> <span class="hljs-number">1728</span> <span class="hljs-number">2197</span> <span class="hljs-number">2744</span> <span class="hljs-number">3375</span>)
</code></pre>
<p><strong>Problem 2</strong></p>
<p>Write a function that generates an infinite lazy sequence of possible permutations of the passed in sequence.</p>
<pre><code class="lang-cpp">(take <span class="hljs-number">3</span> (lazy-perm [<span class="hljs-string">"a"</span> <span class="hljs-string">"b"</span> <span class="hljs-string">"c"</span>]))
;(<span class="hljs-string">"a"</span> <span class="hljs-string">"b"</span> <span class="hljs-string">"c"</span>)

(take <span class="hljs-number">12</span> (lazy-perm [<span class="hljs-string">"a"</span> <span class="hljs-string">"b"</span> <span class="hljs-string">"c"</span>])) 
;(<span class="hljs-string">"a"</span> <span class="hljs-string">"b"</span> <span class="hljs-string">"c"</span> <span class="hljs-string">"aa"</span> <span class="hljs-string">"ab"</span> <span class="hljs-string">"ac"</span> <span class="hljs-string">"ba"</span> <span class="hljs-string">"bb"</span> <span class="hljs-string">"bc"</span> <span class="hljs-string">"ca"</span> <span class="hljs-string">"cb"</span> <span class="hljs-string">"cc"</span>)

(take <span class="hljs-number">39</span> (lazy-perm [<span class="hljs-string">"a"</span> <span class="hljs-string">"b"</span> <span class="hljs-string">"c"</span>])) 
;(<span class="hljs-string">"a"</span> <span class="hljs-string">"b"</span> <span class="hljs-string">"c"</span> <span class="hljs-string">"aa"</span> <span class="hljs-string">"ab"</span> <span class="hljs-string">"ac"</span> <span class="hljs-string">"ba"</span> <span class="hljs-string">"bb"</span> <span class="hljs-string">"bc"</span> <span class="hljs-string">"ca"</span> <span class="hljs-string">"cb"</span> <span class="hljs-string">"cc"</span> <span class="hljs-string">"aaa"</span> <span class="hljs-string">"aab"</span> <span class="hljs-string">"aac"</span> <span class="hljs-string">"aba"</span> <span class="hljs-string">"abb"</span> <span class="hljs-string">"abc"</span> <span class="hljs-string">"aca"</span> <span class="hljs-string">"acb"</span> <span class="hljs-string">"acc"</span> <span class="hljs-string">"baa"</span> <span class="hljs-string">"bab"</span> <span class="hljs-string">"bac"</span> <span class="hljs-string">"bba"</span> <span class="hljs-string">"bbb"</span> <span class="hljs-string">"bbc"</span> <span class="hljs-string">"bca"</span> <span class="hljs-string">"bcb"</span> <span class="hljs-string">"bcc"</span> <span class="hljs-string">"caa"</span> <span class="hljs-string">"cab"</span> <span class="hljs-string">"cac"</span> <span class="hljs-string">"cba"</span> <span class="hljs-string">"cbb"</span> <span class="hljs-string">"cbc"</span> <span class="hljs-string">"cca"</span> <span class="hljs-string">"ccb"</span> <span class="hljs-string">"ccc"</span>)

(take <span class="hljs-number">120</span> (lazy-perm [<span class="hljs-string">"a"</span> <span class="hljs-string">"b"</span> <span class="hljs-string">"c"</span>]))
;(<span class="hljs-string">"a"</span> <span class="hljs-string">"b"</span> <span class="hljs-string">"c"</span> <span class="hljs-string">"aa"</span> <span class="hljs-string">"ab"</span> <span class="hljs-string">"ac"</span> <span class="hljs-string">"ba"</span> <span class="hljs-string">"bb"</span> <span class="hljs-string">"bc"</span> <span class="hljs-string">"ca"</span> <span class="hljs-string">"cb"</span> <span class="hljs-string">"cc"</span> <span class="hljs-string">"aaa"</span> <span class="hljs-string">"aab"</span> <span class="hljs-string">"aac"</span> <span class="hljs-string">"aba"</span> <span class="hljs-string">"abb"</span> <span class="hljs-string">"abc"</span> <span class="hljs-string">"aca"</span> <span class="hljs-string">"acb"</span> <span class="hljs-string">"acc"</span> <span class="hljs-string">"baa"</span> <span class="hljs-string">"bab"</span> <span class="hljs-string">"bac"</span> <span class="hljs-string">"bba"</span> <span class="hljs-string">"bbb"</span> <span class="hljs-string">"bbc"</span> <span class="hljs-string">"bca"</span> <span class="hljs-string">"bcb"</span> <span class="hljs-string">"bcc"</span> <span class="hljs-string">"caa"</span> <span class="hljs-string">"cab"</span> <span class="hljs-string">"cac"</span> <span class="hljs-string">"cba"</span> <span class="hljs-string">"cbb"</span> <span class="hljs-string">"cbc"</span> <span class="hljs-string">"cca"</span> <span class="hljs-string">"ccb"</span> <span class="hljs-string">"ccc"</span> <span class="hljs-string">"aaaa"</span> <span class="hljs-string">"aaab"</span> <span class="hljs-string">"aaac"</span> <span class="hljs-string">"aaba"</span> <span class="hljs-string">"aabb"</span> <span class="hljs-string">"aabc"</span> <span class="hljs-string">"aaca"</span> <span class="hljs-string">"aacb"</span> <span class="hljs-string">"aacc"</span> <span class="hljs-string">"abaa"</span> <span class="hljs-string">"abab"</span> <span class="hljs-string">"abac"</span> <span class="hljs-string">"abba"</span> <span class="hljs-string">"abbb"</span> <span class="hljs-string">"abbc"</span> <span class="hljs-string">"abca"</span> <span class="hljs-string">"abcb"</span> <span class="hljs-string">"abcc"</span> <span class="hljs-string">"acaa"</span> <span class="hljs-string">"acab"</span> <span class="hljs-string">"acac"</span> <span class="hljs-string">"acba"</span> <span class="hljs-string">"acbb"</span> <span class="hljs-string">"acbc"</span> <span class="hljs-string">"acca"</span> <span class="hljs-string">"accb"</span> <span class="hljs-string">"accc"</span> <span class="hljs-string">"baaa"</span> <span class="hljs-string">"baab"</span> <span class="hljs-string">"baac"</span> <span class="hljs-string">"baba"</span> <span class="hljs-string">"babb"</span> <span class="hljs-string">"babc"</span> <span class="hljs-string">"baca"</span> <span class="hljs-string">"bacb"</span> <span class="hljs-string">"bacc"</span> <span class="hljs-string">"bbaa"</span> <span class="hljs-string">"bbab"</span> <span class="hljs-string">"bbac"</span> <span class="hljs-string">"bbba"</span> <span class="hljs-string">"bbbb"</span> <span class="hljs-string">"bbbc"</span> <span class="hljs-string">"bbca"</span> <span class="hljs-string">"bbcb"</span> <span class="hljs-string">"bbcc"</span> <span class="hljs-string">"bcaa"</span> <span class="hljs-string">"bcab"</span> <span class="hljs-string">"bcac"</span> <span class="hljs-string">"bcba"</span> <span class="hljs-string">"bcbb"</span> <span class="hljs-string">"bcbc"</span> <span class="hljs-string">"bcca"</span> <span class="hljs-string">"bccb"</span> <span class="hljs-string">"bccc"</span> <span class="hljs-string">"caaa"</span> <span class="hljs-string">"caab"</span> <span class="hljs-string">"caac"</span> <span class="hljs-string">"caba"</span> <span class="hljs-string">"cabb"</span> <span class="hljs-string">"cabc"</span> <span class="hljs-string">"caca"</span> <span class="hljs-string">"cacb"</span> <span class="hljs-string">"cacc"</span> <span class="hljs-string">"cbaa"</span> <span class="hljs-string">"cbab"</span> <span class="hljs-string">"cbac"</span> <span class="hljs-string">"cbba"</span> <span class="hljs-string">"cbbb"</span> <span class="hljs-string">"cbbc"</span> <span class="hljs-string">"cbca"</span> <span class="hljs-string">"cbcb"</span> <span class="hljs-string">"cbcc"</span> <span class="hljs-string">"ccaa"</span> <span class="hljs-string">"ccab"</span> <span class="hljs-string">"ccac"</span> <span class="hljs-string">"ccba"</span> <span class="hljs-string">"ccbb"</span> <span class="hljs-string">"ccbc"</span> <span class="hljs-string">"ccca"</span> <span class="hljs-string">"cccb"</span> <span class="hljs-string">"cccc"</span>)
</code></pre>
<h2 id="heading-macros"><strong>Macros</strong></h2>
<p>A macro specifies some code to be executed, sort of like a function, but it also allows some of that code to be replaced with values that come from the user.</p>
<p>The code in a macro is kind of like a template that can be altered to suit the caller’s needs. With this powerful feature, the language can be expanded to do things that the language inventor never thought to add.</p>
<ul>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/6/1">Macros</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/6/2">Set macros</a></p>
</li>
</ul>
<h3 id="heading-hands-on-practice-5"><strong>Hands-On Practice</strong></h3>
<p><strong>Problem 1</strong></p>
<p>Write a macro that takes in a grade earned on a student assignment (on a 0-100 scale) and some code to execute if the grade is a passing or failing.</p>
<pre><code class="lang-cpp">(defmacro eval-grade [grade <span class="hljs-keyword">if</span>-passing <span class="hljs-keyword">if</span>-failing] ...)
</code></pre>
<p>And use it to print or call a function based on the value of the grade</p>
<pre><code class="lang-cpp">(def users-grade <span class="hljs-number">43</span>)

(eval-grade users-grade (println <span class="hljs-string">"Passing"</span>) (println <span class="hljs-string">"Failing"</span>)) ;;<span class="hljs-string">"Failing"</span>

(eval-grade users-grade (praise users-grade) (warning users-grade)) ;;call the warning function
</code></pre>
<h2 id="heading-concurrency"><strong>Concurrency</strong></h2>
<p>Concurrency in Clojure is a big topic. I start by discussing threads. Then I talk about different strategies for dealing with data that is shared among different threads.</p>
<ul>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/7/1">Threads</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/7/2">Threaded poker</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/7/3">refs and threads</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/7/4">Atoms</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/7/5">Poker with atoms</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/7/6">Thread logging with agents</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook/chapter/7/7">Simpler concurrency</a></p>
</li>
</ul>
<h3 id="heading-hands-on-practice-6"><strong>Hands-On Practice</strong></h3>
<p><strong>Problem 1</strong></p>
<p>This lab asks you to create a Clojure program that will count how many primes are in a given range.</p>
<p>Create a thread pool and have each thread check a single number in the range. If it finds a prime, it will increase a counter (which should be an <code>atom</code> since it is shared by all of the threads). Look at the program above on Atoms as a starting point.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>If you've made it this far, you’ve already taken meaningful steps toward learning a language that can change how you write and think about code.</p>
<p>Clojure offers a fresh perspective on programming, one that focuses on simplicity, immutability, and the power of functions. Learning Clojure will change your brain and you will take these lessons with you to other languages as well.</p>
<p>So keep experimenting, keep asking questions, and keep practicing to sharpen your skills.</p>
<h2 id="heading-comments-and-feedback"><strong>Comments and Feedback</strong></h2>
<p>You can find all of these code playbacks in the free 'book', <a target="_blank" href="https://playbackpress.com/books/cljbook/">An Animated Introduction to Clojure</a>. There are more free books here:</p>
<ul>
<li><p><a target="_blank" href="https://playbackpress.com/books/cppbook/">An Animated Introduction to Programming in C++</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/pybook">An Animated Introduction to Programming with Python</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/sqlbook">Database Design and SQL for Beginners</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/workedsqlbook">Worked SQL Examples</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/sqlitebook">Programming with SQLite</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/webdevbook">An Introduction to Web Development from Back to Front</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/cljbook">An Animated Introduction to Clojure</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/exbook">An Animated Introduction to Elixir</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/rubybook">A Brief Introduction to Ruby</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/flutterbook">Mobile App Development with Dart and Flutter</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/patternsbook">OO Design Patterns with Java</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/wordzearchbook">How I Built It: Word Zearch</a></p>
</li>
</ul>
<p>Comments and feedback are welcome via email: <a target="_blank" href="mailto:mark@playbackpress.com">mark@playbackpress.com</a>.</p>
<p>If you'd like to support my work and help keep Playback Press free for all, consider donating using <a target="_blank" href="https://github.com/sponsors/markm208">GitHub Sponsors</a>. I use all of the donations for hosting costs. Your support helps me continue creating educational content like this. Thank you!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Clojure Hashmaps Explained: How to Retrieve Values From and Update Hashmaps ]]>
                </title>
                <description>
                    <![CDATA[ A hashmap is a collection that maps keys to values. They have various names in other languages – Python refers to them as dictionaries, and JavaScript’s objects essentially work like hashmaps. A hashmap can, like many collections, be constructed in t... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/clojure-hashmaps-explained-how-to-retrieve-values-from-and-update-hashmaps/</link>
                <guid isPermaLink="false">66c34758eb0555cdb6fd9acf</guid>
                
                    <category>
                        <![CDATA[ Clojure ]]>
                    </category>
                
                    <category>
                        <![CDATA[ data structures ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sat, 01 Feb 2020 00:00:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9cea740569d1a4ca34e0.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>A hashmap is a collection that maps keys to values. They have various names in other languages – Python refers to them as dictionaries, and JavaScript’s objects essentially work like hashmaps.</p>
<p>A hashmap can, like many collections, be constructed in two ways. There is the constructor function:</p>
<pre><code class="lang-text">;; Note that each argument is *prepended* to the hashmap, not appended.
(def a-hashmap (hash-map :a 1 :b 2 :c 3))
a-hashmap
; =&gt; {:c 3, :b 2, :a 1}
</code></pre>
<p>You can also define them using a hashmap literal. This is often more concise and clear. Using commas to separate key/value pairs in hashmaps is recommended, as it can make the boundaries more clear.</p>
<pre><code class="lang-text">;; This hashmap is actually in the right order, unlike the one above.
(def another-hashmap {:a 1, :b 2, :c 3})
another-hashmap
; =&gt; {:a 1, :b 2, :c 3}
</code></pre>
<h2 id="heading-when-to-use-a-hashmap"><strong>When to use a hashmap?</strong></h2>
<p>A hashmap is useful when you want to give names to your variables. If you’re ever thinking to yourself, <em>“What if I used an object…”</em> before you snap out of it and realize you’re using Clojure, try using a hashmap.</p>
<p>They are also useful if you want to associate two different values with each other. Take, for example, a ROT13 cipher – you could associate <code>\A</code> with <code>\N</code>, <code>\B</code> with <code>\M</code>, and so on. </p>
<p>This would be long and boring to write in most languages, but Clojure has some functions that can generate it for you and make it <em>fun!</em></p>
<h2 id="heading-keywords-and-retrieving-values-from-hashmaps"><strong>Keywords and retrieving values from hashmaps</strong></h2>
<p>Hold up. What is this? <code>:a</code>? <code>:b</code>? <code>:c</code>? Those look odd. Those, you see, are keywords. They’re called <em>key</em>-words because they’re often used as keys in hashmaps.</p>
<p>Why are they often used as keys? Well, unlike strings, keywords can be used as functions to extract values from a hashmap; no need for <code>get</code> or <code>nth</code>!</p>
<pre><code class="lang-text">(def string-hashmap {"a" 1, "b" 2, "c" 3})
("a" string-hashmap)
; =&gt; ClassCastException java.lang.String cannot be cast to clojure.lang.IFn

(def keyword-hashmap {:a 1, :b 2, :c 3})
(:a keyword-hashmap)
; =&gt; 1

;; You can also pass a keyword a default value in case it's not found, just like get.
(:not-in-the-hashmap keyword-hashmap "not found!")
; =&gt; "not found!"
</code></pre>
<h2 id="heading-update-a-hashmap">Update a hashmap</h2>
<p>You can update values inside a hashmap using <code>assoc</code>. This allows you to append new key/value pairs or change old ones.</p>
<pre><code class="lang-text">(def outdated-hashmap {:a 1, :b 2, :c 3})

(def newer-hashmap (assoc outdated-hashmap :d 4))
newer-hashmap
; =&gt; {:a 1, :b 2, :c 3, :d 4}

(def newest-hashmap (assoc newer-hashmap :a 22))
newest-hashmap
; =&gt; {:a 22, :b 2, :c 3, :d 4}

;; Note that outdated-hashmap has not been mutated by any of this.
;; Assoc is pure and functional.
outdated-hashmap
; =&gt; {:a 1, :b 2, :c 3}
</code></pre>
<h2 id="heading-converting-other-collections-to-hashmaps"><strong>Converting other collections to hashmaps</strong></h2>
<p>Converting to a hashmap is tricky. To demonstrate, let’s try using it like <code>vec</code> or <code>seq</code>.</p>
<pre><code class="lang-text">(hash-map [:a 1 :b 2 :c 3])
; =&gt; IllegalArgumentException No value supplied for key: [:a 1 :b 2 :c 3]
</code></pre>
<p>The <code>hash-map</code> function thinks that we’re trying to create a hashmap with <code>[:a 1 :b 2 :c 3]</code> as one of the keys. Watch what happens if we give it the right number of arguments:</p>
<pre><code class="lang-text">(hash-map [:a 1 :b 2 :c 3] "foo")
; =&gt; {[:a 1 :b 2 :c 3] "foo"}
</code></pre>
<p>To convert a sequence to a hashmap, you’ll need to use and understand <code>apply</code>. Luckily, this is pretty simple: <code>apply</code> essentially destructures a collection before applying a function to it.</p>
<pre><code class="lang-text">;; These two expressions are exactly the same.
(+ 1 2 3)
; =&gt; 6
(apply + [1 2 3])
; =&gt; 6
</code></pre>
<p>This is how you would convert a vector to a hashmap:</p>
<pre><code class="lang-text">(apply hash-map [:a 1 :b 2 :c 3])
; =&gt; {:c 3, :b 2, :a 1}

;; This is the same as:
(hash-map :a 1 :b 2 :c 3)
; =&gt; {:c 3, :b 2, :a 1}
</code></pre>
<p>That should be everything you need to get started with hashmaps in Clojure. Now get out there and start hashing with the best of 'em.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
