Programming in Clojure
This book is 100% complete
Completed on 2015-12-10
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.
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! :-)
What is Clojure?
- Why should you care?
- The name?
- Getting started
Data Structures and Syntax
- Vars and Symbols
- Referential transparency
- First-class functions
- Partial Application (and Currying)
- Recursive iteration
- List Comprehension
- Sequence Abstraction
- Lazy Sequences
- Pattern Matching
- Thread Safe
Organisation with Namespaces
- What is a namespace?
- Loading namespaced files
Change the language via Macros
- Expanding all the way down
- Writing your own Macros
Projects with Leiningen
- 10 second example
- 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
Writing Clojure from Vim
- Rainbow Parentheses
- Sexp Mappings for Regular People
The Leanpub 45-day 100% Happiness Guarantee
Within 45 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms...