Programming in Clojure


This book is no longer available for sale.

Programming in Clojure

Quickly get up to speed with this modern and powerful Lisp based JVM language

About the Book

Hello, thanks for stopping by and checking out my book about the Clojure programming language. If you have any questions then please feel free to contact me and I'll try to get back to you within a reasonable time frame.

Why write this book?

After I finished writing the (printed) book "Pro Vim" for Apress Publishing, I had decided I didn't want to write another book because quite frankly it can be a bit soul destroying. You make no money from it and it takes an unbelievable amount of time and effort to write something of decent quality.

So why go through that process again?

Well, I felt it needed to be written. I've bought lots of Clojure books - both printed and self-published (either via Leanpub or other means) - and there are some great books out there, but I felt they were overly complicated and focused on things I as a beginner Clojure developer either wasn't interested in or was stuff I was never going to use (and not just me, other people I know who have read the same books would claim a similar feeling, and in some cases felt they were being spoken down to by the author).

So my aim with this book was to make it useful for people like me (at the end of the day who else are you better posed to write for than yourself). But more so, I've always held the belief that I am the lowest common denominator; i.e. if I can understand it, then anyone can.

Who's the book aimed at?

This book is aimed at anyone who has written code. Not back-end or front-end but anyone who has written code. Where ever possible I try not to assume the reader has knowlege of the same things I do, and so I aim to explain things in as simple a form as possible (again, if I can understand it, then anyone can).

This book is also useful for anyone that looked at Clojure in the past and then was put off by the idea it was too complicated. Clojure is just a programming language like any other. The fact that it looks a little different (i.e. it's a Lisp based language - I cover this in the book) doesn't mean it's difficult to learn or write.

I was worried when I first saw Clojure because of a few reasons, but the main two were: it looked complex! but when I learnt the syntax I realised how amazingly straight forward it actually is and second: the amount of parenthesis! but almost immediately after writing some Clojure code for the first time I realised how not a problem this was and in fact Lisp code can be really beautiful, concise and elegant. 

Don't be fooled into thinking this is just a top level look at the language, it's not! For a small read (152 pages at last count) it goes quite deep in places where I feel it necessary, and that's in order to help you to really understand specific features within the language. Only then can you properly take advantage of these features, when you know them intimately.

What's inside?

I kick-start with what Clojure is and why you should care (many of you may have heard of it, and that it's functional but not much else). Unlike some books on the subject I don't waste pages and pages on this because quite frankly you want to get started learning Clojure syntax and how to get up and running, not learn the history of the language and where its inspirations come from (hell that's what Google's for).

I move swiftly onto data structures and the general language syntax, then to functional programming (FP) concepts. Although not critical to getting code to work, understanding FP is essential for writing concise and elegant code. I then discuss the concept of sequences, destructuring, pattern matching, polymorphism (yep, you know, that word you might have heard used a lot in Object-Oriented design).

I spend a fair amount of time on the subject of concurrency, as it's an important feature and selling point of the Clojure language. Everything from delays, promises, futures, atoms, locks, agents, refs, channels - and I aim to make understanding the when as important as the what (in the beginning, writing my first Clojure application I didn't really understand the difference between let's say an atom or a ref, or what the difference was between promises and futures? nothing I read on the internet really helped to explain it in a simple/clear way - so this was another reason I wanted to write this book).

I discuss how Namespaces work in Clojure (again, this is an underplayed important part of writing applications); I explain how you can add your own features into the language thanks to its built-in Macro system. I also look at Object-Oriented Programming techniques available in Clojure (yes Clojure aims to be a functional language, but it's not a "pure" one, and on top of that it tries to be a bit more practical in terms of real-world usage by incorporating some useful OOP concepts too).

I then look at the popular Leiningen project management tool. Explain how it all works and how to use it (you'll pretty much use it all the time so it's important to understand how it all comes together). As part of this I explain how to build a very simple "hello world" web application to help get you started with Leiningen (the idea being that web applications are a book unto itself - but learning the basics really sets you up properly for building all types of applications using Leiningen).

We'll then detour slightly from the use of a REPL (which I use for the majority of the book - as it helps make it easier to test example code I present in the book) and I'll explain how you can write and run code directly within Vim (a popular text editor - see Pro Vim if you'd like to learn more)

Finally, I show you how to create a simple command line application, just to demonstrate the diversity of the Clojure eco-system.

What's with the cover art?

Clojure to me always seemed scary from the outside? When reading some Clojure books I felt a little like I was stuck in a little dingy lost out in choppy waters and so this cover seemed to represent the mood I think a lot of people have when they come to Clojure initially.

But the specific imagery has a "hopeful" quality to it. You have the dark brooding sea below but above clear bright skys above you, signalling sunshine and warmth. The horizon also helps give a feeling of endless breadth and possibility (much like the Clojure language itself).

Sure, it's a bit of a hippy sounding explanation - but hey, it's just a picture right. What's important is the book contents. So go read it! :-)

About the Author

Mark McDonnell
Mark McDonnell

Staff Software Engineer @BuzzFeed.

Formerly: @BBC @StormCreative.

Compassionate Listener. Polyglot. Author. Husband. Father. Jazz Guitarist. He/Him.

Table of Contents

  • What is Clojure?
    • Why should you care?
    • The name?
    • Getting started
  • Data Structures and Syntax
    • List
    • Vector
    • Map
    • Set
    • Vars and Symbols
  • Functional Programming
    • Immutability
    • Referential transparency
    • First-class functions
    • Partial Application (and Currying)
    • Recursive iteration
    • Composability
  • Sequences
    • List Comprehension
    • Sequence Abstraction
    • Lazy Sequences
  • Destructuring
  • Pattern Matching
  • Polymorphism
  • Concurrency
    • Retriable
    • Coordinated
    • Asynchronous
    • Thread Safe
    • Delay
    • Promise
    • Future
    • Atom
    • Lock
    • Agent
    • Ref
    • Channels
  • Conventions
    • Functions
    • Macros
  • Organisation with Namespaces
    • What is a namespace?
    • Loading namespaced files
    • Interning
  • Change the language via Macros
    • Expanding all the way down
    • Writing your own Macros
  • OOP
    • defprotocol
    • deftype
    • defrecord
    • Reify
  • Projects with Leiningen
    • 10 second example
    • Help!
    • Compojure
    • Templates
    • Project file
    • Different flavours of Compojure?
    • Real world example
    • Getting your code ready for deployment
    • Where to next?
  • Creating Command Line Apps
    • Running via Leiningen
    • Running via Jar
    • Running via Binary
    • Understanding the CLI code
    • Conclusion
  • Writing Clojure from Vim
    • Rainbow Parentheses
    • Sexp Mappings for Regular People
    • Fireplace
    • Alternatives
  • Summary

Other books by this author

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

80% Royalties. Earn $16 on a $20 book.

We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earnedover $13 millionwriting, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub