<?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" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Daniel King on Medium]]></title>
        <description><![CDATA[Stories by Daniel King on Medium]]></description>
        <link>https://medium.com/@daniel.oliver.king?source=rss-8dad49c4d059------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*I18DWdTAuwPU6nxD8-IdAg.jpeg</url>
            <title>Stories by Daniel King on Medium</title>
            <link>https://medium.com/@daniel.oliver.king?source=rss-8dad49c4d059------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 08:49:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@daniel.oliver.king/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Getting REAL Work Done In Clojure: Application Development]]></title>
            <link>https://medium.com/@daniel.oliver.king/getting-real-work-done-in-clojure-application-development-683c8129a313?source=rss-8dad49c4d059------2</link>
            <guid isPermaLink="false">https://medium.com/p/683c8129a313</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[programming-languages]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[clojure]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Daniel King]]></dc:creator>
            <pubDate>Sat, 01 Jun 2019 18:06:36 GMT</pubDate>
            <atom:updated>2020-02-26T12:39:44.362Z</atom:updated>
            <content:encoded><![CDATA[<h4>A Joyful Introduction To Clojure: Part III</h4><p><a href="https://medium.com/@daniel.oliver.king/getting-work-done-in-clojure-the-building-blocks-39ad82796926">&lt;&lt; Part II</a></p><p>When you’re learning a new programming language, the language itself is only half the battle.</p><p>Most language tutorials focus heavily on the <em>features </em>of the<em> </em>language itself, which neglects the other half of the story: To get REAL work done, you also need to know the <em>ecosystem of tools</em> surrounding the language.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/455/1*6SgKoK5fE86XV5a4stFCdA.jpeg" /><figcaption>You, the first time you try to build a non-trivial application in an unfamiliar language</figcaption></figure><p>Now that you’ve learned some of the fundamentals of Clojure in <a href="https://medium.com/@daniel.oliver.king/a-clojure-development-environment-that-gets-out-of-your-way-c11e6711ead3">Part I</a> and <a href="https://medium.com/@daniel.oliver.king/getting-work-done-in-clojure-the-building-blocks-39ad82796926">Part II</a>, it’s time to get our hands dirty with application development.</p><p>We’re going to be creating a revolutionary new kind of application…</p><p>…a URL-shortening service.</p><p>Hey, don’t look at me like that, it’s a good learning exercise.</p><p>We’ll start with a reasonably simple version in this article, and then make it more sophisticated later.</p><p>I’ll assume that you have your Clojure development environment set up, that you’re familiar with how to do basic things in the language, and that you already have the <a href="https://github.com/dking1286/joyful_clojure">sample repository</a> set up on your local machine. If not, make sure you go through <a href="https://medium.com/@daniel.oliver.king/a-clojure-development-environment-that-gets-out-of-your-way-c11e6711ead3">Part I</a> and <a href="https://medium.com/@daniel.oliver.king/a-clojure-development-environment-that-gets-out-of-your-way-c11e6711ead3">Part II</a> before tackling this one, and pull the most recent version of the example repo from GitHub.</p><h3>So, what are we doing again?</h3><p>The ultimate goal is to get you familiar enough with application development in Clojure that you would feel comfortable picking up Clojure for your next software project, whether it is a side project for fun, or a professional business application.</p><p>We’re going to be building a URL-shortening service called Shortify. In this article, we’ll start with a bare-bones version that mostly uses the Clojure you’ve already learned. Then, in the next few articles, we’ll learn about some more advanced Clojure language features, use those features to make a more robust version of the Shortify application, and create a web frontend for the application using <a href="https://clojurescript.org/">ClojureScript</a>, the Clojure-to-Javascript compiler.</p><p>Buckle up, let’s dive in (I may be mixing metaphors here…)</p><h3>Overview</h3><p>At a high level, a basic URL-shortening service needs to be able to do two things:</p><ol><li>The user can submit a URL for shortening, and the service stores the URL and returns an id to the user</li><li>The user can submit an id corresponding to a stored URL, and the service will return the URL</li></ol><p>We will implement this behavior using an HTTP server and a PostgreSQL database:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*MEs7yp2ZCT_7fcDxhE9XFQ.png" /></figure><p>Let’s go through this piece-by-piece. If you haven’t already done so, open up the sample application in Atom:</p><pre>cd /path/to/joyful_clojure/03_clojure_application_development<br>atom .</pre><h3>The Database</h3><p>To make things as platform-independent as possible, the sample repository is set up to run a PostgreSQL database inside a Docker container. That way, you don’t have to mess around with installing PostgreSQL on your local machine and setting up permissions, which can be a headache.</p><p>If you want, you can skip Docker and run PostgreSQL directly on your local machine. In that case, you’ll need to tweak the project’s database settings accordingly.</p><p>If you plan to use Docker, <a href="https://docs.docker.com/install/">install Docker Community Edition</a> if you don’t already have it. Once you have Docker installed, you can test your installation by running the hello-world container:</p><pre>docker run hello-world</pre><p>Once you’ve got Docker up and running, you can start the PostgreSQL engine in a Docker container using the scripts/dev_database.sh script in the sample repository.</p><p>Before we run the script, let’s take a look at what it does:</p><pre>#! /bin/bash<br>docker run \<br>  --name=local-postgres \<br>  --rm \<br>  -e POSTGRES_USER=postgres \<br>  -e POSTGRES_PASSWORD=mysecretpassword \<br>  -d \<br>  -p 8082:5432 \<br>  -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data \<br>  postgres:alpine</pre><p>We’re going to run the postgres:alpine container image, and we’re naming the running container local-postgres.</p><p>The --rm option indicates that the container will be removed once it stops. The -d indicates that the container will be in “detached mode”, which means that it will run in the background, not in the foreground of the current terminal.</p><p>The environment variables given by the -e options indicate the login credentials that should be used to connect to the database once it is running. Since this database is just for local development, we can use the default “postgres” user and a hard-coded string as the password.</p><p>The -p option binds port 5432 inside the container to port 8082 outside the container. The PostgreSQL server inside the container listens for connections on port 5432, so we will be able to connect to the server from outside the container by sending queries to port 8082.</p><p>Finally, the -v option associates a directory in your filesystem with a directory in the container’s filesystem. The PostgreSQL server inside the container saves its data at /var/lib/postgresql/data and by associating these two directories, the data will be written to the filesystem <em>outside</em> the container at $HOME/docker/volumes/postgres. This allows the data in your development database to persist on your hard drive, even when the Docker container is stopped and deleted.</p><p>Got all that? Let’s run the database container:</p><pre>scripts/dev_database.sh</pre><p>If it works, you should see a hash id print out in your terminal, but nothing else should happen. You can check to see if the container is running by running docker container ls, which should show the local-postgres container.</p><p>Each time you restart your machine, you will need to run this script again to start the development database container. However, as mentioned above, the data will be persisted on your hard drive even when the container shuts down.</p><p>Next, let’s connect to the running PostgreSQL server using the psql command line client. I’ve provided another shell script to run the necessary Docker command:</p><pre>scripts/dev_database_connection.sh</pre><p>You should see something like this once it starts:</p><pre>psql (11.1)<br>Type &quot;help&quot; for help.</pre><pre>postgres=#</pre><p>Let’s create a database for development using the CREATE DATABASE command:</p><pre>postgres=# CREATE DATABASE url_shortening_db_dev;</pre><p>Then, make another database for testing:</p><pre>postgres=# CREATE DATABASE url_shortening_db_test;</pre><p>You can connect to the development database using the \c command:</p><pre>postgres=# \c url_shortening_db_dev<br>You are now connected to database &quot;url_shortening_db_dev&quot; as user &quot;postgres&quot;.<br>url_shortening_db_dev=#</pre><p>You can list the available data tables with the \d command:</p><pre>url_shortening_db_dev=# \d<br>Did not find any relations.<br>url_shortening_db_dev=#</pre><p>We don’t have any tables yet, so this message is not surprising.</p><p>Now that the database is up and running, we need to configure the application so that it can connect to this database.</p><h4>Clojure application configuration</h4><p>In order for our application to work in both local development and production, certain aspects of our application need to be configurable. In our case, we need to be able to connect to the <em>local</em> PostgreSQL database in development, and to the <em>production</em> database when the application is running on a production server.</p><p>In production, it is common to configure applications by passing in <strong>environment variables</strong>. In development, on the other hand, it is more convenient if we can save our configuration options into a <strong>file</strong>, which is ignored by Git.</p><p>We’re going to achieve this by using the <a href="https://github.com/weavejester/environ">environ</a> library for Clojure.</p><p>The environ library merges all configuration options passed to the application into a single map called env in the environ.core namespace. It gets these configuration options from 2 places: Environment variables, and a .lein-env file, if one exists.</p><p>The .lein-env file is not something you create manually — instead, the lein-environ Leiningen plugin automatically generates .lein-env by looking at the :env key in the project.clj configuration map.</p><p>Open project.clj and take a look at the :dependencies and :plugins sections:</p><pre>:dependencies [[org.clojure/clojure &quot;1.9.0&quot;]<br>               [org.clojure/data.json &quot;0.9.0&quot;]<br>               [ring &quot;1.7.1&quot;]<br>               [ring/ring-json &quot;0.4.0&quot;]<br>               [ring-logger &quot;1.0.1&quot;]<br>               [compojure &quot;1.6.1&quot;]<br>               [org.postgresql/postgresql &quot;42.2.5&quot;]<br>               [ragtime &quot;0.7.2&quot;]<br>               [environ &quot;1.1.0&quot;]<br>               [clj-time &quot;0.15.0&quot;]<br>               [com.taoensso/timbre &quot;4.10.0&quot;]]</pre><pre>:plugins [[lein-ring &quot;0.12.5&quot;]<br>          [lein-environ &quot;1.1.0&quot;]]</pre><p>As before, the :dependencies section includes dependencies of our application code. Don’t worry about all of these just yet — for now, notice that the environ library has been included.</p><p>The :plugins section declares the Leiningen plugins that should be used for this project. Leiningen plugins typically add additional functionality to Leiningen that can be used during the build process. In this case, we have included the lein-environ plugin, which automatically generates the .lein-env configuration file.</p><p>Next, take a look at the :profiles section:</p><pre>:profiles<br>{:dev {:env {:environment &quot;development&quot;}}</pre><pre> :test {:env {:environment &quot;test&quot;}<br>        :dependencies [[pjstadig/humane-test-output &quot;0.9.0&quot;]]<br>        :injections [(require &#39;pjstadig.humane-test-output)<br>                     (pjstadig.humane-test-output/activate!)]}</pre><pre> :prod {:env {:environment &quot;production&quot;}<br>        :uberjar-name &quot;app-standalone.jar&quot;<br>        :main main<br>        :aot :all}})</pre><p>Leiningen allows you to define different “profiles”, which specify configuration options for different environments (remember the ~/.lein/profiles.clj file where we declared the dependency on Proto-Repl?).</p><p>Let’s run the REPL with the dev profile active:</p><pre>lein with-profile +dev repl</pre><p>Once the REPL is running, you should see that a .lein-env file was generated in the project directory. This file should be ignored by Git. The .gitignore file in the example repository is already set up to ignore this file.</p><p>If you open .lein-env, you should see something like this:</p><pre>{:environment &quot;development&quot;}</pre><p>Notice that .lein-env is populated with the contents of the :env key in the dev profile.</p><p>Stop your REPL, and take a look at the db.core namespace inside the src folder of the project:</p><pre>(ns db.core<br>  (:require [environ.core :refer [env]]))</pre><pre>(def connection<br>  &quot;Map representing the database connection.&quot;<br>  {:dbtype (:database-type env)<br>   :dbname (:database-name env)<br>   :user (:database-username env)<br>   :password (:database-password env)<br>   :host (:database-host env)<br>   :port (:database-port env)})</pre><p>The environ library provides the environ.core namespace, from which we are importing the env map. This map merges the configuration options from .lein-env with any environment variables passed into the application.</p><p>Also, note that I am using <em>keywords as functions</em> in this expression. A keyword can be used as a function to look up the corresponding value in a map. In other words, (:database-type env) is equivalent to (get env :database-type)</p><p>There’s a problem here: We only populated the .lein-env file with one configuration option, {:environment &quot;development&quot;}. However, this namespace is expecting several other keys that configure the database connection: :database-type , :database-name , and so-on.</p><p>Naively, we might try including these configuration options into the dev profile defined in project.clj. However, it’s usually a <em>Bad Idea</em> to check your local database configuration into the Git repository.</p><p>Here’s the trick: Recall that Leiningen can <em>also </em>pull configuration options from files called profiles.clj — this is what we used to declare the dependency on Proto Repl. So, to configure your local database connection, we can create a profiles.clj file <em>in the project directory</em>, and tell Git to ignore it.</p><p>In the example repository, the .gitignore file is already set up to ignore profiles.clj. Create a profiles.clj file inside the 03_clojure_application_development project folder, and add the following contents:</p><pre>{:dev-local {:env {:port &quot;8080&quot;<br>                   :database-type &quot;postgresql&quot;<br>                   :database-name &quot;url_shortening_db_dev&quot;<br>                   :database-username &quot;postgres&quot;<br>                   :database-password &quot;mysecretpassword&quot;<br>                   :database-host &quot;localhost&quot;<br>                   :database-port &quot;8082&quot;}}<br> :test-local {:env {:port &quot;9090&quot;<br>                    :database-type &quot;postgresql&quot;<br>                    :database-name &quot;url_shortening_db_test&quot;<br>                    :database-username &quot;postgres&quot;<br>                    :database-password &quot;mysecretpassword&quot;<br>                    :database-host &quot;localhost&quot;<br>                    :database-port &quot;8082&quot;}}}</pre><p>Notice that we are defining <em>two </em>new profiles here: a dev-local profile which configures the connection to the development database we created inside the Docker container, and a test-local profile that configures the connection to the test database. If you decided not to use the Docker container to run PostgreSQL, you will need to adjust these configuration options accordingly.</p><p>Let’s run the REPL again, with the new dev-local profile included:</p><pre>lein with-profile +dev,+dev-local repl</pre><p>Once the REPL starts, take another look at the .lein-env file. You should see that your database configuration options are populated.</p><p>Connect your editor to the REPL and navigate to the db.core namespace. If everything worked correctly, the env map should contain the configuration options from the dev-local profile:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*wrAhKN9XuZtiXRI1Zj5mxQ.gif" /></figure><h4>Adding Tables to the Database</h4><p>Now, let’s add a <strong>urls</strong> table to our database, to store the shortened urls. We <em>could </em>do this with a CREATE TABLE statement in the psql command line client, but this approach is not very scalable.</p><p>Instead, whenever we need to change the structure of our database, we will create a <strong>migration</strong> and check it into Git, so that the database setup is repeatable by any other developers working on the project.</p><p>We will store the migration files in the resources/migrations directory and use the <a href="https://github.com/weavejester/ragtime">ragtime</a> library to manage these migrations.</p><p>For each database migration, we need to specify how to <em>add</em> the new tables to the database, and also how to <em>remove</em> them, in case we need to roll back to an earlier version of the database. Ragtime expects us to store these two components of the migration as two different files: one with an up.sql file extension, and one with down.sql.</p><p>We need to keep track of the order in which the migrations should happen as well, so I’m labeling each migration file with the timestamp at which it was created.</p><p>Take a look at the xxxx-create-urls-table.up.sql migration:</p><pre>CREATE TABLE urls (<br>  id VARCHAR(255) NOT NULL UNIQUE PRIMARY KEY,<br>  url VARCHAR(255) NOT NULL<br>);</pre><p>Our urls table has 2 columns: id and url.</p><p>Let’s run this migration to create the urls table.</p><p>Open the db.migration namespace in your editor, and change into this namespace in your REPL as well.</p><p>A couple of things to notice:</p><ol><li>Some of the functions in this namespace are defined with defn- instead of defn. This indicates that those functions are private, and cannot be imported into other namespaces.</li><li>At the top of the file, we are bringing in functions from several other namespaces using the :require form. In addition, we are directly importing the org.postgresql.util.PSQLException Java class using the :import form.</li><li>This namespace exposes 4 public functions: create-migration!, migrate-up!, rollback!, and migrate-down!.</li></ol><p>Run the migrate-up! function to create the new table in the database:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*RdZpFZoSIpBRhU3a01htqQ.gif" /></figure><p>Ragtime keeps track of which migrations have already been run. If you run migrate-up! again, Ragtime knows that the create-urls-table migration has already happened, so it will not try to run it again:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*8ZJWkNT8dGUd5luZsevnfw.gif" /></figure><p>Notice that the “applying xxxx-create-urls-table” message was not printed the second time, indicating that ragtime did not make any changes to the database.</p><p>Connect to your database with scripts/dev_database_connection.sh again, switch to the development database, and list the tables:</p><pre>postgres=# \c url_shortening_db_dev<br>You are now connected to database &quot;url_shortening_db_dev&quot; as user &quot;postgres&quot;.<br>url_shortening_db_dev=# \d<br>               List of relations<br> Schema |        Name        | Type  |  Owner   <br>--------+--------------------+-------+----------<br> public | ragtime_migrations | table | postgres<br> public | urls               | table | postgres<br>(2 rows)</pre><p>As expected, the urls table was created. Ragtime also created a ragtime_migrations table to keep track of which migrations have already happened.</p><p>You can get more details about an individual table by passing it as an argument to the \d command:</p><pre>url_shortening_db_dev=# \d urls<br>                       Table &quot;public.urls&quot;<br> Column |          Type          | Collation | Nullable | Default <br>--------+------------------------+-----------+----------+---------<br> id     | character varying(255) |           | not null | <br> url    | character varying(255) |           | not null | <br>Indexes:<br>    &quot;urls_pkey&quot; PRIMARY KEY, btree (id)</pre><p>Our urls table is created, but it currently has no rows:</p><pre>url_shortening_db_dev=# SELECT * FROM urls;<br> id | url <br>----+-----<br>(0 rows)</pre><p>Let’s add some fake data into the database, so that we have something to look at when we run the server.</p><h4>Seeding the Database</h4><p>Inserting initial data into a new database is known as <strong>seeding</strong> the database. For this reason, we will keep the fake data files in the resources/seeds directory.</p><p>Open the urls.edn file in the resources/seeds directory:</p><pre>[{:table :urls<br>  :data [{:id &quot;12345&quot;<br>          :url &quot;<a href="https://someawesomewebsite.com/">https://someawesomewebsite.com</a>&quot;}<br>         {:id &quot;67890&quot;<br>          :url &quot;<a href="https://anotherwebsite.com/?some=param">https://anotherwebsite.com?some=param</a>&quot;}<br>         {:id &quot;23456&quot;<br>          :url &quot;<a href="https://myspace.com/">https://myspace.com</a>&quot;}]}]</pre><p>Notice that we are defining 3 records for the urls table in this file.</p><p>The .edn file extension stands for “extensible data notation”, and it is roughly equivalent to a JSON file, except that it uses Clojure syntax instead of Javascript syntax to represent the data. Clojurists would point out that EDN offers some compelling advantages over JSON, but that is a story for another time.</p><p>At a high level, in order to seed the data into the database, we need to do the following:</p><ol><li>Get a list of all files in the resources/seeds directory</li><li>Read the text of each file into memory</li><li>Parse the text of each file into proper data structures</li><li>Insert the data from each file into our SQL database as table rows</li></ol><p>These tasks are accomplished in the db.seed namespace. Open it in your editor, and let’s break down the new concepts.</p><p>I’ve mentioned a few times that Clojure runs on the Java Virtual Machine (JVM). One of the explicit design goals of Clojure is to embrace the capabilities of the host platform. As a result, many common IO tasks in Clojure are accomplished using thin wrappers over tried-and-true Java libraries.</p><p>In the db.seed namespace, we are using two such libraries: clojure.java.io is used to read and write to the file system, and is a thin wrapper around the <a href="https://docs.oracle.com/javase/7/docs/api/java/io/package-summary.html">java.io</a> package. Meanwhile, clojure.java.jdbc is the standard way of reading and writing to SQL databases and is a thin wrapper around the <a href="https://docs.oracle.com/javase/tutorial/jdbc/basics/index.html">JDBC library</a>.</p><p>Let’s look at the insert-seed! function first.</p><pre>(defn- insert-seed!<br>  &quot;Inserts the contents of a single seed file into the database.&quot;<br>  [seed]<br>  (doseq [{:keys [table data]} seed]<br>    (jdbc/insert-multi! connection table data)))</pre><p>The parameter seed is expected to be the contents of a seed file, which is a vector of maps, each of which contains :table and :data keys specifying a list of rows to be inserted into one table. For each map, we are calling the jdbc/insert-multi! function, which takes the following parameters:</p><ul><li>connection, a map representing the database location and credentials</li><li>table, a keyword representing the name of the table</li><li>data, a sequence of maps, each of which represents the structure of a row in the table</li></ul><p>Next, the get-all-seed-files function:</p><pre>(defn- get-all-seed-files<br>  &quot;Gets a sequence of File objects representing all the files<br>   in the seeds directory.&quot;<br>  []<br>  (.listFiles (io/file (io/resource &quot;seeds&quot;))))</pre><p>There are 3 steps happening here: First, the io/resource function creates an absolute URL to something in the resources directory. Next, the io/file function creates a <a href="https://docs.oracle.com/javase/8/docs/api/java/io/File.html">java.io.File</a> object representing the target of that URL, which in this case is the seeds directory. Finally, we call the <a href="https://docs.oracle.com/javase/8/docs/api/java/io/File.html#listFiles--">.listFiles</a> method to get a sequence of java.io.File objects representing all the files in that directory.</p><p>Where is this .listFiles function coming from? Since Clojure embraces the underlying JVM platform, you can actually directly call methods on Java objects from within Clojure:</p><pre>// Java syntax<br>file.listFiles()</pre><pre>;; Clojure syntax<br>(.listFiles file)</pre><p>If a function name begins with a ., the Clojure compiler will look for a method with that name on the passed-in object, instead of looking for a function with that name in the current namespace.</p><p>To complete the seeding process, we need two other functions: The slurp function reads the entire contents of a file into memory as a string, and the edn/read-string function parses a string of .edn data into a Clojure data structure, similar to how the JSON.parse function works in Javascript.</p><p>The entire process of inserting the seed data into the database is implemented by the insert-all-seeds! function:</p><pre>(defn insert-all-seeds!<br>  &quot;Reads all files in the seeds directory and inserts their contents into<br>   the database.&quot;<br>  []<br>  (-&gt;&gt; (get-all-seed-files)<br>       (map slurp)<br>       (map edn/read-string)<br>       (map insert-seed!)<br>       doall))</pre><p>Here we are using the thread-last macro -&gt;&gt; that we introduced <a href="https://medium.com/@daniel.oliver.king/getting-work-done-in-clojure-the-building-blocks-39ad82796926">last time</a>.</p><p>There’s one other trick here: Recall that map returns a <em>lazy</em> sequence, meaning that the values in the sequence are not actually computed until we try to <em>read</em> values from the sequence. In this case, we want all of the computation to happen <em>immediately </em>so that the seeds are actually, y’know, inserted into the database. The doall function forces all elements of a lazy sequence to be evaluated immediately.</p><p>Got all that? Let’s run the insert-all-seeds! function to seed your database with data. Make sure you change into the db.seed namespace in your REPL first:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*kvVZlkdI8d6CvTEnvTyetg.gif" /></figure><p>If all went well, you should now have 3 rows in the urls table in the database:</p><pre>url_shortening_db_dev=# SELECT * FROM urls;</pre><pre>  id   |                  url                  <br>-------+---------------------------------------<br> 12345 | <a href="https://someawesomewebsite.com/">https://someawesomewebsite.com</a><br> 67890 | <a href="https://anotherwebsite.com/?some=param">https://anotherwebsite.com?some=param</a><br> 23456 | <a href="https://myspace.com/">https://myspace.com</a><br>(3 rows)</pre><p>Side note: What happens if we run the seed function again? We put a UNIQUE constraint on the id column, so the seeding process should fail. Indeed, this is exactly what happens:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*0h3rf1n7zIugW6xt3NqRxg.gif" /></figure><p>With that, our database is ready. Next, let’s talk about the server itself.</p><h3>The Server</h3><p>To construct our http server, we’re going to use two main libraries: <a href="https://github.com/ring-clojure/ring">ring</a> for the base server abstraction, and <a href="https://github.com/weavejester/compojure">compojure</a> for routing.</p><h4>Ring</h4><p>As HTTP servers go, ring’s structure is wonderfully simple: A server is represented by a single <strong>handler </strong>function, which takes a map representing a request as a parameter and returns a map representing a response. Here’s an entire http server with two routes and a default “not-found” response:</p><pre>(defn handler<br>  [req]<br>  (case (:uri req)<br>    &quot;/&quot; {:status 200<br>         :headers {&quot;Content-type&quot; &quot;text/plain&quot;}<br>         :body &quot;Hello world!&quot;}</pre><pre>    &quot;/goodbye&quot; {:status 200<br>                :headers {&quot;Content-type&quot; &quot;text/plain&quot;}<br>                :body &quot;Goodbye world!&quot;}</pre><pre>    {:status 404<br>     :headers {&quot;Content-type&quot; &quot;text/plain&quot;}<br>     :body &quot;Not found&quot;}))</pre><p>There’s no complex API to remember, the server is just a simple function that takes in data and returns data. This is what we mean when we say that Clojure embraces <strong>data-oriented programming</strong>.</p><p>As with many http server libraries, ring has <strong>middleware</strong> to represent the idea of doing extra processing on each request and response, such as logging each request, or automatically serializing the response body into a JSON string.</p><p>In ring, a middleware is implemented as a <em>function that takes a handler and returns a modified handler</em>.</p><p>For example, here is a middleware that prints each request:</p><pre>(defn wrap-request-printing<br>  [handler]<br>  (fn [req]<br>    (println req)<br>    (handler req)))</pre><p>Here is a middleware that uses json/write-string from <a href="https://github.com/clojure/data.json">data.json</a> to serialize the response body into a JSON string, assuming that the :body key on the response is a Clojure data structure:</p><pre>(defn wrap-json-response<br>  [handler]<br>  (fn [req]<br>    (let [res (handler req)]<br>      (-&gt; res<br>          (update :body json/write-str)<br>          (assoc-in [:headers &quot;Content-type&quot;]<br>                    &quot;application/json&quot;)))))</pre><p>In a ring application, it’s typical to define an app variable representing your handler wrapped with several middleware:</p><pre>(def app<br>  (-&gt; handler<br>      wrap-request-printing<br>      wrap-json-response))</pre><p>In the sample application, this is done in the app namespace:</p><pre>(ns app<br>  (:require [ring.middleware.json :refer [wrap-json-body<br>                                          wrap-json-response]]<br>            [middleware.logging :refer [wrap-logging]]<br>            [middleware.error-handling :refer [wrap-error-handling]]<br>            [routes :refer [root-handler]]))</pre><pre>(def app<br>  &quot;Main Ring handler for the application&quot;<br>  (-&gt; root-handler<br>      wrap-error-handling<br>      wrap-logging<br>      ;; Serialize the response body into JSON<br>      wrap-json-response<br>      ;; If the request body contains JSON, parse it into a Clojure<br>      ;; data structure, converting string keys to keywords<br>      (wrap-json-body {:keywords? true})))</pre><p>Usually, you can find most of the middleware you need in libraries, such as the wrap-json-body and wrap-json-response middleware I’m using from the <a href="https://github.com/ring-clojure/ring-json">ring-json</a> library.</p><p>However, it <em>is</em> useful to be able to create your own middleware for error handling, which is done in the middleware.error-handling namespace:</p><pre>(ns middleware.error-handling)</pre><pre>(defn get-response-from-error<br>  &quot;Converts a thrown error into the response that should be<br>   sent back to the client.&quot;<br>  [error]<br>  (let [type (get (ex-data error) :type)]<br>    (case type<br>      :not-found {:status 404 :body &quot;Not found&quot;}<br>      {:status 500 :body &quot;Internal server error&quot;})))</pre><pre>(defn wrap-error-handling<br>  &quot;Ring middleware that catches any thrown errors and sends an appropriate<br>   response back to the client.&quot;<br>  [handler]<br>  (fn [req]<br>    (try<br>      (handler req)<br>      (catch Exception e<br>        (get-response-from-error e)))))</pre><p>The handler functions of the application are defined in the urls namespace. For example, the create-url-handler reads the request body, creates a url in the database, and returns a 201 response if creation was successful:</p><pre>(defn create-url-handler<br>  [req]<br>  (let [url (get-in req [:body :url])<br>        row (create-url! url)]<br>    {:status 201 :body row}))</pre><p>At this point you might be wondering: I mentioned that a ring application consists of a <strong>single</strong> handler function, but we have <strong>two </strong>handler functions defined in the urls namespace. This is where our routing library comes in.</p><h4>Compojure</h4><p>Like ring, compojure is dirt simple: It just defines a bunch of macros that allow us to compose multiple handler functions into one without manually writing if statements to check the url and request method.</p><p>The single ring handler of our application is defined in the routes namespace:</p><pre>(ns routes<br>  (:require [compojure.core :refer :all]<br>            [compojure.route :as route]<br>            [urls]))</pre><pre>(defroutes root-handler<br>  (GET &quot;/urls/:id&quot; [] urls/get-url-handler)<br>  (POST &quot;/urls&quot; [] urls/create-url-handler)<br>  (route/not-found &quot;Not found&quot;))</pre><p>Notice that we are using :refer :all in the require statement for comopjure.core, which means that defroutes, GET, POST, and all other functions defined in compojure.core are usable in this namespace.</p><p>Taken all together, our server currently implements the following behavior:</p><ul><li>There are 2 routes: One for creating new short urls, and one for getting existing urls. If a request is sent to any other path, a default “Not found” response is sent.</li><li>If the request body contains JSON, it is automatically parsed into a Clojure data structure by the wrap-json-body middleware</li><li>If the response body contains a Clojure data structure, it is automatically serialized into JSON by the wrap-json-response middleware</li><li>If an error is thrown while handling a request, the error is caught by the wrap-error-handling middleware and an appropriate response is sent back to the client</li><li>Each request is logged by the wrap-logging middleware</li></ul><p>Now that we’ve explored the components of the codebase, let’s actually run the server.</p><h4>Running the Development Server</h4><p>If we wanted, we could start the server directly from the REPL by calling the function main/-main. However, there is a major problem with this approach: When we refresh the REPL, the running server would not pick up the changes. We would need to manually stop and restart the server every time we make a change.</p><p>To sidestep this problem and give ourselves a better development experience, we’re going to use the <a href="https://github.com/weavejester/lein-ring">lein-ring</a> Leiningen plugin.</p><p>Notice that I included lein-ring in the :plugins key in project.clj. I also included some configuration options for the plugin in the :ring key:</p><pre>:ring {:handler app/app<br>       :port 8080<br>       :nrepl {:start? true :port 8081}<br>       :auto-reload? true}</pre><p>As the configuration options imply, the plugin will start an http server on port 8080 using app/app as the main Ring handler. The server will automatically reload when we make changes to our source files.</p><p>The plugin will also start an nREPL server on port 8081, allowing us to connect our editor just as if we had run a normal lein repl.</p><p>So, how do we actually start the server? The plugin actually adds a few new CLI commands to Leiningen. Try running the following:</p><pre>lein with-profile +dev,+dev-local ring server-headless</pre><p>You should see some messages printed in the console saying that the http server and nREPL server are now running.</p><p>Try making a request to the server from another terminal:</p><pre>curl localhost:8080</pre><p>The server should respond with Not found. This is expected, because we didn’t define a &quot;/&quot; route in our handler. You should also see some information about the request logged to the console. The wrap-logging middleware is responsible for that.</p><p>You should also be able to connect Atom to the nREPL server running on port 8081 (try it to be sure).</p><p>Let’s (finally!) use our API to create a shortened URL:</p><pre>curl localhost:8080/urls \<br>  -X POST \<br>  -H &quot;Content-type: application/json&quot; \<br>  -d &#39;{&quot;url&quot;: &quot;https://some-other-website.com&quot;}&#39;</pre><p>The server should respond with a json string containing &quot;id&quot; and &quot;url&quot; fields. Also, let’s check the development database to make sure the new record was actually added (replace the id value with the one that was returned from the server):</p><pre>url_shortening_db_dev=# SELECT url<br>url_shortening_db_dev-# FROM urls<br>url_shortening_db_dev-# WHERE id = &#39;b371e020-63c8-47b0-aa7b-83226d183788&#39;;<br>              url               <br>--------------------------------<br> <a href="https://some-other-website.com/">https://some-other-website.com</a><br>(1 row)</pre><pre>url_shortening_db_dev=#</pre><p>So far so good. Finally, let’s check that the server can look up a url by id (again, replace the id in the request url with your value):</p><pre>curl localhost:8080/urls/b371e020-63c8-47b0-aa7b-83226d183788</pre><p>The server should return a JSON document representing the database row.</p><p>Great, our server works! Of course, we’ve only checked the “happy paths”. To ensure that our server is robust to failures, it would be a good idea to do some <strong>automated testing </strong>on the server as well.</p><p>The example repository doesn’t have very thorough test coverage (that’ll be your job), but there are a few unit tests and integration tests. Let’s talk about each in turn.</p><h4>Unit Testing</h4><p>There are some unit tests for the error-handling middleware in the file test/middleware/error_handling_test.clj, which defines the middleware.error-handling-test namespace.</p><p><strong>Extremely important side note</strong>: Notice the correspondence between namespaces and file names. The file that defines a namespace must satisfy two conditions:</p><ol><li>The file must be in a top-level folder declared in the :source-paths or :test-paths keys in project.clj. For those of you that are familiar with the JVM, this technically means that your file must be “on the classpath.”</li><li>The relative path of the file from the top level folder must <em>precisely</em> match the namespace name, with all “-” characters replaced with “_”.</li></ol><p>This is a<strong> requirement</strong> in order for the Clojure compiler to be able to find your files. The most common mistake I make is to violate condition 2 by naming my files with dashes instead of underscores. So, if I try to define a namespace middleware.error-handling-test in a file called middleware/error-handling-test.clj, I will get an error message pointing out my mistake:</p><pre>Could not locate middleware/error_handling_test__init.class or middleware/error_handling_test.clj on classpath. Please check that namespaces with dashes use underscores in the Clojure file name.</pre><p>Don’t be like me. Remember to name your files with <strong>underscores</strong>, not dashes.</p><p>Okay, back to unit testing.</p><p>Recall that in <a href="https://medium.com/@daniel.oliver.king/a-clojure-development-environment-that-gets-out-of-your-way-c11e6711ead3">Part I</a> of this guide, I mentioned that we can use the REPL to run our tests. Let’s take a look at the middleware.error-handling-test namespace:</p><pre>(ns middleware.error-handling-test<br>  (:require [clojure.test :refer :all]<br>            [middleware.error-handling :refer :all]))</pre><pre>(deftest test-get-response-from-error<br>  (testing &quot;should return a 404 response when the exception data<br>            includes :type :not-found&quot;<br>    (let [e (ex-info &quot;Boom&quot; {:type :not-found})<br>          response (get-response-from-error e)]<br>      (is (= (:status response) 404))))<br>  (testing &quot;should return a 500 response otherwise&quot;<br>    (let [e (ex-info &quot;Boom&quot; {:type :wrong})<br>          response (get-response-from-error e)]<br>      (is (= (:status response) 500)))))</pre><p>Test cases defined with deftest are actually just functions, and we can run the test by directly calling the function in the REPL:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kvn-AQnt8MbXMlwkhUAeQA.gif" /></figure><p>Notice what I just did:</p><ol><li>I changed into the middleware.error-handling-test namespace</li><li>I called the test-get-response-from-error function. No error was reported, meaning the test passed.</li><li>To show what happens when a test fails, I changed the condition in the test so that it would fail</li><li>I refreshed the REPL</li><li>I called the test-get-response-from-error function again, and an error was reported in the REPL.</li></ol><p>This approach works, but it requires a lot of steps to rerun your tests. Luckily, Proto-Repl includes some handy commands for automating this process:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*kv7uIfK6iwSkfZU1uvL6_Q.jpeg" /></figure><p>My favorite is “Run Tests in Namespace”, but all three of them will automatically refresh your REPL and change into the current namespace, so you don’t have to do it manually:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZHSUal845-7LnztGhV50iA.gif" /></figure><p>This approach works great for unit testing, but we also want to be able to test the behavior of the whole system together, including the database.</p><h4>Integration Testing with the Database</h4><p>In order for our tests to be reliable and repeatable, it’s important for each test case to be <em>isolated</em>, meaning that it does not depend on any other test cases. This means that we want each test case to start with the database in a fresh, known state.</p><p>We can accomplish this by doing the following for each test case:</p><ol><li>Run migrate-down! to delete all the tables and data from the database</li><li>Run migrate-up! to re-create all the tables</li><li>Run insert-all-seeds! to add known data to the database</li><li>Run the actual test case</li></ol><p>If one test case changes the state of the database, no other test cases will be affected.</p><p>In Clojure, we accomplish this with <strong>fixtures</strong>, which are functions that specify “setup” or “teardown” operations that you want to do around your test cases.</p><p>The file tests/routes_test.clj contains a few basic integration tests. Open it, and notice the following invocation at the top of the file:</p><pre>(use-fixtures :each with-database-reset)</pre><p>Here we are declaring that we want to run the with-database-reset function before each test case. We’ll look at the definition of the function in a moment.</p><p>I mentioned before that you can use the REPL to run your tests during development, but there’s a tricky detail when it comes to integration tests: We want to run our integration tests with the url_shortening_db_test database, but the development REPL is running with the :dev-local profile, which means that we are connected to the url_shortening_db_dev database instead.</p><p>Therefore, if I run my integration tests from the REPL, my <em>development </em>database will be reset, instead of the dedicated <em>testing</em> database!</p><p>This is a solvable problem, but it requires rethinking the architecture of the application. We’ll do this is a later article in the series, but for now, we’ll just <em>disallow</em> running the integration tests from the development REPL, and use the Leiningen test runner instead.</p><p>Open the test_helpers.clj file, and take a look at the definition of the with-database-reset function:</p><pre>(defn with-database-reset<br>  [run-tests]<br>  ;; Implementation<br>  )</pre><p>Notice that this fixture function takes a run-tests callback function as a parameter. As the name implies, this callback will execute the actual test case. Therefore, we can put setup code <em>before</em> the invocation of run-tests and teardown code <em>after</em>.</p><p>Notice that this fixture throws an error if the (:environment env) is not &quot;test&quot;. This is our ham-fisted way of disallowing running tests on the development database.</p><p>Instead, we can run the integration tests from the command line with Leiningen:</p><pre>lein with-profile +test,+test-local test :once</pre><p>Notice that we are using the test and test-local profiles, which will use the testing database that we configured in profiles.clj.</p><p>You will probably see a bunch of stuff print in the console (that’s the Ring logging middleware), but at the end you should see something like this:</p><pre>Ran 3 tests containing 8 assertions.<br>0 failures, 0 errors.</pre><h3>Wrapping Up &amp; Exercises</h3><p>Okay, that was a lot. Let’s recap what we’ve done:</p><ul><li>Set up a PostgreSQL server inside a Docker container, and created development and testing databases inside the container.</li><li>Used environ and profiles.clj to configure the database connection for different application environments.</li><li>Applied <strong>migrations</strong> to change the database structure, and <strong>seeds </strong>to populate the database with initial data.</li><li>Learned about constructing servers with Ring <strong>handlers</strong> and <strong>middleware.</strong></li><li>Learned about using Compojure to compose multiple handlers with routing logic.</li><li>Used the lein-ring plugin to run the devlopment server with automatic reloading.</li><li>Ran unit tests from the development REPL and integration tests from the command line.</li></ul><p>The application’s functionality is currently pretty bare bones, so here are a few suggested improvements you can make to practice what you’ve learned.</p><h4>Exercise 1</h4><p>Right now, the user just gets back a random UUID when they add a new URL to the database. It would be nice if they could specify the “id” of the url at creation time, so it will be easier for them to remember.</p><p>Update the URL creation route to allow the user to pass in an id parameter. If they don’t pass it in, a random id should be generated for them.</p><p><strong>Extra credit: </strong>If the user passes in an id that already exists, a 409 (conflict) status code should be returned by the server.</p><h4>Exercise 2</h4><p>Currently, there is no way to modify or delete short urls from the database. Once you create one, it’s there forever.</p><p>Implement two new routes: DELETE /urls/:id should delete the corresponding URL from the database, and PATCH /urls/:id allows the user to update an existing record in the database by passing JSON data in the request body.</p><h4>Exercise 3 (only attempt if you’re feeling ambitious)</h4><p>In this version of the application, there is no notion of “ownership” or “access controls”. Once you’re done with Exercise 2, anyone can update or delete anyone else’s short urls.</p><p>Design and implement an ownership system so that a user can only update or delete their own short urls. They should receive a 403 (forbidden) resopnse if they try to update a short url that doesn’t belong to them.</p><p><strong>Hint:</strong> The <a href="https://github.com/funcool/buddy">buddy</a> library may be useful for authentication and authorization.</p><h4>Next Steps</h4><p>Even this simple application has introduced a lot of key parts of the Clojure ecosystem. However, there are some serious shortcomings:</p><ol><li>Because the database connection is configured by the global env map, there is no way to run tests requiring a database from the development REPL without damaging your development database. Instead, you have to use lein test, which runs a completely separate Leiningen process with a different profile.</li><li>The lein-ring plugin allows us to start a development server with automatic reloading. However, the REPL needs to be manually refreshed. If you forget to refresh the REPL, the state of the dev server and the REPL will be out of sync with each other, leading to confusing behavior.</li><li>By default, the clojure.java.jdbc library creates a new database connection for every query. This is fine for development, but it’s inefficient for production, where your server will be handling many concurrent requests. A better approach would be to create a <strong>connection pool</strong>, so that existing connections can be reused for future queries.</li><li>There is no documentation or enforcement on the required shape of function parameters and return values.</li></ol><p>Our end goal is to create a more “production-ready” version of the Shortify application that solves these problems. However, before we do that, we’ll need to explore some of Clojure’s more advanced freatures. Next up: State management and concurrency in Clojure.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=683c8129a313" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting Work Done in Clojure: The Building Blocks]]></title>
            <link>https://medium.com/@daniel.oliver.king/getting-work-done-in-clojure-the-building-blocks-39ad82796926?source=rss-8dad49c4d059------2</link>
            <guid isPermaLink="false">https://medium.com/p/39ad82796926</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[clojure]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[programming-languages]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Daniel King]]></dc:creator>
            <pubDate>Sun, 02 Dec 2018 02:11:20 GMT</pubDate>
            <atom:updated>2019-06-01T18:08:08.488Z</atom:updated>
            <content:encoded><![CDATA[<h4>A Joyful Introduction to Clojure, Part II</h4><p><a href="https://medium.com/@daniel.oliver.king/a-clojure-development-environment-that-gets-out-of-your-way-c11e6711ead3">&lt;&lt; Part I</a> <a href="https://medium.com/@daniel.oliver.king/getting-real-work-done-in-clojure-application-development-683c8129a313">Part III&gt;&gt;</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5gWocGVRwQjho_K5JVN37w.jpeg" /><figcaption>Photo credit: <a href="http://kryshen.net/photos/">kryshen.net/photos/</a></figcaption></figure><p>Now that we have our development environment set up, we can actually get to work and build something in Clojure.</p><p>In this article, I’ll be assuming that you’ve already gone through <a href="https://medium.com/@daniel.oliver.king/a-clojure-development-environment-that-gets-out-of-your-way-c11e6711ead3">part I of this guide</a>, so you should already have the joyful_clojure <a href="https://github.com/dking1286/joyful_clojure">example repository</a> on your local machine. If you haven’t gone through part I yet, now would be a good time.</p><p>If you forked and cloned the repository, make sure you get the most recent version by doing the following:</p><pre>cd /path/to/joyful_clojure<br>git pull upstream master</pre><p>If you downloaded the repository as a zip file, then you will need to download it again to get the most recent updates.</p><p>Let’s open the second sample project in Atom:</p><pre>cd /path/to/joyful_clojure/02_clojure_building_blocks<br>atom .</pre><p>Next, fire up a Clojure REPL in your terminal:</p><pre>lein repl</pre><p>Finally, use the “Proto Repl: Remote Nrepl Connection” command in Atom to connect to your running REPL on port 8081:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/933/1*sVcxlAMbapCnsdrE4BCyEw.png" /></figure><p>Now that we’re up and running, let’s dive in and learn how to get work done in Clojure.</p><h3>Clojure’s Built-in data structures</h3><p>To be able to get anything done in a new programming language, we have to start with the building blocks. Clojure leans heavily toward a functional programming paradigm, so the primary building blocks of Clojure programs are <strong>functions </strong>and <strong>data</strong>.</p><p>We’ve already written some functions, so let’s focus on the other building block: data.</p><h4>Primitive Data Types</h4><p>Clojure has all of the primitive data types that you would expect: numbers, strings, characters, booleans, regular expressions, and so on.</p><pre>nil ;; Clojure&#39;s equivalent of null<br>123 ;; An integer<br>1.23 ;; A double (floating point number)<br>true ;; A boolean<br>\H ;; A character<br>&quot;Hello&quot; ;; A string<br>#&quot;^Hello.*&quot; ;; A regular expression</pre><p>If you are familiar with high-level languages like Javascript, Python, or Ruby, you might be surprised that there is a distinction between a “character” and a string of length 1. Clojure runs on the Java Virtual Machine, so it inherits the semantics of strings from Java, where a string is implemented as an <em>array of characters</em> behind the scenes. Roughly speaking, the difference between \H and &quot;H&quot; is the same as the difference between 1 and [1] : A string is a <em>sequence of characters</em>, and the string &quot;H&quot; just happens to be a sequence of length 1.</p><h4>Immutable Data Structures: Clojure’s Secret Sauce</h4><p>Clojure has a rich library of standard data structures, and one of Clojure’s signature strengths is that all of them are immutable.</p><p>First, let’s look at the <strong>vector</strong>. This is a sequential data structure that is analogous to a “list” or “array” data structure in most other languages. It is represented by square brackets.</p><pre>;; A vector of integers<br>[1 2 3]</pre><p>You can look up elements by index in a vector using the get function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*QlrKW0-QciMwiTEJTLEAzQ.gif" /></figure><p>You can also append an element to a vector with the conj function, short for “conjoin”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*4gpjQN-9vTNOd0X7xMSZ2Q.gif" /></figure><p>Vectors are immutable, like all Clojure data structures. Functions like conj that maniputate vectors always return a <em>new</em> vector, rather than mutating the old one:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*z4I4WqsvW3Gbj4M6Xhi8Jg.gif" /></figure><p>Notice that my-vector is unchanged, because conj returned a new vector.</p><p>A couple of other useful functions: first returns the first element of a vector, while rest returns everything <em>except</em> the first element:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/725/1*nidGUEFKDs0l3V2Yx551-A.gif" /></figure><p>Technically, rest doesn’t return another vector, it returns a “lazy sequence”, which is represented in the console by parentheses instead of square brackets. We’ll come back to lazy sequences later.</p><p>Next the <strong>map</strong>. This is a key-value structure similar to a Python <em>dictionary</em>, Javascript <em>object</em>, or Ruby <em>hash. </em>It is represented by curly braces.</p><pre>;; A map representing a person<br>{&quot;first-name&quot; &quot;Daniel&quot;<br> &quot;last-name&quot; &quot;King&quot;}</pre><p>Something to notice: All of the data structures simply use spaces to separate the elements. Commas are treated as whitespace by the Clojure compiler, and no : is required between the keys and values in a map.</p><p>In the map above, I am using strings for the keys, but it’s possible to use any kind of data as a key:</p><pre>;; A nested map representing a collection of people by id<br>{1 {&quot;id&quot; 1<br>    &quot;first-name&quot; &quot;Daniel&quot;<br>    &quot;last-name&quot; &quot;King&quot;}<br> 2 {&quot;id&quot; 2<br>    &quot;first-name&quot; &quot;Jane&quot;<br>    &quot;last-name&quot; &quot;Smith&quot;}}</pre><p>Notice that the outer map uses integers as keys, and the inner maps use strings.</p><p>Clojure actually has another primitive data type called a <strong>keyword</strong>, which is similar to a string but used for a different purpose. Keywords are used to represent <em>names </em>that are important within the context of the program, whereas strings are usually used to represent plain text that does not refer to anything else in the program. For performance reasons, maps usually use <em>keywords</em> as keys instead of strings:</p><pre>;; A map that uses keywords as keys<br>{:first-name &quot;Daniel&quot;<br> :last-name &quot;King&quot;</pre><p>Like vectors, values can be looked up by key in a map using the get function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*j6UxYbAIfomcRp5Z17k4Xg.gif" /></figure><p>New key-value pairs can be added to a map using the assoc function, short for “associate”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*czm-tdNSAJRv7EWvo-mnDA.gif" /></figure><p>Key-value pairs can be removed from a map with the dissoc function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/694/1*Av30r5bfabTYl4jyTSChWA.gif" /></figure><p>Sometimes it can be awkward to express more complex computations on the values of a map in terms of get and assoc . For example, suppose I wanted to increment the age on a person:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*QnTEh7RWPaom25B-LIqsWQ.gif" /></figure><p>If you’re like me, you probably find that expression more difficult to read than it should be.</p><p>A much cleaner way to accomplish this is to use the update function, which allows you to pass in a <em>function</em> that takes in the old value and returns the new one:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*AGm0oXsC7FUxLXgPD8WsPg.gif" /></figure><p>Notice the fn operator, which creates an anonymous function.</p><p>We can make this even cleaner using Clojure’s built-in inc function, which simply increments a number by 1:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*f8rV5FAc8bZvdtPwPk5sJA.gif" /></figure><p>Nested maps are extremely common in Clojure, so the map functions get , assoc , and update also have versions that operate on nested maps: get-in , assoc-in, and update-in.</p><p>For example, suppose I had a map of <em>people by id</em>, and I wanted to increment the age of one specific person:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*cVD-7eJ3hoDlpWajAmf41Q.gif" /></figure><p>Notice that update-in take a <em>vector of keys</em> instead of a single key as its second argument, representing the path to the specific nested value you want to update. get-in and assoc-in work similarly. Try them out in your REPL!</p><p>Clojure also has a <strong>set</strong> data type, which represents a collection of unique elements. They are represented by curly braces with a preceding # glyph:</p><pre>;; A set of integers<br>#{1 2 3}</pre><p>Sets are <em>unordered</em>, so if you iterate through a set, you are not guarenteed to get the elements in the same order as you put them in. The advantage of sets is that you can check the presence of an element in O(1) time using the contains? function:</p><pre>;; Returns true in O(1) time<br>(contains? #{1 2 3} 3)</pre><p>If you wanted to do the same thing for a vector, you would need to iterate through the elements of the vector to find the element you’re looking for, which takes O(n) time.</p><p><em>Warning:</em> You will be disappointed with the results if you try to use the contains? function to check the presence of an element in a <em>vector</em>. contains? works on both maps and vectors, but it checks for the presence of a certain <em>key, </em>not a certain <em>value</em>. In a vector, the keys are <em>indices</em>, so the contains? function checks for the presence of a certain <em>index:</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*YnauEKfthRosmIE6Zyqc6g.gif" /></figure><p>The last core data structure in Clojure is the <strong>list</strong>, which represents sequential data stored in a linked list. A list is represented by elements inside parentheses:</p><pre>;; A list of integers<br>&#39;(1 2 3)</pre><p>Notice the single quote &#39; before the first parenthesis. This quote means “don’t try to evaluate this as code”. If we didn’t put the quote there, the Clojure compiler would try to interpret this list as a <em>function call</em> to a function named “1”. Obviously there is no such function, so an error would occur.</p><p>Usually, vectors are preferred over lists for representing sequential data. The main use case of lists is to represent <em>unevaluated code</em> when you are <em>metaprogramming:</em> Writing code that generates or manipulates other code. We’ll touch on the concept of metaprogramming again later, as it is a particular strength of languages in the Lisp family, including Clojure.</p><p>In summary: Clojure offers four main collection types: <strong>vectors</strong> for sequential data, <strong>maps</strong> for key-value pairs, <strong>sets</strong> for collections of unique elements, and <strong>lists</strong> for unevaluated code. Probably 70% or more of Clojure programming involves manipulating these four core data structures (especially vectors and maps), and the Clojure standard library has a <a href="https://clojure.org/api/cheatsheet">very rich set of generic functions</a> prebuilt for this purpose. Clojurists refer to this as “data-oriented programming”.</p><h3>Control Flow in Clojure: Avoiding Classic Mistakes</h3><p>To get any non-trivial work done in a programming language, you have to know how to use if statements, loops, and so on. Clojure handles these concepts in a different way from most other languages, so I want to help you avoid falling in to some of the common traps.</p><p>At this point, let’s switch into the main namespace where the sample code is located:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/1*67Syk4UU4RyzpCsTtJZPrw.gif" /></figure><h4>Local Variables (…but they don’t vary)</h4><p>Previously, we discussed that the def operator is used to bind names to values in Clojure:</p><pre>(def three-squared (* 3 3))</pre><p>In most languages, the same syntax is used to create global variables, and function-local variables:</p><pre>// Javascript</pre><pre>// Global variable<br>const theNumber = 3;</pre><pre>function add7(num) {<br>  // Local variable<br>  const answer = num + 7;<br>  return answer;<br>}</pre><p>However, if you try to do the same thing in Clojure, you will be disappointed with the results. The def operator binds a name to a value at the <em>namespace</em> scope, so if you use it inside a function, the value of that name that you set <em>inside</em> the function will be visible to all of the other functions in that namespace. This is probably not what you want, and it is <em>exceedingly</em> rare to do so in practice.</p><p>To create function-local bindings, use the let operator:</p><pre>// Javascript<br>function add7(num) {<br>  const answer = num + 7;<br>  return answer;<br>}</pre><pre>;; Clojure<br>(defn add-7<br>  [num]<br>  (let [answer (+ num 7)]<br>    answer))</pre><p>Notice that the return value of the function is placed <em>inside</em> the parentheses of the let block, because the local bindings are only visible inside that block. If you try to use a local binding outside of the let block where it was defined, you will get an error like Unable to resolve symbol: answer in this context:</p><pre>(defn add-7<br>  [num]<br>  (let [answer (+ num 7)]) ;; WRONG, the let block ends here<br>  answer)</pre><p>You can declare as many local bindings as you want, and each one can depend on previously-declared bindings:</p><pre>(defn hypotenuse<br>  &quot;Calculates the hypotenuse of a right triangle&quot;<br>  [side1 side2]<br>  (let [a-squared (* side1 side1)<br>        b-squared (* side2 side2)<br>        c-squared (+ a-squared b-squared)]<br>    (Math/sqrt c-squared)))</pre><h4>Conditional logic</h4><p>I mean, you can’t have a programming language without if , right?</p><p>Clojure has an if expression, as you would expect. It works roughly the same as the ternary operator ? in other languages:</p><pre>;; Determines if a number is even<br>(defn even?<br>  [num]<br>  (if (= (mod num 2) 0)<br>    true<br>    false))</pre><p>Notice the general format: (if condition branch-1 branch-2) . The entire expression evaluates to branch-1 if the condition is true, and branch-2 if not.</p><p>Hold on, though, because there are a few gotchas that can trip you up if you’re coming from other languages.</p><p><strong>Gotcha #1</strong><em>: </em>Truthiness</p><p>Many languages have a notion of “truthiness”, where they treat certain values as being “true” or “false” when used in a logical expression. Annoyingly, languages disagree as to which values are “truthy”. For example, Python and Javascript agree that 0 is falsey and all other numbers are truthy, but Python treats an empty list [] as falsey, whereas Javascript treats it as truthy.</p><p>Clojure takes a (subjectively) more principled view on this matter: false and nil are falsey, and <em>anything else </em>is truthy. This can cause some unexpected behavior if you are used to relying on the quirks of other languages:</p><pre>(if number-of-users<br>  (println &quot;We have users&quot;)<br>  (println &quot;We have no users&quot;))</pre><p>The example above would always print “We have users”, even if number-of-users is 0. The correct way would be to explicitly check if number-of-users is non-zero:</p><pre>(if (&gt; number-of-users 0)<br>  (println &quot;We have users&quot;)<br>  (println &quot;We have no users&quot;))</pre><p><strong>Gotcha #2: </strong>Doing multiple things on a branch</p><p>One limitation of Clojure’s if expression is that you can only put <em>one expression</em> on the if-branch and the else-branch. This usually isn’t a problem, but on rare occasions, you want to be able to do multiple things on one of the branches.</p><p>Suppose you wanted to be able to print out 2 different messages on the else-branch. Naively, I might try something like this:</p><pre>;; This is wrong<br>(if (&gt; number-of-users 0)<br>  (println &quot;We have users&quot;)<br>  (println &quot;We have no users&quot;)<br>  (println &quot;We should probably go find some.&quot;))</pre><p>Unfortunately, this results in an error, because the if expression is only expecting 3 arguments: The condition, the if-branch, and the else-branch.</p><p>To remedy this, we can use the do operator, which combines multiple expressions into one:</p><pre>;; This is correct<br>(if (&gt; number-of-users 0)<br>  (println &quot;We have users&quot;)<br>  (do<br>    (println &quot;We have no users&quot;)<br>    (println &quot;We should probably go find some.&quot;)))</pre><p>This is pretty rare in practice, but still useful to be aware of.</p><p><strong>Gotcha #3:</strong> More than 2 branches</p><p>In many languages, you can do something like this if you need more than 2 branches in your logic:</p><pre>// Javascript<br>function isTeenager(age) {<br>  if (age &lt; 13) {<br>    return false;<br>  } else if (age &gt; 19) {<br>    return false;<br>  } else {<br>    return true;<br>  }<br>}</pre><p>In Clojure, trying to do the same thing leads to many levels of nesting:</p><pre>(defn is-teenager<br>  [age]<br>  (if (&lt; age 13)<br>    false<br>    (if (&gt; age 19)<br>      false<br>      true)))</pre><p>This works, but it’s not very easy to read. Clojure provides a convenient operator called cond which allows us to express any number of logical branches without going overboard on nesting:</p><pre>(defn is-teenager<br>  [age]<br>  (cond<br>    (&lt; age 13) false<br>    (&gt; age 19) false<br>    :else true))</pre><p>Using cond is completely equivalent to nested if expressions, but it’s convenient nonetheless.</p><h4>Loops and iteration</h4><p>Now for the real meat. This is where Clojure diverges the most from other languages.</p><p>Suppose that we have a list of users, and we want to get the ids of all the users who are 21 or older. We would most likely represent the list as a <em>vector of maps</em>:</p><pre>(def users<br>  [{:id 1 :age 31<br>    :first-name &quot;Daniel&quot; :last-name &quot;King&quot;}<br>   {:id 2 :age 16<br>    :first-name &quot;Angel&quot; :last-name &quot;Herrera&quot;}<br>   {:id 3 :age 31<br>    :first-name &quot;Jane&quot; :last-name &quot;Smith&quot;}<br>   {:id 4 :age 20<br>    :first-name &quot;Ruth&quot; :last-name &quot;Langley&quot;}])</pre><p>We define a function get-eligible-user-ids , which would ideally return the vector [1 3] if we pass in these users.</p><p>In most languages, we would probably do something like this:</p><ol><li>Create an empty list to hold the results</li><li>Iterate through the list of users with a for loop or something similar</li><li>If the user’s age is at least 21, push the user’s id into the results list</li><li>Once the loop completes, return the results list</li></ol><p>Something like this:</p><pre>// Javascript<br>function getEligibleUserIds(users) {<br>  const results = [];<br>  for (const user of users) {<br>    if (user.age &gt;= 21) {<br>      results.push(user.id);<br>    }<br>  }</pre><pre>  return results;<br>}</pre><p>Clojure has an operator called doseq (short for do-sequence) which does roughly the same thing as a for-in or for-of loop in most languages (side note: Clojure also has a for operator, but it does something else). To get started, we could try just printing out the id of each user:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g9P9fp2Sj5fvEMZpH_h7DA.gif" /></figure><p>So far so good. As a next step, we can try to emulate the Javascript solution using a combination of let , doseq , if , and conj (spoiler alert: this approach looks reasonable, but it is <em>wrong</em>):</p><pre>;; WARNING: This is wrong<br>(defn get-eligible-user-ids<br>  [users]<br>  (let [results []]<br>    (doseq [user users]<br>      (if (&gt;= (get user :age) 21)<br>        (conj results (get user :id))))<br>    results))</pre><p>But then, when I try the function, I get an empty vector [] instead of the result I wanted, [1 3]:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*67fIgYJt69eUv956eaWILw.gif" /></figure><p>The problem is that the results vector is immutable! When we try to add an id into results using conj , we are actually creating a new vector, leaving results unmodified. Therefore, when we return results at the end, it is still empty.</p><p>Most languages rely on mutability to accomplish basic tasks. Clojure’s focus on immutability is one of its best qualities, but it requires that we rethink our approach. Instead of using imperative loops to accomplish this task, we are going to use <em>recursion</em>.</p><p>Clojure provides a structure called loop/recur which allows us to perform a recursive computation. To print out the ids of every user in our list, we would do something like this:</p><pre>(loop [remaining users]<br>  (if (empty? remaining)<br>    nil<br>    (let [user (first remaining)]<br>      (println (get user :id))<br>      (recur (rest remaining)))))</pre><p>There’s a lot going on here, so let’s break it down.</p><ol><li>The loop operator defines a local name remaining and initializes this name to the value users , exactly like the let form would do.</li><li>We check to see if there are any users left in remaining . If not, we are done, and we can return nil .</li><li>Otherwise, we take the first user from remaining and bind it to the name user</li><li>We print out the id of the user.</li><li>We call the recur operator to jump back to the beginning of the loop. The next time through the loop, the local names established at the beginning are bound to the values that we pass into recur . Here, the current value of (rest remaining) is bound to the name remaining on the next iteration.</li></ol><p>Using this structure, we can write a correct version of the get-eligible-user-ids function:</p><pre>(defn get-eligible-user-ids-1<br>  [users]<br>  (loop [remaining users<br>         results []]<br>    (if (empty? remaining)<br>      results<br>      (let [user (first remaining)<br>            next-remaining (rest remaining)<br>            next-results (if (&gt;= (get user :age) 21)<br>                           (conj results (get user :id))<br>                           results)]<br>        (recur next-remaining next-results)))))</pre><p>Thankfully, this gives the correct results (try it in your REPL to be sure).</p><p>At this point, you might be thinking that this is a <em>lot of work</em> to get a simple result.</p><p>You are correct! loop/recur is there when you need it, but it’s rare to need it in practice. The Right Way to do this particular task in Clojure is to use map and filter .</p><p>In Javascript, you can accomplish this task by doing the following:</p><pre>function getEligibleUserIds2(users) {<br>  return users<br>    .filter((user) =&gt; user.age &gt;= 21)<br>    .map((user) =&gt; user.id);<br>}</pre><p>It looks much the same in Clojure:</p><pre>(defn get-eligible-user-ids-2<br>  [users]<br>  (map (fn [user] (get user :id))<br>       (filter (fn [user] (&gt;= (get user :age) 21)) users)))</pre><p>Notice that the map and filter functions take the sequence to be operated on as the <em>last</em> argument, like (filter predicate sequence). As a result, the Clojure version needs to be read inside-out, like a math expression, as opposed to the Javascript version, which can be read top-to-bottom.</p><p>If this bothers you (as it probably does), then hang on: By the time we’re done, it’s going to be much more readable.</p><p>One interesting thing about Clojure’s implementation of map and filter is that they return <em>lazy sequences, </em>rather than concrete vectors. This means that even if you chain multiple calls to map and filter , none of the computation is actually done until you try to look at the elements in the resulting sequence. Therefore, chaining many calls to map and filter is much more computationally efficient in Clojure than it is in Javascript, where each intermediate map or filter immediately transforms the entire list, resulting in a lot of extra arrays being created and then thrown away.</p><p>On the other hand, there are times when you don’t <em>want </em>a lazy sequence, you want a concrete vector. In these cases, you have a couple of options.</p><p><em>Option 1: </em>You can use reduce to manually construct an output vector:</p><pre>(defn get-eligible-user-ids-3<br>  [users]<br>  (reduce conj []<br>          (map (fn [user] (get user :id))<br>               (filter (fn [user] (&gt;= (get user :age) 21)) users))))</pre><p><em>Option 2:</em> You can use the (into collection sequence) function, which iterates through sequence and appends each element to collection:</p><pre>(defn get-eligible-user-ids-4<br>  [users]<br>  (into []<br>        (map (fn [user] (get user :id))<br>             (filter (fn [user] (&gt;= (get user :age) 21)) users))))</pre><p>This will <em>iterate </em>through the lazy sequence produced by the chained map and filter calls, and append each element to the (initially) empty vector [].</p><p>Option 2 is more idiomatic, both because it is simpler, and because into has some internal optimization which makes it perform better than reduce in most cases.</p><p>This works, but it’s getting hard to read again, due to all the nesting. Luckily, it is possible to write this in a top-to-bottom fashion using the magic of <em>threading macros</em>.</p><h3>Prettifying the Code With Threading Macros</h3><p>Earlier, we very briefly touched on the idea of <em>metaprogramming</em>, or writing code that generates or manipulates other code. Clojure primarily implements this idea with <em>macros</em>, which are functions that transform unevaluated code.</p><p>We’re not going to be writing any macros yet, but I want to explore a couple of the macros that are provided in the Clojure standard library, since they are so commonly used. In particular, I want to look at the <em>threading macros</em>, which allow us to write code in a more readable, top-to-bottom style.</p><p>First, let’s look at the <em>thread-last </em>macro, represented by the -&gt;&gt; operator (yes, it’s an arrow with two arrowheads).</p><p>Try evaluating the following in your REPL:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*2o-vG_mdaHtbYn4zDh7wUA.gif" /></figure><p>What the heck is going on here? Well, remember that the word “REPL” stands for “read-eval-print loop”, because when you enter some text into the REPL, the Clojure compiler first calls read-string to transform the text into <em>unevaluated code</em>, then calls eval on the unevaluated code to run it on the JVM and produce a result, then calls print to print the result out in the terminal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*F3LP2WD_V8hSp5Ig8ApIxw.png" /></figure><p>There is actually a <em>fourth step</em> in this process, called <em>macro expansion</em>, where any macros present in the code are evaluated. Since macros are <em>functions that transform unevaluated code</em>, the macro expansion step occurs <em>between</em> the read and eval steps, while the code is still unevaluated:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*TWaXkK0LEuSnuBtcz2gMnw.png" /></figure><p>I guess the acronym RMEPL never really caught on.</p><p>We can isolate the macro expansion step by using the macroexpand operator, which will help us understand what’s really going on with the -&gt;&gt; macro (notice that I put a &#39; before the -&gt;&gt; expression inside of macroexpand):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*wo_gzKhDqOFRyuAwDiCONA.gif" /></figure><p>Notice that the -&gt;&gt; macro takes the first expression and <em>inserts it as the last argument to the next expression</em>.</p><p>You can also call the -&gt;&gt; macro with more than 2 arguments:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*2E5p9eaWFTFWfgD9QJbYhg.gif" /></figure><p>Notice that the first expression is inserted as the <em>last argument</em> to the next expression, then that is inserted as the <em>last argument</em> to the following expression.</p><p>Here’s the key takeaway: We can use the -&gt;&gt; macro to <strong>rewrite nested function calls in a sequential, top-to-bottom way</strong>.</p><p>This is exactly what we need to make the get-eligible-users function more readable.</p><p>Here’s what the get-eligible-users function would look like if we re-write it with the thread-last macro:</p><pre>(defn get-eligible-user-ids-5<br>  [users]<br>  (-&gt;&gt; users<br>       (filter (fn [user] (&gt;= (get user :age) 21)))<br>       (map (fn [user] (get user :id)))<br>       (into [])))</pre><p>Really pause and make sure you understand what’s going on here. I’ll put in some commas to make it a bit clearer (remember, the Clojure compiler ignores commas):</p><pre>(defn get-eligible-user-ids-5<br>  [users]<br>  (-&gt;&gt; users<br>       (filter (fn [user] (&gt;= (get user :age) 21)) ,,,,)<br>       (map (fn [user] (get user :id)) ,,,,)<br>       (into [] ,,,,)))</pre><p>The -&gt;&gt; macro takes the first expression, and inserts it as the last argument in the second expression (where the commas are), which is then inserted as the last argument of the third expression, and so on.</p><p>The result is that the body of the function is <em>completely equivalent</em> to get-eligible-user-ids-4, where we nested the map and filter calls:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/694/1*UEffgWidZGV5AAFXkzxHmw.gif" /></figure><p>The thread-last -&gt;&gt; macro is extremely common in Clojure code, so it’s really really important to get comfortable with it.</p><p>There is also a <em>thread-first</em> macro, represented by the -&gt; operator, which inserts each expression as the <em>first </em>argument of the next expression:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/694/1*BK9aTpF5oKMF0asklKLyXA.gif" /></figure><p>This macro is useful when you want to make multiple updates to a data structure in one operation, as in this add-new-user function:</p><pre>(def users-table<br>  {:users-list [&quot;123&quot; &quot;456&quot;]<br>   :users-by-id {&quot;123&quot; {:id &quot;123&quot;<br>                        :first-name &quot;Daniel&quot;}<br>                 &quot;456&quot; {:id &quot;456&quot;<br>                        :first-name &quot;Jane&quot;}}})</pre><pre>(defn add-new-user<br>  [users-table user]<br>  (let [id (get user :id)]<br>    (-&gt; users-table<br>        (assoc-in [:users-by-id id] user)<br>        (update :users-list conj id))))</pre><h3>Putting it all together</h3><p>Okay…that’s enough for now.</p><p>Before we move on, it’s important to practice what you’ve learned.</p><p>There are a few exercises in the main namespace in the 02_clojure_building_blocks project. Try applying what you’ve learned to create those functions.</p><p>The solutions are in the solutions namespace. You know the drill…don’t look at the solutions before you’ve given the exercises a try!</p><p>However, <em>do</em> look at the solutions once you’re finished. I included several solutions to some of the question, to demonstrate some common Clojure idioms that we didn’t have time to discuss in this article.</p><p>Some hints to get you started:</p><p>The <a href="https://clojure.org/api/cheatsheet">Clojure cheatsheet</a> is a super-useful resource. I recommend having it open at all times (but don’t worry if you don’t understand everything in there yet)</p><p>Clojure has a built-in library called clojure.string, which contains a number of <a href="https://clojure.github.io/clojure/clojure.string-api.html#clojure.string/index-of">useful functions for working with strings</a>. This library is imported at the top of the main namespace with the following invocation:</p><pre>(ns main<br>  (:require [clojure.string :as string]))</pre><p>This allows you to call functions in this library by prefixing them with string/:</p><pre>(string/upper-case &quot;hello&quot;)<br>=&gt; &quot;HELLO&quot;</pre><p>In the next article, we’ll move beyond the basic features of the Clojure <em>language</em> and learn how to actually build a useful application with the Clojure <em>ecosystem</em>.</p><p><a href="https://medium.com/@daniel.oliver.king/a-clojure-development-environment-that-gets-out-of-your-way-c11e6711ead3">&lt;&lt; Part I</a> <a href="https://medium.com/@daniel.oliver.king/getting-real-work-done-in-clojure-application-development-683c8129a313">Part III&gt;&gt;</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=39ad82796926" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Clojure Development Environment That Gets Out of Your Way]]></title>
            <link>https://medium.com/@daniel.oliver.king/a-clojure-development-environment-that-gets-out-of-your-way-c11e6711ead3?source=rss-8dad49c4d059------2</link>
            <guid isPermaLink="false">https://medium.com/p/c11e6711ead3</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming-languages]]></category>
            <category><![CDATA[clojure]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Daniel King]]></dc:creator>
            <pubDate>Sat, 06 Oct 2018 21:19:52 GMT</pubDate>
            <atom:updated>2019-02-10T18:02:57.649Z</atom:updated>
            <content:encoded><![CDATA[<h4>A Joyful Introduction to Clojure, Part I</h4><p><a href="https://medium.com/@daniel.oliver.king/getting-work-done-in-clojure-the-building-blocks-39ad82796926">Part II &gt;&gt;</a></p><p>One of the hardest things about getting started with Clojure is setting up a development environment that allows you to get the most out of the language with a minimum of fuss.</p><p>For starters, you have to choose an editor. The most popular choices in the Clojure world are <a href="https://www.gnu.org/software/emacs/">Emacs</a>, <a href="https://www.vim.org/">Vim</a>, and <a href="https://www.jetbrains.com/idea/">IntelliJ</a>. Most Clojure tutorials will recommend one of these, because the ecosystem of Clojure tools and plugins around these editors is extremely high quality and mature.</p><p>We’re not going to use those. Instead, we’re going to use <a href="https://atom.io/">Atom</a>, because</p><ol><li>It is extremely straightforward to download and install.</li><li>It is extremely usable without learning lots of new concepts and hotkeys — it will let you focus on learning Clojure, not your editor.</li><li>Of the popular lightweight editors (<a href="https://www.sublimetext.com/">Sublime</a>, <a href="https://atom.io/">Atom</a>, and <a href="https://code.visualstudio.com/">VS Code</a>), Atom has the best Clojure support, due to the awesome <a href="https://atom.io/packages/proto-repl">Proto REPL</a> project.</li></ol><p>Next, you have to choose a command line tool. There are three popular options here: <a href="https://leiningen.org/">Leiningen</a>, <a href="http://boot-clj.com/">Boot</a>, and the new <a href="https://clojure.org/guides/getting_started">official Clojure CLI</a>. Most Clojure tutorials will recommend Leiningen, because it has been the build tool of choice for most of Clojure’s history, and it’s ecosystem of tools and plugins is extremely high quality and mature.</p><p>In this case, we’re going to go with the conventional wisdom and use Leiningen.</p><p>Let’s dive in.</p><p><em>Disclaimer: The specific steps described in this guide are geared toward users on Unix-like systems. If you are on Windows, you will need to adapt file paths and installation instructions for your own system. The Clojure parts should be the same, though.</em></p><p><em>Update 11/02/2018: A previous version of this article recommended the Clojure CLI, but I decided to revert back to using Leiningen because it will make our lives easier down the line when we do more advanced project configuration.</em></p><h3>Step 0: Installing tools</h3><p>Leiningen and Clojure both require that Java is installed on your machine. You can check this by typing which java in your terminal. If you get an output like /usr/bin/java , you are good to go. Otherwise, you will need to install <a href="https://openjdk.java.net/install/">OpenJDK 8</a> on your machine.</p><p>The <a href="https://leiningen.org/">Leiningen website</a> has manual installation instructions for Leiningen. If you’d rather, you can also install Leiningen through the package manager on your system (for example, on Mac you can use brew install leiningen ).</p><p>Once you have installed Leiningen, you can check to see if everything went according to plan by typing which lein in your terminal. You should get an output like /bin/lein (or wherever your Leiningen executable is located).</p><h3>Step 1: Baby’s First Clojure</h3><p>At the most basic level, the Clojure development environment is composed of two tools:</p><ol><li>The Clojure compiler, which translates Clojure code into bytecode for the JVM (Java Virtual Machine)</li><li>The Clojure REPL (Read-Eval-Print Loop), which allows you to interactively work with the Clojure compiler. Expressions typed into the REPL are compiled on the fly, the resulting bytecode is executed on the JVM, and then the outputs are printed back in the REPL prompt.</li></ol><p>Leiningen is essentially a wrapper that manages the low-level details of these tools for you. When you use Leiningen, you are just using the Clojure compiler and REPL behind the scenes, without needing to fuss with configuration details.</p><p>Let’s write your first Clojure code! Fire up an interactive Clojure REPL session by typing lein repl in your terminal. You should see a prompt like:</p><pre>nREPL server started on port 34616 on host 127.0.0.1 - nrepl://127.0.0.1:34616<br>REPL-y 0.3.7, nREPL 0.2.12<br>Clojure 1.9.0<br>OpenJDK 64-Bit Server VM 1.8.0_181-8u181-b13-1ubuntu0.16.04.1-b13<br>    Docs: (doc function-name-here)<br>          (find-doc &quot;part-of-name-here&quot;)<br>  Source: (source function-name-here)<br> Javadoc: (javadoc java-object-or-class-here)<br>    Exit: Control+D or (exit) or (quit)<br> Results: Stored in vars *1, *2, *3, an exception in *e</pre><pre>user=&gt;</pre><p>One of the great things about Clojure is that its syntax is <em>ridiculously</em> consistent. Every operation, every function call, every instruction follows the exact same format:</p><pre>(operator argument argument [...more arguments])</pre><p>If you’re used to other languages, this can look strange until you get used to it. For example, to add 2 plus 2, we do this:</p><pre>(+ 2 2)</pre><p>Notice that the operator comes first, as it always does in Clojure. Try typing this in and evaluating it in your REPL prompt:</p><pre>user=&gt; (+ 2 2)<br>4</pre><p>Well, at least the Clojure compiler can do math correctly.</p><p>In any programming language, we need a way to bind names to values. Many languages use the = operator for this purpose, but Clojure calls this operation def instead:</p><pre>user=&gt; (def my-name &quot;Daniel King&quot;)<br>#&#39;user/my-name</pre><p>Once you have done this, the symbol my-name is bound to the string value that you provided:</p><pre>user=&gt; my-name<br>&quot;Daniel King&quot;</pre><p>As you would expect, we can also define our own functions. This is done with the defn operator, whose usage looks like this:</p><pre>(defn function-name [arguments] body)</pre><p>Let’s create a function in your REPL (make sure you get all the parentheses in the right places):</p><pre>user=&gt; (defn hello-message [name] (str &quot;Hello &quot; name))<br>#&#39;user/hello-message</pre><p>We can then call your function, and pass in the my-name value you defined earlier as an argument:</p><pre>user=&gt; (hello-message my-name)<br>&quot;Hello Daniel King&quot;</pre><p>A few things to notice about this:</p><ol><li>Clojure has lots of nested parentheses. It’s a pain to type all the close-parens in the right places, but your editor will take care of that for you once we set it up.</li><li>The str function concatinates strings together, similar to how the + operator works on strings in many other languages.</li><li>There is no return keyword in Clojure. A function automatically returns the value of the last expression in the function body, in this case the concatination of &quot;Hello “ and my-name .</li><li>I have been carefully avoiding using the word “variable”. In most languages, doing num = 3 is called “assigning a value to a variable”. In Clojure, doing (def num 3) is known as “binding a name to a value”. The symbol num is not a “variable”, because it will not vary once it is initially bound.</li></ol><p>We’re done with the REPL for now, so go ahead and stop it by typing quit. Now that we have the Clojure compiler up and running, let’s get the rest of the development environment set up.</p><h3>Step 2: Setting up Atom for Clojure development (an opinionated guide)</h3><p>You will not be surprised to learn that the first step is to <a href="https://atom.io/">download and install Atom</a> if you don’t already have it.</p><p>Once you have Atom installed, we need to do some customizing.</p><h4>Intalling Atom packages</h4><p>Open the settings page by going to Edit &gt; Preferences, and click on the Install tab on the left side of the screen:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ESlL-mOeGbUVHYoAWvuyhg.png" /><figcaption>Installing packages in Atom</figcaption></figure><p>You will need to install 3 packages: ink, lisp-paredit, and proto-repl. Once these are installed, restart Atom.</p><h4>Tweaking package settings</h4><p>Go to the Packages tab, search for “bracket-matcher”, and open the settings page for the bracket-matcher plugin:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AxAfenEPtvIHTFw6bOfe5Q.png" /></figure><p>Find the “Autocomplete Characters” option, and remove the single apostrophes and backticks. It should look like this when you’re done:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/615/1*1o-mM8L0j6gm3vXxh5m1kw.png" /></figure><p>We did this because Clojure uses individual apostrophes and backticks, unlike most languages which use them in matching pairs.</p><p>Next, open the settings for the proto-repl plugin:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4C232rOqaCaJGLz3Ztv42Q.png" /></figure><p>Uncheck the “Ink Console” option. This turns off some of the fancy graphical effects for Proto REPL, but improves the functionality. We’re here to get work done, not marvel at pretty graphics.</p><h4>Adding local dependencies to Leiningen</h4><p>To allow Proto REPL to communicate with your Clojure project, we have to declare a dependency on the proto-repl library.</p><p>Find the file ~/.lein/profiles.clj , or create it if it doesn’t exist. Then, copy-paste the following data into this file:</p><pre>{:user {:dependencies [[proto-repl &quot;0.3.1&quot;]]}}</pre><p>Let’s break this down a bit.</p><p>In Leiningen, configuration is done through the use of “profiles”, which allow you to collect groups of configuration options under a name. Then, when you run Leiningen, you specify which profiles you want to use. Leiningen will then merge all of the configuration options contained in those profiles.</p><p>Normally, Leiningen profiles are defined in the configuration files for a single project. However, Leiningen will also look in ~/.lein/profiles.clj for available profiles, which makes this a good place to put dependencies needed by the development environment on your local machine.</p><p>There is a special profile called user which is always turned on by default. In the configuration above, we are declaring that when the user profile is used, we should include a dependency on proto-repl version 0.3.1.</p><p>The result: Whenever you are working on <em>any</em> Clojure project on your local machine, Leiningen will <em>always</em> include a dependency on proto-repl version 0.3.1.</p><p>Try running lein repl again in your terminal. You should see the new dependency being downloaded before the REPL starts. Then, you can stop the REPL by running quit.</p><p><strong>Story time (a cautionary tale): </strong>When I was first learning Clojure, I went through a tutorial that told me to declare a dependency in ~/.lein/profiles.clj like we’ve done here. I then forgot that it was possible to declare dependencies there. About a year later, I was working on a side project in Clojure, and I started getting strange errors whenever I tried to start my REPL. Ultimately, after a couple of hours of searching, I happend to stumble across ~/.lein/profiles.clj and noticed that the dependency it declared was <em>out of date</em>. The errors were happening because one of the newer libraries I was using in my project was incompatible with the old dependency version declared in ~/.lein/project.clj . If I had remembered that ~/.lein/profiles.clj existed, it would have taken me minutes, not hours, to find this issue and fix it.</p><p><strong>The moral of the story: </strong>Remember that ~/.lein/profiles.clj exists, so that you can update those dependencies to newer versions as necessary.</p><p>At this point, Atom is fully set up for Clojure development. Let’s work on something bigger, so you can see how all the tools fit together.</p><h3>Step 3: A full Clojure project</h3><p>The code samples for this series of articles live in <a href="https://github.com/dking1286/joyful_clojure">this Github repository</a>. Get it onto your local machine by doing one of the following:</p><ol><li>Fork and clone the repository (preferred method)</li><li>Download the repository as a zip file (alternate method if you are not confident with Git and Github)</li></ol><p>If you forked and cloned the repository, make sure that you also set my version of the repository as a remote, so that you can pull updates as necessary:</p><pre>cd /path/to/joyful_clojure<br>export CLJ_URL=<a href="https://github.com/dking1286/joyful_clojure.git">https://github.com/dking1286/joyful_clojure.git</a><br>git remote add upstream $CLJ_URL</pre><p>Then, use Atom to open sample project 1:</p><pre>atom /path/to/joyful_clojure/01_clojure_development_environment</pre><p>Let’s look at a couple of the files before we start changing things. First, the project.clj file:</p><pre>(defproject joyful-clojure-01 &quot;0.1.0-SNAPSHOT&quot;<br>  :dependencies [[org.clojure/clojure &quot;1.9.0&quot;]]<br>  :source-paths [&quot;src&quot;]<br>  :repl-options {:port 8081})</pre><p>The project.clj file is the main configuration file for Leiningen. It is roughly like the package.json in a Javascript project: It declares the dependencies for the project, and includes some other important configuration information.</p><p>The :source-paths key specifies where the Clojure compiler will look for the source files in this project. In almost all Clojure projects, a directory called src is used for this purpose.</p><p>As in the ~/.lein/profiles.clj file, the :dependencies key shows the dependencies of the project. Right now, the only dependency we have declared is the Clojure compiler itself, version 1.9.0.</p><p>We’ll come back to the :repl-options key in a moment.</p><p>Next, open the src/main.clj file:</p><pre>(ns main)</pre><pre>(def my-name &quot;Daniel King&quot;)</pre><pre>(defn hello-message<br>  [name]<br>  (str &quot;Hello &quot; name))</pre><pre>(defn -main<br>  [&amp; args]<br>  (println (hello-message my-name)))</pre><p>Every Clojure file starts with a call to the ns operator, which declares the “namespace” created by the file. The namespace must match the name of the file.</p><p>Notice that we are defining the same my-name and hello-message values that we defined in the terminal REPL earlier. We have also added a -main function, which is the entry point to the entire project when we compile and run it.</p><p>In your terminal, try running the following:</p><pre>lein run -m main</pre><p>You should see something like Hello Daniel King printed in your terminal.</p><p>Here’s what just happened: When we use the command lein run , Leiningen will compile and run the whole project. It does so by performing the following actions:</p><ol><li>Look at the namespace specified by the -m command line option. This namespace will be the entry point of the project. In our case, this is the main namespace declared in main.clj.</li><li>Find the function named -main in the namespace declared in step 1.</li><li>Compile the whole project and run the -main function, passing in any arguments given on the command line.</li></ol><p>Now that we have a working project, the last step is to connect Proto REPL to your project. This is where the magic happens.</p><h3>Step 4: Live development with Proto REPL</h3><p>Use lein repl to start a REPL in your terminal again. When the REPL starts, notice the following line that gets printed:</p><pre>nREPL server started on port 8081 on host 127.0.0.1</pre><p>nREPL is short for “network REPL”, and it is a protocol that allows other processes to send commands to a Clojure REPL through a network port.</p><p>This opens up lots of interesting possibilities: You could expose an nREPL port on an application in production, and then use it to debug your application in real time while it is running.</p><p>Clojure-aware editors also make use of nREPL. Instead of attempting to parse your source code to provide editing features like context-aware autocomplete, the editor just connects to a running REPL through an nREPL port, and then uses that connection to determine everything it needs to know about the project.</p><p>Remember that we put :repl-options {:port 8081} in your project.clj file. When you start a REPL with lein repl , the nREPL server will listen for connections on this port.</p><p>Let’s connect Atom to the nREPL server listening on port 8081. With the 01_clojure_development_environment project open in Atom, open the Atom command palette (Ctrl+Shift+P on Linux, probabyl Cmd+Shift+P on Mac) and search for the command “Proto Repl: Remote Nrepl Connection”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0u0ikwg8VM38QxQ117yjBQ.png" /></figure><p>Run this command, and Atom will prompt you to choose a host and port for the nREPL client. It should be able to auto-detect the correct host and port of the nREPL server, but if not, you can enter them manually:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W0oM3tkTpzP7gCzajldkrw.png" /></figure><p>Press enter, and you should see a Clojure REPL window pop up:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*plfowlkRrKzqgjkHaIF9PA.png" /></figure><p>In this REPL window, you can evaluate Clojure code, just like we were doing earlier in the terminal (note that you use Shift+Enter to evaluate code):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/784/1*F-9fymNQx521NHWF8utAnw.gif" /></figure><p>That’s all well and good, but we haven’t yet done anything that you couldn’t do in your terminal. The real magic is that you can also navigate around your project’s source code in this REPL.</p><p>The REPL always starts in a namespace called user by default. Notice the user=&gt; that appears each time you evaluate code in the REPL — this means that the REPL is evaluating code in the context of the user namespace. However, our code is currently located in the main namespace, as indicated by the ns operator in our source file. Therefore, the REPL cannot currently “see” our source code, because the REPL is looking at a different namespace.</p><p>To navigate the REPL into the main namespace where our code is located, enter (in-ns &#39;main) in the REPL (note the single apostrophe before the namespace name — we’ll come back to what this means later):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ntDAPNdscGZnMdt7H_52GA.gif" /></figure><p>You should see the user=&gt; change to main=&gt; indicating that the REPL will now evaluate code in the context of the main namespace defined by our source file. Once you’ve done this, the REPL has access to all of the values and functions defined in this source file:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tPYBrzmbuZDtfQRgmzUjkA.gif" /></figure><p>This allows us to quickly run our functions as we’re developing them, observe their behavior, and correct them. It allows us to be intimately connected to the code in a way that most other tools can’t match, because the feedback loop is so wonderfully, beautifully short.</p><p>One last thing: If you change your code, the REPL can immediately refresh and pick up those changes. However, you need to tell it when you want it to refresh. Luckily, Proto REPL has a command called “Refresh namespaces” that allows us to do this easily (on my machine, the hotkey is Ctrl+, r. Look in the command palette to check the hotkey on your system).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*shq2Q4FS4vhwfr1ulh4XDA.gif" /></figure><p>With all this in mind, here’s a summary of the end-to-end workflow:</p><ol><li>Start a REPL in your terminal with lein repl</li><li>Open your project in Atom, and connect to your REPL using “Proto Repl: Remote Nrepl Connection”</li><li>Navigate the REPL into the namespace you are working on with in-ns</li><li>Add new functions and values, or change the existing ones</li><li>Refresh the REPL using “Proto Repl: Refresh Namespaces”</li><li>Try out the functions in the REPL to get feedback on your work</li><li>Repeat 4, 5, and 6 until the work is done</li></ol><h3>Frequently asked questions</h3><p><strong>Q: </strong><em>This REPL-driven development thing seems pretty cool, but I like to do TDD. When should I use the REPL in development, vs. using a test runner like I would in another language?</em></p><p><strong>A</strong>: The awesome thing is that you don’t have to choose: The REPL <em>is </em>your test runner during development. I’ll show how this works in a later article in this series, but for now, note that Proto REPL has commands called “Run tests in current namespace” and “Run all tests”. When I’m developing Clojure, I typically use these commands to run my tests, rather than relying on an external test runner that watches my files for changes.</p><p>Leiningen can also run the automated tests for your project using the lein test command. This command is mostly used to run tests in automated environments, like Continuous Integration pipelines, rather than in your local development environment.</p><p><strong>Q:</strong> <em>No, seriously, what’s the deal with the single quote in </em>(in-ns &#39;main) <em>? It’s confusing, and unlike anything I’ve seen in any other language.</em></p><p><strong>A:</strong> This opens up a very deep conversation on the “metaphysics” of Clojure, but I’ll try to give you a sense of what it means without getting too far into the weeds. Feel free to skip this answer and come back to it later, if it suits you.</p><p>Many programming languages have a function called eval or something similar, which allows you to take a piece of <em>data</em> and interpret it as executable code. For example, in Javascript, the eval function takes a string and executes it as code:</p><pre>const unevaluated = &quot;console.log(&#39;Hello world&#39;)&quot;;<br>eval(unevaluated) // Prints &quot;Hello world&quot; to the console</pre><p>Clojure also has an eval function, like all Lisp dialects. However, it also has the “quote” operator, which is the opposite of eval : It means “don’t evaluate this”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*JFyqsLA3MBBBjOyNQWv85w.gif" /></figure><p>Notice that when I entered (+ 2 2) into my REPL, the expression was evaluated to 4 , but when I entered &#39;(+ 2 2) , the result was the <em>unevaluated</em> list (+ 2 2) .</p><p>The equivalent of the Javascript code above in Clojure would look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*BFOi4Vl6VePtapc9E2koHw.gif" /></figure><p>One last thing to notice before we table this discussion for later: In Clojure, there is a distinction between strings (represented by matched &quot; characters) and unevaluated code (represented by a single &#39; character). In Javascript, the eval function expects a string as an argument:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/957/1*KlnO_8ON8gATqOdR9odxPA.png" /><figcaption>eval in Javascript</figcaption></figure><p>However, in Clojure, the eval function expects to receive a <em>data structure</em> representing the code to evaluate, not a string. There is a separate function called read-string that takes in a string and returns such a data structure:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*F3LP2WD_V8hSp5Ig8ApIxw.png" /><figcaption>eval in Clojure</figcaption></figure><p>To execute a <em>string </em>of code as we did in Javascript, we need to use both read-string and eval :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*y6uRYJV8SqZXYltoyJGXng.gif" /></figure><p>In fact, this is exactly what the REPL is doing under the hood: It is calling read-string on the text that you type in, then calling eval on the resulting data structure, then printing the returned value to the console. This is unsurprising in hindsight, since REPL stands for Read-Eval-Print Loop.</p><p>With all that said, let’s talk about (in-ns &#39;main) . Normally, when you evaluate a symbol like main , the Clojure compiler looks for a “variable” defined in the current namespace with that name (again, I am putting the word “variable” in quotes because they don’t vary). So, if we are in the user namespace and we evaluate (in-ns main) with no single quote, the Clojure compiler will look for a variable called main in the user namespace. This will cause an error, because there is no such variable:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*HNujiAZdJx8OF0Mh-yRVDA.gif" /></figure><p>We include the single quote to tell the compiler <em>not </em>to evaluate the symbol main , so that this error will not occur:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*wzq5P54aDL4sVIDzJIvSvQ.gif" /></figure><h3>Surviving Clojure parentheses</h3><p>When we were typing out Clojure code in the terminal, you might have noticed that it is tough to get all the parentheses in the right places, even for relatively small bits of code. This problem grows exponentially when the code gets more complex, like this (still relatively small) function from the <a href="https://github.com/ring-clojure/ring">Ring HTTP server library</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d435ab51ea42d4378123ed003f21d0bc/href">https://medium.com/media/d435ab51ea42d4378123ed003f21d0bc/href</a></iframe><p>For this reason, it’s essential that our editor help us to manage all these parentheses. This is the purpose of the lisp-paredit plugin that we installed, a classic Emacs plugin that has been ported to most other major editors over the years.</p><p>By default, Paredit is configured to use “strict mode,” which will not allow you to delete a parenthesis if there is anything inside it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*5-WCEOYln8newWHERUEL9Q.gif" /></figure><p>Notice that I can’t delete the closing parenthesis unless there’s nothing left inside the matching pair.</p><p>This is <em>extremely </em>useful, because it prevents you from accidentally deleting a closing parenthesis somewhere, and then needing to hunt around among dozens of parentheses to find where there’s one missing.</p><p>However, this can also come back to bite you, if you don’t know how to handle it. Let’s see how.</p><p>Suppose that I’m writing a function that constructs a url to request an article from awesome-website.com . The url should look like this:</p><pre>https://awesome-website.com/articles/&lt;article-id&gt;</pre><p>I start writing my function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*DRTatHvIU8bD31WIUkoD-g.gif" /></figure><p>Then, I realize that I need to concatinate the value of article-id into this string, so I try to use the str function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*1bzH1elMdrNy39cFWti3Zg.gif" /><figcaption>Noooooo!</figcaption></figure><p>…and I realize that Paredit won’t let me delete the closing parenthesis on the str call to move the string inside the parentheses!</p><p>There are 3 ways we could handle this:</p><ol><li>Turn off Paredit strict mode (bad idea, don’t do this)</li><li>Cut-and-paste the string into the parentheses (slow and awkward)</li><li>Use the full power of Paredit (highly recommended)</li></ol><p>Paredit has a command called “slurp forwards”, which moves the right-hand parenthesis forward to enclose the next value:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*V_UqsPDxhie3ggoKm8nsDA.gif" /></figure><p>Once that’s done, we can concatinate the article-id into the string to finish the function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*0pw0B0S5aH7VvTX9CSHSNg.gif" /></figure><p>You can find the hotkey for “slurp forwards” in the Atom Command Palette:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pW45BbPKq1j4xVxeUy6ixg.gif" /></figure><p>Paredit has a <em>lot</em> of functionality, but this one command constitutes 80% or more of my usage. It should be sufficient to help you survive Clojure’s parentheses.</p><p>That’s it! Our Clojure development environment is set up. In the next article, we will dive into the language itself, and learn how to get work done using Clojure’s awesome immutable data structures.</p><p><a href="https://medium.com/@daniel.oliver.king/getting-work-done-in-clojure-the-building-blocks-39ad82796926">Part II &gt;&gt;</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c11e6711ead3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hack Your Personal Finances]]></title>
            <link>https://medium.com/@daniel.oliver.king/hack-your-personal-finances-fe0dbdceecf2?source=rss-8dad49c4d059------2</link>
            <guid isPermaLink="false">https://medium.com/p/fe0dbdceecf2</guid>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[habits]]></category>
            <category><![CDATA[personal-finance]]></category>
            <category><![CDATA[money]]></category>
            <category><![CDATA[personal-development]]></category>
            <dc:creator><![CDATA[Daniel King]]></dc:creator>
            <pubDate>Tue, 03 Jan 2017 01:45:16 GMT</pubDate>
            <atom:updated>2017-01-03T01:45:16.684Z</atom:updated>
            <content:encoded><![CDATA[<h3>Hack Your Personal Finances: Why Small Tweaks Can Make a LIFE CHANGING Difference</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/275/1*PPKBKNdVWDX6kmI3Zm0mxg.jpeg" /></figure><blockquote>Be not afraid of gowing slowly, be afraid only of standing still.</blockquote><p>Happy 2017! What’s your new year’s resolution? If you’re like <a href="http://www.statisticbrain.com/new-years-resolution-statistics/">many people</a>, you might have set up a resolution for this year, like “I’m going to lose weight!”, or “I’m going to enjoy life to the fullest!”, whatever that means.</p><p>And, if you’re like many people, you have probably made some new year’s resolutions in the past, and then <em>failed</em> to meet them. Too often, we set out to achieve some huge goal that we think will make huge difference in our lives. In the early days, we are motivated by that goal. As time goes on, though, the goal by itself isn’t enough to sustain us. We are putting in lots of hours at the gym, but we don’t see our progress day-by-day, and so we get discouraged, and we stop being so dedicated to the mission, and we eventually give up entirely.</p><p>It is much easier to make changes in our lives if the changes are <em>small</em>. It is easier to commit to walking up the stairs instead of taking the elevator, rather than committing to spending 2 hours in the gym daily. But, make no mistake: These <em>small</em> changes, when they become habits, can add up to LIFE CHANGING results.</p><p>I realize that you’ve probably heard all of this before, and you’d rather read about practical things that you can <em>do</em>. Today, I would like to make <em>one </em>suggestion as to a small change that we can all make to our habits this year. This particular change has to do with <em>money</em>, and how we can make choices that will improve our financial situation. However, the concepts we’ll talk about here can be applied to almost any aspect of your life to make <em>small</em> changes that will create HUGE benefits.</p><h4>Every Financial Decision is a Long-Term Decision</h4><p>To truly understand how to make small changes to our habits that will add up in a BIG way, it is important to change the way we look at the small purchases that we make on a day-to-day basis. I’m going to use a classic example to illustrate this principle: A habit of going out for coffee every morning.</p><p>There are a lot of people who stop in at Starbucks every morning on their way to work to pick up a coffee. About 5 years ago, I was one of them.</p><p>“A coffee is $5,” I would think to myself. “That’s not bad at all. Why not throw in a pastry or two while I’m at it?” All in all, it is easy to spend around $10 a day on your morning coffee.</p><p>This doesn’t sound like much…<strong>until you look at the <em>long-term </em>consequences</strong>.</p><p>There is a great rule of thumb that I recently learned from <a href="http://www.mrmoneymustache.com/2013/06/24/when-the-back-of-the-napkin-can-be-worth-millions/">Mr. Money Mustache</a>: For a weekly expense, multiply the amount by <strong>752</strong> in order to estimate the cost after 10 years. For a monthly expense, multiply the amount by <strong>173 </strong>(see <em>A Note on the Math </em>at the end of this post if you’re curious about how these numbers are determined).</p><p>Applying this rule of thumb to my morning coffee, I find that after 10 years, I will have given up $10 * 7 days per week * 752 = $52,640.</p><p><strong><em>WHAAAAAAT????!!!!!</em></strong></p><p>FIFTY-TWO-THOUSAND DOLLARS, FOR COFFEE AND PASTRIES?</p><p>I don’t know about you, but I sure wish that I had made a decision 10 years ago that would have resulted in me having FIFTY-TWO-THOUSAND DOLLARS MORE in savings right now. Once I saw that, the decision would be pretty damn easy to make coffee at home from now on.</p><p><strong>That is the power of small habits: They accumulate over surprisingly short times to yield enormous results.</strong></p><p>Don’t make the mistake of thinking that this is about coffee — if you focus on that example, you’re missing the point. The point is, it is easy to fall into the trap of believing that a small habit like going out for coffee every morning won’t have much of an effect on your overall financial picture. But, when you take into consideration that<a href="http://www.cbsnews.com/news/most-americans-cant-handle-a-500-surprise-bill/"> 63% of Americans would have trouble pulling together one-thousand dollars to handle a surprise expense</a>, it becomes clear that for most people, it is insane to spend over <em>fifty-thousand dollars</em> on coffee when you could have made coffee just as well at home.</p><h4>Life Changing Results</h4><p>If you look in the mainstream media, it is easy to find headlines like <a href="https://www.theguardian.com/business/2015/dec/25/wealthy-americans-living-paycheck-to-paycheck-income-paying-bills">this one</a> about how you just can’t get by on $100,000 a year anymore. Given this, some people might look at the $52,640 figure from above and think, “yeah, that’s a nice chunk of change, but it’s small potatoes compared to what you <em>really </em>need in today’s world.”</p><p>NONSENSE.</p><p>$52,640 is LIFE CHANGING money. To see why, it is important to think about it not in terms of what you can <em>buy</em> with it, but in terms of the <em>freedom </em>that it can give you.</p><p>$52,640 is enough money that you could live comfortably in any city in the United States for <em>months</em>, without needing anything extra.</p><p>It’s enough money that you could quit your job, devote some time to learning or going back to school, and reinvent yourself in a new career that you’re more passionate about (incidentally, I myself did this in 2016, and I am <em>so thankful</em> that I didn’t have to worry about how I was going to put food on the table during that transition).</p><p>It’s enough money that you don’t have to <em>worry </em>about losing your current job, because you would be able to survive comfortably for a little while as you put some effort into finding your next opportunity.</p><p>It’s enouh money that you could escape an abusve relationship, help out a family member in dire need, or move to a new place to seek new opportunities.</p><p>All of these LIFE CHANGING possibilities…from a small decision to make coffee at home instead of going out.</p><p>So, here’s my challenge for 2017: Choose some relatively small aspect of your financial situation, apply the rule of thumb above to estimate how much you could save over 10 years if you made a change, and then…make the change. Some things to consider:</p><ul><li>Going out to eat for lunch or dinner a lot? Try cooking at home more regularly (my favorite strategy is to spend a couple of hours on Sunday cooking for the entire week).</li><li>Spending a lot of time commuting to work? Think about if it would be worth it to move closer to cut down your commute time.</li><li>Driving a new car? Consider trading it in and getting a good used car on Craigslist (see <a href="http://www.mrmoneymustache.com/2012/03/19/top-10-cars-for-smart-people/">here</a> for some good suggestions, although the list is a little out of date)</li><li>Using an expensive cell phone plan from a major provider? Consider switching to one of the excellent alternatives like <a href="https://republicwireless.com/">Republic Wireless</a> (I literally cut my phone spending down by 75% by doing this, which will save me over $10,000 after 10 years)</li></ul><p>Whatever changes you decide to make, I’d love it if you would let us know about it in the comments below. Let’s support each other in making our lives a little better this year.</p><p>Once again, happy 2017!</p><p><em>If you enjoyed this article or found it helpful, please click the </em>heart <em>below to recommend it to others who might like it as well!</em></p><p>(<em>A Note on the Math</em>: These numbers are based on the assumption that if you hadn’t spent that money, you would have put it into a productive investment paying about 7% per year. This is a very reasonable assumption on average, provided that you own, for example, shares of a <a href="http://jlcollinsnh.com/2012/05/09/stocks-part-v-keeping-it-simple-considerations-and-tools/">low-cost stock index fund</a>. If you’re not very confident about investing in the stock market or have some anxiety over the risks involved, I highly recommend reading the <a href="http://jlcollinsnh.com/stock-series/">Stock Series</a>, by Jim Collins.)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fe0dbdceecf2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dependency Injection part 2: A (More) Practical Example for Testing in JavaScript]]></title>
            <link>https://medium.com/@daniel.oliver.king/dependency-injection-part-2-a-more-practical-example-for-testing-in-javascript-d96f5c59d17e?source=rss-8dad49c4d059------2</link>
            <guid isPermaLink="false">https://medium.com/p/d96f5c59d17e</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[code-quality]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[interfaces]]></category>
            <dc:creator><![CDATA[Daniel King]]></dc:creator>
            <pubDate>Fri, 23 Dec 2016 18:38:13 GMT</pubDate>
            <atom:updated>2017-01-23T03:08:28.017Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z3TGKPZe9nT4khE6KEQ3iw.jpeg" /></figure><blockquote>Business and politics have a wholesome and an unwholesome interface. You have to eliminate the unwholesome interface. ~ Salman Khurshid</blockquote><p>In the <a href="https://medium.com/@daniel.oliver.king/dependency-injection-practical-examples-for-testing-and-refactoring-in-javascript-3cb5b58b50be#.iicbbq4ub">last article</a>, I discussed the technique of <strong>dependency injection</strong> as a practical strategy for making our code easier to test and refactor. This time around, I’d like to dig in a little farther and show a larger scale example in order to demonstrate how dependency injection is <em>really </em>used in practice.</p><p>But first, to provide some context, let’s talk about a very far-reaching and important topic in software development: <em>Interfaces.</em></p><h4>Interfaces</h4><p>Last time, I described dependency injection as follows:</p><p>If a function/object/module depends on some other function/object/module, it should <em>not </em>go out and get what it needs. Instead, <em>we </em>explicitly give it anything it needs, typically by passing it in as a function parameter.</p><p>This concept touches on the very deep concept of <em>interfaces</em>, which (broadly) refers to the ways in which different parts of a system interact with each other.</p><p>We encounter <em>interfaces</em> all over our daily lives without even thinking about it. Think about a smartphone: To interact with the phone, you don’t open it up and fiddle around with the electronics inside (or, at least, most people don’t) — instead, you just use the few buttons and touchscreen that are provided on the outside of the phone. These few buttons and touchscreen form the <em>interface </em>of the phone, and because they are there, you don’t need to know the internal details of how the phone works in order to use it — those details are <strong>encapsulated</strong>, because the phone presents a clean, wholesome interface to the outside world.</p><p>The same principle holds true in software: To the extent possible, we want to create functions and modules that are <em>self-contained</em> and present a clean, wholesome <em>interface </em>to the outside world, rather than having their internal mechanisms tangled up with things outside. The essential purpose of dependency injection is to allow us to create clean interfaces where the different parts of our system can interact, rather than jumbling them together.</p><p>In the last article, I mainly looked at dependency injection at the <em>function </em>level. This time, we’re going to scale it up a bit and look at dependency injection at the <em>module </em>level, which is the most common use case that people think of when they hear the term “dependency injection”.</p><p>Disclaimer: This article was heavily inspired by <a href="https://www.youtube.com/watch?v=fgqh-OZjpYY">this excellent FunFunFunction video</a>, which was in turn inspired by this <a href="https://www.destroyallsoftware.com/talks/boundaries">excellent talk</a> on <em>boundaries</em>. Check out those resources if you’re interested in learning more!</p><p>Without further ado…</p><h4>Dependency Injection at the Module Level</h4><p>If you want to follow along on an actual codebase, you can find the complete code for this example on <a href="https://github.com/dking1286/dependency-injection-demo-2">github</a>.</p><p>Suppose that you were creating some kind of blog website, and you have a database full of articles. One day, your boss asks you to create a piece of functionality that gets the titles of the most popular articles on the blog. Your initial inclination might be to create something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/251ebef144bf08a28df419fd3c2c7f83/href">https://medium.com/media/251ebef144bf08a28df419fd3c2c7f83/href</a></iframe><p>Here, we are creating an article controller which has a <em>getPopularTitles</em> method. This method queries the database for the titles of all of the articles that have over a certain number of “likes”. Note that we are using the <a href="http://knexjs.org/">knex library</a>, which is a popular way of interacting with SQL databases in JavaScript.</p><p>However, notice that this module is not self-contained. It imports knex itself and then directly interacts with the database, which means that if we try to test this module, we will end up querying the real database. This module is not presenting a clean, wholesome <em>interface</em> to the outside world. Let’s fix this by using dependency injection. Instead of importing knex and exporting the article controller object directly, this module will export a <em>function </em>which takes in the dependencies as an object parameter and <em>returns</em> the article controller object:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e15b80a2876b3610ddc28e99f719e686/href">https://medium.com/media/e15b80a2876b3610ddc28e99f719e686/href</a></iframe><p>Now, notice that the article controller does not import anything itself. Therefore, it is <em>self-contained</em>, and it is presenting a clean interface to the outside world. This will make it much easier to test in isolation.</p><h4>Testing the Module</h4><p>Now that we have cleaned up the module using dependency injection, we can test it by passing in a <em>mock</em> knex object, instead of the real knex library. Let’s think about what this mock needs to look like.</p><p>Referring back to the article controller code above, we can see that the knex object is <em>directly called. </em>This means that our mock knex object needs to be a function. However, after the function is called, a <em>select </em>method and a <em>where </em>method are chained directly afterward. Here’s how we can mock that: our knex mock will be a function, but since function are just <em>objects</em> in JavaScript, we will also attach <em>select</em> and <em>where </em>methods to this function. When the function itself or either of its methods are called, they will simply return the original function again, allowing them to be chained indefinitely.</p><p>In order to confirm that the correct query is performed on the database, we will use <em>spies</em> from the <a href="http://sinonjs.org/">sinon.js</a> library.</p><p>With all that in mind, here’s what our test file might look like. Make sure to read through it carefully and notice what’s going on:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa6f2a1583b48a298fef7e7dcf42c360/href">https://medium.com/media/aa6f2a1583b48a298fef7e7dcf42c360/href</a></iframe><p>Notice that we call the <em>createKnexMock</em> function to make our mock database connection, complete with sinon spies. Then, we call <em>createArticleController</em> and pass in the mock knex object. This allows us to test the article controller object in isolation, without needing to connect to an actual database.</p><h4>Final Thoughts</h4><p>If you found this article helpful in terms of understanding how dependency injection can help us make our modules more self-contained and our interfaces more clean, I definitely recommend checking out the <a href="https://www.youtube.com/watch?v=fgqh-OZjpYY">FunFunFunction video on mocking</a> and this <a href="https://www.destroyallsoftware.com/talks/boundaries">excellent talk on boundaries</a>, which will go into more detail about similar ideas. Happy coding!</p><p>If you enjoyed this article or found it helpful, please click the heart below to recommend it to others who might like it as well!</p><p>You might also enjoy learning about:</p><ul><li><a href="https://medium.com/@daniel.oliver.king/getting-started-with-recursion-f89f57c5b60e#.yzva6zmf1">Learning to think with recursion</a></li><li><a href="https://medium.com/@daniel.oliver.king/writing-expressive-code-b69ef7a5a2fa#.ofs3id6np">Writing expressive code</a></li><li><a href="https://medium.com/@daniel.oliver.king/hack-your-personal-finances-fe0dbdceecf2#.a536z5if9">Hacking your personal finances</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d96f5c59d17e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dependency Injection: Practical Examples for Testing and Refactoring in JavaScript]]></title>
            <link>https://medium.com/@daniel.oliver.king/dependency-injection-practical-examples-for-testing-and-refactoring-in-javascript-3cb5b58b50be?source=rss-8dad49c4d059------2</link>
            <guid isPermaLink="false">https://medium.com/p/3cb5b58b50be</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[software-design]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[code-quality]]></category>
            <dc:creator><![CDATA[Daniel King]]></dc:creator>
            <pubDate>Thu, 01 Dec 2016 03:52:30 GMT</pubDate>
            <atom:updated>2017-01-23T03:02:12.006Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A2__sNyut5-gP2Jbnp0ycQ.jpeg" /></figure><blockquote>Three Rules of Work: Out of clutter find simplicity; From discord find harmony; In the middle of difficulty lies opportunity ~ Albert Einstein</blockquote><p>As someone who builds software, I find myself thinking a lot about how to organize my code. Good quality code organization is one of the main factors that makes a large software system flexible, easy to change, easy to reason about, and easy to test. On the flip side, if code is poorly organized, the system can become a spaghettified mess that is all but impossible to reason about or change, and if you have worked on a codebase of any significant size, you have probably struggled with the question of how to test some aspect of that system.</p><p>In this article, I would like to discuss one of the popular patterns of code organization: <strong>Dependency Injection</strong>. In the process, we will look at a practical example that will allow us to see ways in which this pattern can help us <strong>refactor </strong>and <strong>test</strong> our code.</p><p>But first…</p><h4>What is Dependency Injection?</h4><p>According to Wikipedia,</p><blockquote>In software engineering, <strong>dependency injection</strong> is a software design pattern that implements inversion of control for resolving <strong>dependencies</strong>. A <strong>dependency</strong> is an object that can be used (a service). An <strong>injection</strong> is the passing of a <strong>dependency</strong> to a dependent object (a client) that would use it.</blockquote><p>Blah blah blah…but what does it all <em>mean</em>? Here’s my favorite mental model:</p><p>If a function/object/module depends on some other function/object/module, it should <em>not </em>go out and get what it needs. Instead, <em>we </em>explicitly give it anything it needs, typically by passing it in as a function parameter.</p><p>To see what this means in action, let’s look at an example.</p><h4>An Example to Get Us Started</h4><p>If you would like to see and follow along with the code for this article, you can find the finished example on my <a href="https://github.com/dking1286/dependency-injection-demo">GitHub repository</a>.</p><p>In order to get the most out of this article, you should have at least some familiarity with:</p><ol><li><a href="http://www.javascriptkit.com/javatutors/javascriptpromises.shtml">Promises</a></li><li>The <a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest">XMLHttpRequest</a> API</li><li>Unit testing with <a href="https://mochajs.org/">Mocha</a> and <a href="http://chaijs.com/">Chai</a></li><li><a href="http://javascriptissexy.com/understand-javascript-closures-with-ease/">Closures</a> in JavaScript</li></ol><p>Suppose that we are writing a client-side module, and we want to write a Promise-based function that makes an http GET request to a provided URL. If you have experience with JavaScript, then you probably know that there are plenty of existing libraries that will do this for you, but this example will serve to illustrate the concept. The usage of the function would look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fb19f824a833394cae6ed2cb2ef675b4/href">https://medium.com/media/fb19f824a833394cae6ed2cb2ef675b4/href</a></iframe><p>To implement this function, we might initially do something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b30668202f7e0df9f767055c646989d/href">https://medium.com/media/6b30668202f7e0df9f767055c646989d/href</a></iframe><p>However, there is a lot going on in this function. Let’s try to refactor it to make it a little easier to reason about.</p><p>One thing that would be natural to factor out of this function is the event handler that fires when the http response comes back. We might try doing something like what we have below, but there is a <strong>huge </strong>problem lurking in this example. See if you can spot it before moving on:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/06a8db923042d423de2ec8900ff60e71/href">https://medium.com/media/06a8db923042d423de2ec8900ff60e71/href</a></iframe><p>You may have noticed that the <em>onHttpResponse</em> function depends on the <em>httpRequest, resolve </em>and <em>reject </em>variables, but those variables do not exist at the location where the event handler function is now defined. So, how can we overcome this issue? By using <strong>dependency injection</strong>!</p><h4>Dependency Injection as a Strategy for Refactoring</h4><p>Since the <em>onHttpResponse</em> function depends on the <em>httpRequest</em>, <em>resolve</em> and <em>reject </em>variables, we can explicitly pass those dependencies into the function as parameters, as shown below. This is a basic example of dependency injection.</p><p>This example is <em>almost </em>correct, but there is still a very sneaky error hiding here. Once again, see if you can spot it before moving on:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e6957cc41bacc8c3b89f5504557c9f6b/href">https://medium.com/media/e6957cc41bacc8c3b89f5504557c9f6b/href</a></iframe><p>The problem here is that the <em>onHttpResponse</em> function is an <em>event handler </em>function. This means that we have <em>no control</em> over what gets passed into the function as arguments when it is invoked. An event handler is <em>always </em>invoked with only one argument, namely the event object. We have a dilemma: We want to be able to pass <em>httpRequest, resolve </em>and <em>reject </em>into the <em>onHttpResponse</em> function as parameters, but when it is invoked, it will only be invoked with the event object.</p><p>To resolve this dilemma, we can turn the <em>onHttpResponse</em> function into a <em>higher order function </em>that takes in the dependencies as parameters and returns the desired event handler function<em>:</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4e3babd7d694db508e7f990568aa2ee/href">https://medium.com/media/e4e3babd7d694db508e7f990568aa2ee/href</a></iframe><p>Notice how <em>httpRequest</em>, <em>resolve </em>and <em>reject</em> are being passed into the <em>onHttpResponse</em> function on line 4, which <em>returns</em> the event handler function, which is assigned to the <em>onload</em> property of the http request. Even though the event handler function is not explicitly receiving <em>resolve, reject, </em>and <em>httpRequest</em> as parameters, it still has access to these variables because they existed at the moment it was defined (read more about <a href="http://javascriptissexy.com/understand-javascript-closures-with-ease/">closure</a> if this point is confusing).</p><p>As a side note, this kind of higher order function can be written very succinctly using ES6 arrow function syntax:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/09c8f8919cc701677e5837b2d6714a3d/href">https://medium.com/media/09c8f8919cc701677e5837b2d6714a3d/href</a></iframe><p>What have we done so far? We used dependency injection as a tool to take a large, complicated function, and refactor it into smaller, more manageable chunks. However, it turns out that we also get something else for free: Those functions can now be easily tested!</p><h4>Dependency Injection as a Strategy for Testing</h4><p>Now that the <em>onHttpResponse </em>function is explicitly accepting <em>httpRequest</em>, <em>resolve </em>and <em>reject </em>as parameters, we can now use a library like <a href="http://sinonjs.org/">sinon.js</a> to test this function by passing in fake versions of those parameters called <strong>spies</strong>. This strategy of passing in fake versions of dependencies for testing purposes is often referred to as <strong>mocking.</strong></p><p>When writing a test, it is important to think carefully about what the behavior of the function should actually be. The event handler created by <em>onHttpResponse</em> should do the following:</p><ol><li>If the response comes back with a status code of 200, it should invoke <em>resolve</em> and pass in the response body.</li><li>If the response comes back with some other status code (say, 404), it should invoke <em>reject</em> and pass in an error object.</li></ol><p>Here is what a test might look like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b0098b1605beb3009c429fd71da8f7fa/href">https://medium.com/media/b0098b1605beb3009c429fd71da8f7fa/href</a></iframe><p>A couple of things to notice in this example:</p><ol><li>Before each test case, <em>resolve </em>and <em>reject</em> are replaced with <strong>spies</strong>, which are functions that can report back whether or not they were invoked. They can also tell you what arguments they were invoked with.</li><li>In each test case, an object literal is passed into the <em>httpRequest</em> parameter of the <em>onHttpResponse </em>function, which takes the place of the real http request that would ordinarily be passed in. This object literal has the status code and the response text hard-coded.</li><li>Although the <em>onHttpResponse </em>function is intended to work as part of a system that makes http requests, this test is not actually making any external requests. This means that we are testing this function in <em>isolation</em>, eliminating the possibility that our tests could give unpredictable results due to outside factors.</li></ol><p>Now that we have the <em>onHttpResponse </em>function working and tested, let’s look at the <em>get </em>function, which was really the whole point of this example. Here’s where we were:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4e3babd7d694db508e7f990568aa2ee/href">https://medium.com/media/e4e3babd7d694db508e7f990568aa2ee/href</a></iframe><p>In its current form, the <em>get </em>function would be fairly difficult to test, because it is making an http request to some external server. One way to make this more testable is to use — you guessed it — dependency injection! Namely, instead of allowing the function to create its own <em>XMLHttpRequest</em> object, we can pass one in as a parameter to the function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/12d74bd14bbeb95039dd4ff385e19999/href">https://medium.com/media/12d74bd14bbeb95039dd4ff385e19999/href</a></iframe><p>Now, if we want to test this function, we can replace the http request with a fake containing spies, similar to what we did with <em>resolve </em>and <em>reject </em>before. This will allow us to test the function without needing to rely on any external server. Our testing file will now look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eef26b7076774f076ca7f11ba53baac6/href">https://medium.com/media/eef26b7076774f076ca7f11ba53baac6/href</a></iframe><p>This is a good thing, but we now have a problem that can crop up frequently when using dependency injection: The function’s interface can get cumbersome to use. Now, whenever we want to use the <em>get</em> function, we have to explicitly pass in a (real) http request object:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f200d4b21f0f030faa21546554e2e79f/href">https://medium.com/media/f200d4b21f0f030faa21546554e2e79f/href</a></iframe><p>This is not so bad in this case, but when the system gets more complicated this can result in some painfully complicated interfaces. So, let’s finish off this example by cleaning up the interface.</p><h4>Cleaning Up the Interface</h4><p>There are many ways that we could simplify the interface in this example. One way or another, our goal will be to have the <em>get</em> function no longer require explicitly passing in an http request object. So the usage of the function should be like it was before:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fb19f824a833394cae6ed2cb2ef675b4/href">https://medium.com/media/fb19f824a833394cae6ed2cb2ef675b4/href</a></iframe><p>But, how can we accomplish this without sacrificing our ability to test the function? One way is to put a default value on the <em>httpRequest </em>parameter, which we can <em>choose</em> to override if we want to pass in our own fake http request:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83c7bfe13dabfb87c2099e8bb74c9769/href">https://medium.com/media/83c7bfe13dabfb87c2099e8bb74c9769/href</a></iframe><p>Now, when we run our tests, we can explicitly pass in a fake http request, but when we use the <em>get</em> function in other parts of the system, it will create its own real http request object by default.</p><h4>Final Thoughts</h4><p>Dependency injection is not a magic bullet that will solve all of your problems for you. As we discussed, it can sometimes lead to function interfaces becoming very complicated and hard to use. However, when used judiciously, it can become a powerful tool that allows us to write more flexible, testable code.</p><p>It’s also worth noting that the examples of dependency injection that I showed here are extremely simple, compared to some more “real-world” usages of the technique. However, I hope that this has been enough to get you comfortable with the concept and has made a <em>practical</em> case for why it is an effective design principle. Check out <a href="https://medium.com/@daniel.oliver.king/dependency-injection-part-2-a-more-practical-example-for-testing-in-javascript-d96f5c59d17e#.odxxn46jy">part 2</a> for a look at more realistic examples!</p><p>Happy coding!</p><p>If you enjoyed this article or found it helpful, please click the heart below to recommend it to others who might like it as well!</p><p>You might also enjoy learning about:</p><ul><li><a href="https://medium.com/@daniel.oliver.king/getting-started-with-recursion-f89f57c5b60e#.yzva6zmf1">Learning to think with recursion</a></li><li><a href="https://medium.com/@daniel.oliver.king/writing-expressive-code-b69ef7a5a2fa#.ofs3id6np">Writing expressive code</a></li><li><a href="https://medium.com/@daniel.oliver.king/hack-your-personal-finances-fe0dbdceecf2#.a536z5if9">Hacking your personal finances</a></li></ul><p><em>Daniel King is a professional software engineer and educator in the Los Angeles area who is available for software development and coaching on a contract basis.</em></p><p><em>daniel.oliver.king@gmail.com</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3cb5b58b50be" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learning to think with recursion, part 2]]></title>
            <link>https://medium.com/@daniel.oliver.king/learning-to-think-with-recursion-part-2-887bd4c41274?source=rss-8dad49c4d059------2</link>
            <guid isPermaLink="false">https://medium.com/p/887bd4c41274</guid>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[data-structures]]></category>
            <dc:creator><![CDATA[Daniel King]]></dc:creator>
            <pubDate>Fri, 21 Oct 2016 00:42:12 GMT</pubDate>
            <atom:updated>2017-01-23T03:06:02.129Z</atom:updated>
            <content:encoded><![CDATA[<h4>Why should I care about this, anyway?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*N85kfgpnjGEDjTkjC05blw.jpeg" /></figure><p>In my <a href="https://medium.com/@daniel.oliver.king/getting-started-with-recursion-f89f57c5b60e#.6y6ieluan">last article</a>, I discussed a particular strategy that I use when I am trying to design a recursive algorithm to solve a problem. I definitely recommend reading that article before this one, but to briefly summarize, here is the strategy:</p><ol><li>Break the problem I am trying to solve down into a problem that is <em>one step simpler</em></li><li><em>Assume </em>that my function will work to solve the simpler problem — really <em>believe </em>it beyond any doubt</li><li>Ask myself: Since I <em>know</em> I can solve the simpler problem, how would I solve the more complex problem?</li></ol><p>Last time, I applied this strategy in order to design a recursive algorithm that reverses a string. If you were reading that article and found yourself saying “What the feather! Why don’t you just use a loop to reverse the string? It’s so much simpler!” then I completely understand. That’s why, this time, we are going to apply this same strategy to do some neat things with <strong>recursive data structures</strong>.</p><h3>Recursive Data Structures</h3><p>Roughly speaking, a <em>recursive data structure</em> is a data structure that contains smaller versions of the same structure inside of itself. In Javascript, a basic example of a recursive data structure could be an object that contains other objects:</p><pre>const danielKing= {<br>  name: {<br>    firstName: &#39;Daniel&#39;,<br>    lastName: &#39;King&#39;,<br>  },<br>  favorites: {<br>    foods: [<br>      &#39;pizza&#39;,<br>      &#39;curry&#39;<br>    ]<br>  }<br>}</pre><p>There are a number of recursive data structures that we use frequently in practice, but two of the most common are the <a href="https://en.wikipedia.org/wiki/Linked_list">linked list</a> and the <a href="https://en.wikipedia.org/wiki/Tree_(data_structure)">tree</a>. In this article, I will be discussing one particular type of tree, called a <a href="https://en.wikipedia.org/wiki/Binary_search_tree">binary search tree</a>.</p><h4>Binary Search Trees</h4><p>Briefly, a binary search tree is a tree structure which satisfies two conditions:</p><ol><li>Each node in the tree has two child nodes, which are called the “left” and “right” child nodes (one or both of the child nodes may be null).</li><li>The value of the left child node must be less than the value of the parent node, and the value of the right child node must be greater than the value of the parent node</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YQ1t6ZVjdR9rNvpegFC4Qg.png" /><figcaption>A Binary Search Tree</figcaption></figure><p>A binary search tree is considered to be a <em>recursive data structure</em> because, if you look at only the left sub-tree of the top node, it is a whole binary search tree in and of itself. Similarly, if you look at only the right sub-tree, it is a complete binary search tree.</p><p>In Javascript, a definition for a single node of this tree might look like this:</p><pre>function BinaryTreeNode(value) {<br>  this.value = value;<br>  this.left = null;<br>  this.right = null;<br>}</pre><p>Notice that we are initializing the “left” and “right” properties of the BinaryTreeNode object to null, meaning that if we create a BinaryTreeNode, it is just a single node sitting by itself. In order to build up a proper binary tree, let’s add a method to this object type that will enable us to add a value to the tree:</p><pre>function BinaryTreeNode(value) {<br>  this.value = value;<br>  this.left = null;<br>  this.right = null;<br>}</pre><pre>BinaryTreeNode.prototype.add = function(value) {<br>  // Do stuff to add another value to the tree<br>}</pre><p>Once we have this function, we will be able to build up a complete binary tree simply by invoking this method repeatedly on the top node.</p><p>So, how can we write a method that will add a new value to the binary tree? Well, since a binary tree is a <em>recursive data structure</em>, let’s think about if we can write a <em>recursive function</em> to accomplish this task! We’ll apply the same strategy as before:</p><ol><li><em>Break it down one level: </em>I am trying to add a new value into the binary tree. A problem that is <em>one step simpler </em>would be to add a new value into <em>the left or right sub-tree</em> of the main tree, since those sub-trees are smaller than the main tree.</li><li><em>Leap of faith</em>: I believe with every fiber of my being that my function can <em>already </em>add a new value into either the left or right sub-tree of the main tree.</li><li><em>Build it up: </em>Since my function can add a new value into either the left or right sub-tree of the main tree, the only other thing I need to decide in order to be able to add the value to the whole tree is <em>which </em>sub-tree it the value should be added to. This goes back to the definition of a binary tree: If the new value is <em>less than</em> the value of the top node, then that value must go in the <em>left sub-tree</em>, but if it’s greater, it must go in the <em>right sub-tree</em>. If the appropriate subtree is null, then I can directly create a new node to fill the space, and if it is not null, I can <em>recursively call the method again</em> to add the value into the appropriate subtree.</li></ol><p>If you want a challenge, try implementing this function before looking at the solution below:</p><pre>function BinaryTreeNode(value) {<br>  this.value = value;<br>  this.left = null;<br>  this.right = null;<br>}</pre><pre>BinaryTreeNode.prototype.add = function(value) {<br>  if (value &lt; this.value) {<br>    if (this.left === null) {<br>      this.left = new BinaryTreeNode(value)<br>    } else {<br>      this.left.add(value)<br>    }<br>  }</pre><pre>  if (value &gt; this.value) {<br>    if (this.right === null) {<br>      this.right = new BinaryTreeNode(value)<br>    } else {<br>      this.right.add(value)<br>    }<br>  }<br>}</pre><h4>Balanced Binary Trees</h4><p>I hope you found this article to be informative! Stay tuned for part 3, in which we’ll discuss some <em>practical applications </em>of recursive data structures (ooooooh, ahhhhh!)</p><p>If you enjoyed this article or found it helpful, please click the heart below to recommend it to others who might like it as well!</p><p>You might also enjoy learning about:</p><ul><li><a href="https://medium.com/@daniel.oliver.king/dependency-injection-practical-examples-for-testing-and-refactoring-in-javascript-3cb5b58b50be#.3ospcu5er">Prevent bugs by writing better tests with dependency injection</a></li><li><a href="https://medium.com/@daniel.oliver.king/writing-expressive-code-b69ef7a5a2fa#.ofs3id6np">Writing expressive code</a></li><li><a href="https://medium.com/@daniel.oliver.king/hack-your-personal-finances-fe0dbdceecf2#.a536z5if9">Hacking your personal finances</a></li></ul><p><em>Daniel King is a professional software engineer and educator in the Los Angeles area who is available for software development and coaching on a contract basis.</em></p><p><em>daniel.oliver.king@gmail.com</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=887bd4c41274" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing Expressive Code]]></title>
            <link>https://medium.com/@daniel.oliver.king/writing-expressive-code-b69ef7a5a2fa?source=rss-8dad49c4d059------2</link>
            <guid isPermaLink="false">https://medium.com/p/b69ef7a5a2fa</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[technical-communication]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[communication]]></category>
            <category><![CDATA[expression]]></category>
            <dc:creator><![CDATA[Daniel King]]></dc:creator>
            <pubDate>Sun, 18 Sep 2016 15:40:15 GMT</pubDate>
            <atom:updated>2017-01-23T03:00:20.004Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Computer science is no more about computers than astronomy is about telescopes.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*fg1afu3B6aRFBMXUd0lALg.jpeg" /></figure><p>About a year ago, I was introduced to the Ruby programming language for the first time. As I was reading about some of the reasons why people like to use it, one adjective kept coming up over and over again: <em>“</em>expressive”.</p><p>At the time, I was most comfortable with the Python programming language, and if you have any familiarity with Python, you know that its syntax prizes <em>explicitness</em> above all else. As the <a href="https://www.python.org/dev/peps/pep-0020/">Zen of Python</a> says, “Explicit is better than implicit”.</p><p>For this reason, I was somewhat confused as to what people were referring to when they described Ruby’s syntax as being “expressive.” By comparison to Python, Ruby has a million ways of accomplishing any given task, which (I felt) increases the cognitive load of reading Ruby code and therefore decreases its “expressiveness” (whatever that meant).</p><p>However, after bouncing this concept of “expressive code” around in my brain for the past year or so, I have come to the point where I feel as though I understand what people mean when they use this term: Roughly, it has to do with how well the code <em>expresses the intent of the programmer </em>to another human who is reading it.</p><p>Although you can often hear people praising particular programming languages for being expressive, I think that it is possible (and important) to write expressive code in any language. That is what I would like to talk about here: Ideas for how we can write expressive code independent of any particular programming language.</p><p>In the real world of computer software, code is <em>read </em>much more often than it is <em>written</em> — that is, a professional software developer spends a good deal of her time reading and trying to understand code that was written by other people, so that she can make appropriate modifications and upgrades. This means that when we write code, we are not only writing instructions to the computer, we are also writing for the benefit of whoever the next person to read our code is — even if it is ourselves, far enough in the future that I don’t remember exactly what I was thinking when I wrote the code.</p><p>This is, I think, one of the essential ideas behind code that is “expressive.” When we write code, we can write it in a way that not only gives correct instructions to the computer, but that <em>clearly communicates our intent </em>to the next human who reads our code.</p><p>To illustrate this idea, let’s suppose that I needed to write a function that would capitalize all of the vowels in a given string (why we would want to do this I’m not sure, but let’s continue). In JavaScript, I might write something that looks like this:</p><pre>function capitalizeVowels(string) {<br>  const VOWELS = [&#39;a&#39;, &#39;e&#39;, &#39;i&#39;, &#39;o&#39;, &#39;u&#39;];<br>  let result = &#39;&#39;;</pre><pre>  for (let i = 0; i &lt; string.length; i++) {<br>    let nextChar = string[i];<br>    if (VOWELS.includes(nextChar)) {<br>      nextChar = nextChar.toUpperCase();<br>    }<br>    <br>    result += nextChar;<br>  }</pre><pre>  return result;<br>}</pre><p>However, as I analyzed this function in more detail, I might notice that if I pass anything other than a string into this function as an argument, the function will do strange things — it might throw an error, or it might return something unexpected, depending on the situation.</p><p>To resolve this issue, it is common to put an <em>if </em>statement into the function to ensure that the argument passed in is actually a string. Naively, I might do something like this:</p><pre>function capitalizeVowels(string) {<br>  if (typeof string === &#39;string&#39;) {<br>    const VOWELS = [&#39;a&#39;, &#39;e&#39;, &#39;i&#39;, &#39;o&#39;, &#39;u&#39;];<br>    let result = &#39;&#39;;</pre><pre>    for (let i = 0; i &lt; string.length; i++) {<br>      let nextChar = string[i];<br>      if (VOWELS.includes(nextChar)) {<br>        nextChar = nextChar.toUpperCase();<br>      }<br>    <br>      result += nextChar;<br>    }</pre><pre>    return result;<br>  } else {<br>    return undefined;<br>  }<br>}</pre><p>Now, this solution will work as intended. However, it doesn’t do a good job of <em>expressing my intent</em>. What do I mean by this? Well, when I see an <em>if-else</em> structure in code, my mind envisions the resulting execution as having two possible paths, which is of course true in this case: Either the input is a string, or it is not. However, writing the code as an <em>if-else</em> structure implies that both of those possibilities are equally significant, which they are not— the case where the input is a string is the “main” purpose of the function, and the case when it is not a string is just an extra piece that allows the function to behave gracefully in the (unlikely) event that the input is not a string.</p><p>How can we change this code to better express the <em>intent </em>of the function, putting the focus on the case where the input <em>is </em>a string as it should be? One way to accomplish this is with the use of a <em>guard condition</em>, which allows the function to “bail out early” if something undesirable happens. Here’s what the function would look like if we refactor it to use a guard condition:</p><pre>function capitalizeVowels(string) {<br>  if (typeof string !== &#39;string&#39;) return undefined;</pre><pre>  const VOWELS = [&#39;a&#39;, &#39;e&#39;, &#39;i&#39;, &#39;o&#39;, &#39;u&#39;];<br>  let result = &#39;&#39;;</pre><pre>  for (let i = 0; i &lt; string.length; i++) {<br>    let nextChar = string[i];<br>    if (VOWELS.includes(nextChar)) {<br>      nextChar = nextChar.toUpperCase();<br>    }<br>    <br>    result += nextChar;<br>  }</pre><pre>  return result;<br>}</pre><p>Notice that the edge case where the input is not a string is handled by the first line in the function, and then it is never given a second thought. The function can proceed with its computation, confident that the input is the correct data type, without having the extra visual clutter of an extra level of indentation.</p><p>This may not seem like a very big improvement, but it can make a huge difference in clarity if there are more complicated conditions to check. For example, let’s suppose that we were trying to write a function that takes in an array of numbers and filters out all of the numbers that are not multiples of 3. In the ES2015 version of JavaScript, it can be written very succinctly like this:</p><pre>function getMultiplesOf3(array) {<br>  return array.filter(number =&gt; number % 3 === 0);<br>}</pre><p>But now, due to how this function interacts with everything else going on in the application, we want to return undefined if the input value is not an array, and we want to throw an error if any of the items in the array is not of type number. If we do this using <em>if-else </em>structures, it gets kind of messy:</p><pre>function getMultiplesOf3(array) {<br>  if (!Array.isArray(array)) {<br>    return undefined<br>  } else {<br>    if (!array.every(item =&gt; typeof item === &#39;number&#39;)) {<br>      throw new Error(&#39;Array has a non-number value&#39;);<br>    } else {<br>      return array.filter(number =&gt; number % 3 === 0);<br>    }<br>  }<br>}</pre><p>Notice that the really important computation, the part where we filter out all of the numbers that aren’t divisible by 3, is completely buried in the code that is checking for the various error conditions. As written, the function does not do a good job of <em>expressing the intent </em>of the function by drawing the reader’s attention to the main computation that it is doing. Instead, we can improve the expressiveness of the function by writing it with guard conditions like this:</p><pre>function getMultiplesOf3(array) {<br>  if (!Array.isArray(array)) return undefined;<br>  if (!array.every(item =&gt; typeof item === &#39;number&#39;) {<br>    throw new Error(&#39;Array has a non-number value&#39;);<br>  }</pre><pre>  return array.filter(number =&gt; number % 3 === 0);<br>}</pre><p>Notice that, although the error-checking code still takes up more lines of code than the main computation, it is much easier to see the true intent of the function. The main computation stands apart from the error-checking code, emphasizing that it is the most important part of what is happening. This makes it much easier for a human to read the code and build a mental model of what the original programmer was thinking when they wrote the function.</p><p>In this article, my goal was to communicate what it means for code to be <em>expressive</em> and to show a couple of examples of how we can go about making our own code more expressive. There are endless ways that we can do this, and I encourage you to use your imagination to think about how— the important thing is that when you write code, you keep the human reader in mind as well as the computer.</p><p>Happy coding!</p><p>If you enoyed this article, you might also enjoy learning about:</p><ul><li><a href="https://medium.com/@daniel.oliver.king/getting-started-with-recursion-f89f57c5b60e#.bvrdahl80">Learning to think with recursion</a></li><li><a href="https://medium.com/@daniel.oliver.king/dependency-injection-practical-examples-for-testing-and-refactoring-in-javascript-3cb5b58b50be#.qcp49vqo4">Writing better tests with dependency injection</a></li><li><a href="https://medium.com/@daniel.oliver.king/hack-your-personal-finances-fe0dbdceecf2#.a536z5if9">Hacking your personal finances</a></li></ul><p><em>Daniel King is a professional software engineer and educator in the Los Angeles area who is available for software development and coaching on a contract basis.</em></p><p><em>daniel.oliver.king@gmail.com</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b69ef7a5a2fa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learning to think with recursion, part 1]]></title>
            <link>https://medium.com/@daniel.oliver.king/getting-started-with-recursion-f89f57c5b60e?source=rss-8dad49c4d059------2</link>
            <guid isPermaLink="false">https://medium.com/p/f89f57c5b60e</guid>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Daniel King]]></dc:creator>
            <pubDate>Tue, 13 Sep 2016 00:31:40 GMT</pubDate>
            <atom:updated>2017-01-23T02:59:54.748Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mWffKy65yspIVCeSsVyq9g.jpeg" /></figure><p>I have frequently heard people new to programming express that they have difficulty understanding how to write recursive algorithms. Although they understand <em>what </em>recursion is, they find difficult to understand <em>how </em>to go about creating a recursive solution. In this article, my goal is <em>not</em> to discuss in detail what recursion is — instead, I would like to discuss how to <em>do </em>recursion — that is, how to think through the process of writing a recursive algorithm to solve a problem.</p><p>When talking about writing recursive functions, most people focus on the fact that any recursive function needs to have two parts:</p><ol><li>A base case, in which the function can return the result immediately</li><li>A recursive case, in which the function must <em>call itself </em>to break the current problem down to a simpler level</li></ol><p>This pattern can be seen very clearly in one of the most commonly cited<br>examples of a recursive function, the factorial function, shown here in JavaScript:</p><pre><br>function factorial(n) {<br>  // Base case<br>  if (n === 0 || n === 1) return 1;</pre><pre>  // Recursive case<br>  return n * factorial(n — 1);<br>}</pre><p>Most of the time, I have heard people teaching recursion focus far too much on the base case, and not enough on the recursive case. People usually don’t have much trouble realizing what the base case should be — the harder part is deciding how the recursive call should be structured in order to accomplish the computation you want. Unfortunately, it is difficult to find useful advice about how we should go about doing this. This is what I would like to talk about here — how we should <em>think</em> when writing a recursive algorithm.</p><p>When I sit down to write a recursive algorithm to solve a problem, I have found it to be helpful to go through the following thought process in order to decide how the recursive call should be structured:</p><ol><li>Break the problem I am trying to solve down into a problem that is <em>one step simpler</em></li><li><em>Assume</em> that my function will work to solve the simpler problem — really <em>believe</em> it beyond any doubt</li><li>Ask myself: Since I <em>know</em> I can solve the simpler problem, how would I solve the more complex problem?</li></ol><p>To illustrate this thought process, let’s look at an example. Suppose that we want to write a recursive function that will return a reversed copy of a given string:</p><pre><br>function reverse(string) {<br>  // Recursive stuff here<br>}</pre><p>I won’t be surprised if you’re wondering, “Why do we need a recursive function to reverse a string? Wouldn’t it be easier to write a loop?” I completely agree! However, it’s important for us to look at a straightforward example first, before we tackle any algorithms that really require recursion.</p><p>First, let’s get the base case out of the way: If the string is only one character (or, for that matter, if it is empty), then we don’t need to do anything to reverse the string, and we can simply return it:</p><pre>function reverse(string) {<br>  // Base case<br>  if (string.length &lt; 2) return string;<br>}</pre><p>Now, for the more challenging part — figuring out how to write the recursive call to accomplish the rest of the task. Let’s go through the thought process that we outlined above:</p><ol><li>I am trying to reverse a string. A problem one step simpler would be to reverse a string that is <em>one letter shorter</em>.</li><li>I will assume, and <em>believe</em> with every fiber of my being, that my function<br>can correctly reverse a string that is one letter shorter than the one I am<br>currently trying to reverse.</li><li>I ask myself: Since I <em>know and believe </em>that my function can correctly reverse a string that is one letter shorter than the one I am currently trying to reverse, how can I reverse the whole string? Well, I can take all of the characters except the first one, reverse those (which I <em>know and believe</em> that my function can do), and then tack the first character on to the end! In code, it would look like this:</li></ol><pre><br>function reverse(string) {<br>  // Base case<br>  if (string.length &lt; 2) return string;</pre><pre>  // Recursive case<br>  return reverse(string.slice(1, string.length)) + string[0];<br>}</pre><p>Think carefully and make sure that you understand steps 1–3 of the thought process above, and how those logical steps resulted in the code shown. If there is anything unclear about it, read it again and think about it some more. Or, leave a question below!</p><p>One common mistake that I see people make when trying to develop a recursive algorithm to solve a problem is that they try to think about how to break the problem down <em>all the way to the base case</em>. I would like to emphasize that in order to develop the function above, I did <em>not </em>think about how I could break the problem down all the way to the base case. That is the <em>function’s </em>job, not yours. Instead, I <em>only </em>thought about the problem that is <em>one step simpler </em>than the problem I am really trying to solve, and then I wrote my recursive algorithm to build up from there to solve the real problem.</p><p>When learning to work with recursion myself, I found it extremely helpful to try doing simple tasks with recursion that I would normally do with a loop. If you’re interested in practicing the skill of writing recursive algorithms, try applying the thought process above to a few of the following problems — no loops allowed!</p><ol><li>Go through an array and print out all of the elements</li><li>Determine whether or not a string is a palindrome</li><li>Calculate <em>a</em> raised to the power of <em>b</em></li><li>Extra credit: Try implementing the <em>map</em> function (the one that transforms arrays) without using loops</li></ol><p>Although the concept of recursion is fairly straightforward to describe (a function that calls itself, how complicated can it be?), it is notoriously challenging to think about how we can use this technique in practice to solve problems. I have enjoyed the process of learning to use recursion in my own programming, and I hope that this article has helped to demystify it a little bit.</p><p>Stay tuned for <a href="https://medium.com/@daniel.oliver.king/learning-to-think-with-recursion-part-2-887bd4c41274#.miz09qwck">part 2</a>, where we’ll explore some more interesting examples of recursion in practice!</p><p>If you enjoyed this article, you might also enjoy learning about:</p><ul><li><a href="https://medium.com/@daniel.oliver.king/dependency-injection-practical-examples-for-testing-and-refactoring-in-javascript-3cb5b58b50be#.qcp49vqo4">Dependency injection</a></li><li><a href="https://medium.com/@daniel.oliver.king/writing-expressive-code-b69ef7a5a2fa#.xnm0ckjj7">Expressive code</a></li><li><a href="https://medium.com/@daniel.oliver.king/hack-your-personal-finances-fe0dbdceecf2#.a536z5if9">Hacking your personal finances</a></li></ul><p><em>Daniel King is a professional software engineer and educator in the Los Angeles area who is available for software development and coaching on a contract basis.</em></p><p><em>daniel.oliver.king@gmail.com</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f89f57c5b60e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>