Book Image

Clojure Programming Cookbook

Book Image

Clojure Programming Cookbook

Overview of this book

When it comes to learning and using a new language you need an effective guide to be by your side when things get rough. For Clojure developers, these recipes have everything you need to take on everything this language offers. This book is divided into three high impact sections. The first section gives you an introduction to live programming and best practices. We show you how to interact with your connections by manipulating, transforming, and merging collections. You’ll learn how to work with macros, protocols, multi-methods, and transducers. We’ll also teach you how to work with languages such as Java, and Scala. The next section deals with intermediate-level content and enhances your Clojure skills, here we’ll teach you concurrency programming with Clojure for high performance. We will provide you with advanced best practices, tips on Clojure programming, and show you how to work with Clojure while developing applications. In the final section you will learn how to test, deploy and analyze websocket behavior when your app is deployed in the cloud. Finally, we will take you through DevOps. Developing with Clojure has never been easier with these recipes by your side!
Table of Contents (16 chapters)
Clojure Programming Cookbook
About the Authors
About the Reviewer


This is a book designed to make you want to try things. It will take you back to the days when you had tons of energy and were ready to just start and make things happen.

Clojure is a one of a kind programming language, where the core is very important, and you can just write efficient code, based on strong building blocks.

With the upcoming release of Clojure 1.9, this language is ahead of other languages due to its simplicity and conciseness. So whether you are embarking on a weekend programming project with Raspberry Pi or getting ready to challenge the world with your new start-up, the tons of good recipes provided in this book are for you.

This book can mostly be read in any order, but make sure that you go through the first few recipes to review the basics of Clojure.

Make things happen and enjoy every piece of it.

What this book covers

Chapter 1, Live Programming with Clojure, helps you review how the Clojure basics work with different data types, how the code control flows, and also how to integrate third-party libraries. You probably know this already, but you simply cannot start a good cookbook without a strong base.

Chapter 2, Interacting with Collections, covers selecting, filtering, transforming, merging, joining, and how to become lazy. Laziness is one of the greatest Clojure feature, and we want to make sure that you will always be lazy enough.

Chapter 3, Clojure Next, introduces you to macros, advanced macros, transducers (what transducers are, anyway), and logic programming. It covers preprocessing and postprocessing of Clojure data as code features.

Chapter 4, File Access and the Network, covers StreamingQueues and real-time networking, which gives you the power to think about distributed systems and how to connect your IoT devices. It also covers files and how to manipulate files to help you with lightweight processing for interconnections.

Chapter 5, Working with Other Languages, introduces you to the basic Clojure tooling that allows you to easily integrate code written in other languages, such as Java, Scala, and .NET, all the way to Clojure on the JavaScript runtime, ClojureScript.

Chapter 6, Concurrency and Parallelism, helps you learn multiple ways of splitting jobs among Clojure processes, interacting with Scala-like actors, and to also sharing states between different instances. Of course, this chapter covers the simplest way of writing a parallel code in Clojure, core.async.

Chapter 7, Advanced Tips, helps you if you want to know how to perform not only pair programming but also shared real-time pair programming. You will also see how to hack the Clojure source code yourself.

Chapter 8, Web Applications, takes you through web applications, web services, or API endpoints or whatever you call them. You also learn how to use nice-looking widgets with Vaadin, how to write rest-like APIs with Liberator, and use Immutant, a set of server-side services at the finger of your Clojure code. 

Chapter 9, Testing, not only helps you with validating your code but also with using random pattern testing for the cases you had not thought about. This chapter also introduces behavior-driven development in Clojure with some OpenCV validation, and finally, explains how to benchmark the speed of your code.

Chapter 10, Deployment and DevOps, covers dockerizing your Clojure code, making it run locally as it would in the Cloud, putting real-time monitoring in place for your critical services, and finally, your favorite Amazon tricks are also covered here.

What you need for this book

You need a bit of time, a computer, and a text editor. That's all this book requires. Of course, previous functional programming experience is a big plus, but having motivation and energy is pretty much all you need.

Who this book is for

This book is for people who want to make things happen. Follow along the lines of the zillions of code samples included in this book, and go ahead from there. Each example should make you want to try things around, and bring up some new ideas. This book should start by being just a cookbook, but by the end, will become your cookbook.


In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).

To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.


In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "However, declaring libraries in project.clj is much simpler than doing it in the pom.xml file of Maven."

A block of code is set as follows:

String str = "Hello ".concat("world !");

Any command-line input or output is written as follows:

$ mv lein ~/bin

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Go to the Data Buckets tab and click on the Create New Data Bucket option."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at .

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at We also have other code bundles from our rich catalog of books and videos available at Check them out!

Downloading the color images of this book 

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.