Book Image

The Clojure Workshop

By : Joseph Fahey, Thomas Haratyk, Scott McCaughie, Yehonathan Sharvit, Konrad Szydlo
Book Image

The Clojure Workshop

By: Joseph Fahey, Thomas Haratyk, Scott McCaughie, Yehonathan Sharvit, Konrad Szydlo

Overview of this book

The Clojure Workshop is a step-by-step guide to Clojure and ClojureScript, designed to quickly get you up and running as a confident, knowledgeable developer. Because of the functional nature of the language, Clojure programming is quite different to what many developers will have experienced. As hosted languages, Clojure and ClojureScript can also be daunting for newcomers because of complexities in the tooling and the challenge of interacting with the host platforms. To help you overcome these barriers, this book adopts a practical approach. Every chapter is centered around building something. As you progress through the book, you will progressively develop the 'muscle memory' that will make you a productive Clojure programmer, and help you see the world through the concepts of functional programming. You will also gain familiarity with common idioms and patterns, as well as exposure to some of the most widely used libraries. Unlike many Clojure books, this Workshop will include significant coverage of both Clojure and ClojureScript. This makes it useful no matter your goal or preferred platform, and provides a fresh perspective on the hosted nature of the language. By the end of this book, you'll have the knowledge, skills and confidence to creatively tackle your own ambitious projects with Clojure and ClojureScript.
Table of Contents (17 chapters)
Free Chapter
2
2. Data Types and Immutability

About the Book

The Clojure Workshop is a step-by-step guide to Clojure and ClojureScript, designed to quickly get you up and running as a confident, knowledgeable developer.

Because of the functional nature of the language, Clojure programming is quite different to what many developers will have experienced. As hosted languages, Clojure and ClojureScript can also be daunting for newcomers because of complexities in the tooling and the challenge of interacting with the host platforms. To help you overcome these barriers, this book adopts a practical approach. Every chapter is centered around building something.

As you progress through the book, you will progressively develop the ‘muscle memory' that will make you a productive Clojure programmer, and help you see the world through the concepts of functional programming. You will also gain familiarity with common idioms and patterns, as well as exposure to some of the most widely used libraries.

Unlike many Clojure books, this Workshop will include significant coverage of both Clojure and ClojureScript. This makes it useful no matter your goal or preferred platform, and provides a fresh perspective on the hosted nature of the language.

By the end of this book, you'll have the knowledge, skills and confidence to creatively tackle your own ambitious projects with Clojure and ClojureScript.

Audience

The Clojure Workshop is an ideal tutorial for the Clojure beginner who is just getting started. A basic understanding of JavaScript and Java would be ideal but not necessary. The Clojure Workshop will guide you well throughout the discussion on the interoperability of these technologies.

About the Chapters

Chapter 1, Hello REPL!, gets you typing code immediately. You'll learn the basics of the language, as well as how to get the most out of Clojure's interactive REPL.

Chapter 2, Data Types and Immutability, provides more building blocks, but these are Clojure building blocks that expose you to one of Clojure's key features: immutability.

Chapter 3, Functions in Depth, is a deeper dive into one of the areas that sets Clojure apart: the functional programming paradigm. These are the tools that will power you through the rest of the book.

Chapter 4, Mapping and Filtering, is the first stop on your exploration of Clojure collections. The patterns and techniques here are all about learning to solve problems. The map and filter functions are two of the foremost Clojure workhorses.

Chapter 5, Many to One: Reducing, will really start getting you thinking in new ways. The data-shaping techniques in this chapter complement those in the previous chapter.

Chapter 6, Recursion and Looping, takes your collection techniques to the next level. This chapter will make you think. By the end of the chapter, you'll be ready to handle tricky problems using advanced functional patterns.

Chapter 7, Recursion II: Lazy Sequences, completes the panorama of Clojure collections with a look at a distinctive Clojure feature. If you can write functions to process complex tree structures, you are ready to use Clojure to solve big problems.

Chapter 8, Namespaces, Libraries, and Leiningen, provides a close look at the tools you need for building real-world Clojure and ClojureScript applications. You have the skills to write good Clojure code; now you need to know how to put your application together.

Chapter 9, Host Platform Interoperability with Java and JavaScript, brings you up to speed on a topic that is one of Clojure's great strengths but can also be daunting. As a hosted language, Clojure gives you access to the underlying platform. Knowing how and when to use that power is a key Clojure skill.

Chapter 10, Testing, is another important step in serious, real-world programming. Understanding Clojure and ClojureScript testing stories is a skill every professional programmer needs.

Chapter 11, Macros, will help you understand a distinctive feature of the Lisp family of languages. Macros allow rich abstraction, but underneath the surface, there are a lot of important practical details.

Chapter 12, Concurrency, reveals another unique Clojure strength. This chapter will give you a taste for building multithreaded applications on the Java Virtual Machine or event-driven ClojureScript single-page applications.

Chapter 13, Database Interaction and the Application Layer, shows you how to leverage Clojure's database libraries. Many real applications require databases, so these skills are essential.

Chapter 14, HTTP with Ring, shows you how to set up and run a Clojure-driven web server. The Ring libraries are the most widely used HTTP technology in the Clojure world.

Chapter 15, The Frontend: A ClojureScript UI, helps put together many of the things you've already learned about ClojureScript, the last layer on a Clojure web stack.

Conventions

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Please note that this function is in the clojure.string namespace, which is not referred to by default."

Words that you see on the screen, for example, in menus or dialog boxes, also appear in the text like this: "When you click on the Fetch Images button, the images appear with authors' names."

A block of code is set as follows:

(defn remove-large-integers [ints]
  (remove #(and (integer? %) (> % 1000)) ints))

In cases where inputting and executing some code gives an immediate output, this is shown as follows:

user=> (sort [3 7 5 1 9])
(1 3 5 7 9)

In the example above, the code entered is (sort [3 7 5 1 9]), and the output is (1 3 5 7 9).

New terms and important words are shown like this: "Welcome to the Clojure Read Eval Print Loop (REPL), a command-line interface that we can use to interact with a running Clojure program."

Key parts of code snippets are highlighted as follows:

{:deps {compojure {:mvn/version "1.6.1"}
metosin/muuntaja {:mvn/version "0.6.4"}
        ring/ring-core {:mvn/version "1.7.1"}
        ring/ring-jetty-adapter {:mvn/version "1.7.1"}}
user=> (require '[muuntaja.middleware :as middleware])
=>nil

Long code snippets are truncated and the corresponding names of the code files on GitHub are placed at the top of the truncated code. The permalinks to the entire code are placed below the code snippet. It should look as follows:

kvitova_matches.clj

1  (def matches
2    [{:winner-name "Kvitova P.",
3      :loser-name "Ostapenko J.",
4      :tournament "US Open",
5      :location "New York",
6      :date "2016-08-29"}
7     {:winner-name "Kvitova P.",
8      :loser-name "Buyukakcay C.",
9      :tournament "US Open",
10     :location "New York",
11     :date "2016-08-31"}

Before You Begin

Each great journey begins with a humble step. Our upcoming adventure in the land of Clojure is no exception. Before we can do awesome things with data, we need to be prepared with a productive environment. In this section, we will see how to do that.

Installing Java

Before installing Clojure, you need to make sure that you have the Java Developer's Kit (JDK) installed on your computer. For Mac and Linux users, prebuilt binaries are a few keystrokes away. On Mac, for Homebrew users, you can just type:

$ brew install openjdk

On Debian-based Linux distributions, you can check which version is available by typing the following:

$ apt-get search openjdk

Depending on the output, you can then type something like:

$ sudo apt-get install openjdk-11-jdk

Clojure does not require a particularly recent version of the JDK.

For Windows, you can download the OpenJDK installer here: https://packt.live/3aBu1Qg. Once you have the installer, click on it to run, then follow the instructions.

Installing Clojure

Once you have a working JDK on your system, setting up Clojure is easy with the Leiningen tool.

  1. Copy the appropriate version (Windows or Mac/Linux) from the Leiningen home page, here: https://leiningen.org/.
  2. Place Leiningen in a directory that is part of your system's $PATH and make it executable.
  3. On Mac or Linux, this means putting it in a directory such as ~/bin and calling chmod:
    $ chmod +x ~/bin/lein
  4. On Windows, to change the $PATH variable, go to Control Panel > User Accounts > User Accounts and click on Change My Environment Variables. In the pane showing the user variables for your personal user account, click on Path and then choose Edit.
    Figure 0.1: User accounts

    Figure 0.1: User accounts

  5. Click on New to add a line, then type in the path to your new bin directory:
    Figure 0.2: Adding the path in bin directory

    Figure 0.2: Adding the path in bin directory

  6. Now that Leiningen is installed and executable, from the command line, simply type:
    $ lein

    Leiningen will fetch Clojure and all the libraries it needs to manage Clojure. And now, by simply typing lein repl, you'll have your very first Clojure REPL:

Figure 0.3: REPL started

Figure 0.3: REPL started

Editors and IDEs

While you can certainly do a lot with a REPL running in a console, it's much more convenient to integrate the Clojure REPL into your favorite editor. Plugins exist for just about every editor and environment out there, from Vim to Emacs and from IntelliJ to Electron or Visual Studio Code.

We can't cover all the possible environments here, but we recommend using the coding tools you are already familiar with and adding a Clojure plugin. The best code editor is the one you enjoy using. As long as there is a Clojure plugin for it, you should be up and running in no time.

Installing the Code Bundle

Download the code files from GitHub at https://packt.live/2vbksal. Refer to these code files for the complete code bundle.

If you have any issues or questions about installation, please email us at [email protected].

The high-quality color images used in book can be found at https://packt.live/2O5EzNX