Reader small image

You're reading from  The Clojure Workshop

Product typeBook
Published inJan 2020
Reading LevelBeginner
PublisherPackt
ISBN-139781838825485
Edition1st Edition
Languages
Right arrow
Authors (5):
Joseph Fahey
Joseph Fahey
author image
Joseph Fahey

Joseph Fahey has been a developer for nearly two decades. He got his start in the Digital Humanities in the early 2000s. Ever since then, he has been trying to hone his skills and expand his inventory of techniques. This lead him to Common Lisp and then to Clojure when it was first introduced. As an independent developer, Joseph was able to quickly start using Clojure professionally. These days, Joseph gets to write Clojure for his day job at Empear AB.
Read more about Joseph Fahey

Thomas Haratyk
Thomas Haratyk
author image
Thomas Haratyk

Thomas Haratyk graduated from Lille University of Science and Technology and has been a professional programmer for nine years. After studying computer science and starting his career in France, he is now working as a consultant in London, helping start-ups develop their products and scale their platforms with Clojure, Ruby, and modern JavaScript.
Read more about Thomas Haratyk

Scott McCaughie
Scott McCaughie
author image
Scott McCaughie

Scott McCaughie lives near Glasgow, Scotland where he works as a senior Clojure developer for Previse, a Fintech startup aiming to solve the problem of slow payments in the B2B space. Having graduated from Heriot-Watt University, his first 6 years were spent building out Risk and PnL systems for JP Morgan. A fortuitous offer of a role learning and writing Clojure came up and he jumped at the chance. 5 years of coding later and it's the best career decision he's made. In his spare time, Scott is an avid reader, enjoys behavioral psychology and financial independence podcasts, and keeps fit by commuting by bike, running, climbing, hill walking, snowboarding. You get the picture!
Read more about Scott McCaughie

Yehonathan Sharvit
Yehonathan Sharvit
author image
Yehonathan Sharvit

Yehonathan Sharvit has been a software developer since 2001. He discovered functional programming in 2009. It has profoundly changed his view of programming and his coding style. He loves to share his discoveries and his expertise. He has been giving courses on Clojure and JavaScript since 2016. He holds a master's degree in Mathematics.
Read more about Yehonathan Sharvit

Konrad Szydlo
Konrad Szydlo
author image
Konrad Szydlo

Konrad Szydlo is a psychology and computing graduate from Bournemouth University. He has worked with Clojure for the last 8 years. Since January 2016, he has worked as a software engineer and team leader at Retailic, responsible for building a website for the biggest royalty program in Poland. Prior to this, he worked as a developer with Sky, developing e-commerce and sports applications, where he used Ruby, Java, and PHP. He is also listed in the Top 75 Datomic developers on GitHub.
Read more about Konrad Szydlo

View More author details
Right arrow

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 workshops@packt.com.

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

lock icon
The rest of the page is locked
Previous PageNext Chapter
You have been reading a chapter from
The Clojure Workshop
Published in: Jan 2020Publisher: PacktISBN-13: 9781838825485

Authors (5)

author image
Joseph Fahey

Joseph Fahey has been a developer for nearly two decades. He got his start in the Digital Humanities in the early 2000s. Ever since then, he has been trying to hone his skills and expand his inventory of techniques. This lead him to Common Lisp and then to Clojure when it was first introduced. As an independent developer, Joseph was able to quickly start using Clojure professionally. These days, Joseph gets to write Clojure for his day job at Empear AB.
Read more about Joseph Fahey

author image
Thomas Haratyk

Thomas Haratyk graduated from Lille University of Science and Technology and has been a professional programmer for nine years. After studying computer science and starting his career in France, he is now working as a consultant in London, helping start-ups develop their products and scale their platforms with Clojure, Ruby, and modern JavaScript.
Read more about Thomas Haratyk

author image
Scott McCaughie

Scott McCaughie lives near Glasgow, Scotland where he works as a senior Clojure developer for Previse, a Fintech startup aiming to solve the problem of slow payments in the B2B space. Having graduated from Heriot-Watt University, his first 6 years were spent building out Risk and PnL systems for JP Morgan. A fortuitous offer of a role learning and writing Clojure came up and he jumped at the chance. 5 years of coding later and it's the best career decision he's made. In his spare time, Scott is an avid reader, enjoys behavioral psychology and financial independence podcasts, and keeps fit by commuting by bike, running, climbing, hill walking, snowboarding. You get the picture!
Read more about Scott McCaughie

author image
Yehonathan Sharvit

Yehonathan Sharvit has been a software developer since 2001. He discovered functional programming in 2009. It has profoundly changed his view of programming and his coding style. He loves to share his discoveries and his expertise. He has been giving courses on Clojure and JavaScript since 2016. He holds a master's degree in Mathematics.
Read more about Yehonathan Sharvit

author image
Konrad Szydlo

Konrad Szydlo is a psychology and computing graduate from Bournemouth University. He has worked with Clojure for the last 8 years. Since January 2016, he has worked as a software engineer and team leader at Retailic, responsible for building a website for the biggest royalty program in Poland. Prior to this, he worked as a developer with Sky, developing e-commerce and sports applications, where he used Ruby, Java, and PHP. He is also listed in the Top 75 Datomic developers on GitHub.
Read more about Konrad Szydlo