Clojure in Action, Second Edition
Amit Rathore
  • MEAP began February 2013
  • Publication in November 2015 (estimated)
  • ISBN 9781617291524
  • 400 pages (estimated)
  • printed in black & white

Clojure in Action, Second Edition is a fully revised edition that covers the new features available in Clojure 1.5. This book provides a rapid introduction to the Clojure language, trading abstract theory for practical examples. You'll start by learning how to use Clojure as a general-purpose language. Next, you'll delve into Clojure's efficient concurrency model, which is based on the familiar database concept of Software Transactional Memory (STM). Then you'll find a new level of productivity through Clojure DSLs that will run on the JVM. Along the way you'll discover countless tips, tricks, and techniques for writing smaller, faster, safer code.

Table of Contents show full

1. Introducing Clojure

1.1. Clojure: What and Why?

1.1.1. Clojure—a modern Lisp

1.1.2. Clojure—pragmatic functional programming

1.1.3. Clojure on the JVM

1.2. Language basics

1.2.1. Lisp syntax

1.2.2. Parentheses

1.3. Host interoperation—A JVM crash course

1.3.1. Java types, classes, and objects

1.3.2. The dot and new operators

1.3.3. Threads and concurrency

1.4. Summary

2. Clojure elements: Data structures and functions

2.1. Coding at the REPL

2.1.1. The Clojure REPL

2.1.2. Hello, world

2.1.3. Looking up documentation using doc, find-doc, and apropos

2.1.4. A few more points on Clojure syntax

2.2. Clojure data structures

2.2.1. nil, truth, and falsehood

2.2.2. Characters and strings

2.2.3. Clojure numbers

2.2.4. Symbols and Keywords

2.2.5. Lists

2.2.6. Vectors

2.2.7. Maps

2.2.8. Sequences

2.3. Program structure

2.3.1. Functions

2.3.2. The let form

2.3.3. Side effects with do

2.3.4. Reader macros

2.4. Program flow

2.4.1. Conditionals

2.4.2. Logical functions

2.4.3. Functional iteration

2.4.4. The threading macros

2.5. Summary

3. Building blocks of Clojure

3.1. Metadata

3.1.1. Java Type Hints

3.2. Java exceptions: try and throw

3.3. Functions

3.3.1. Defining functions

3.3.2. Calling functions

3.3.3. Higher-order functions

3.3.4. Writing higher-order functions

3.3.5. Anonymous functions

3.3.6. Keywords and symbols

3.4. Scope

3.4.1. Vars and binding

3.4.2. The let form revisited

3.4.3. Lexical closures

3.5. Namespaces

3.5.1. The ns macro

3.5.2. Working with namespaces

3.6. Destructuring

3.6.1. Vector bindings

3.6.2. Map bindings

3.7. Reader literals

3.8. Summary

4. Multimethod polymorphism

4.1. Polymorphism and its kinds

4.1.1. Parametric Polymorphism

4.1.2. Ad hoc Polymorphism

4.1.3. Subtype Polymorphism

4.2. Polymorphism using multimethods

4.2.1. Life without multimethods

4.2.2. Ad hoc polymorphism using multimethods

4.2.3. Multiple dispatch

4.2.4. Subtype polymorphism using multimethods

4.3. Summary

5. Exploring Clojure and Java interop

5.1. Calling Java from Clojure

5.1.1. Importing Java classes into Clojure

5.1.2. Creating instances

5.1.3. Accessing methods and fields

5.1.4. Macros and the dot special form

5.1.5. Helpful Clojure macros for working with Java

5.1.6. Implementing interfaces and extending classes

5.2. Compiling Clojure code to Java byte code

5.2.1. Example—a tale of two calculators

5.2.2. Creating Java classes and interfaces using gen-class and gen-interface

5.3. Calling Clojure from Java

5.4. Summary

6. State and the concurrent world

6.1. The problem with state

6.1.1. Common problems with shared state

6.1.2. Traditional solution

6.2. Separating identities and values

6.2.1. Immutable values

6.2.2. Objects and time

6.2.3. Immutability and concurrency

6.3. Clojure’s way

6.3.1. Requirements for immutability

6.3.2. Managed references

6.4. Refs

6.4.1. Creating refs

6.4.2. Mutating refs

6.4.3. Software transactional memory

6.5. Agents

6.5.1. Creating agents

6.5.2. Mutating agents

6.5.3. Working with agents

6.5.4. Side effects in STM transactions

6.6. Atoms

6.6.1. Creating atoms

6.6.2. Mutating atoms

6.7. Vars

6.7.1. Creating vars and root bindings

6.7.2. Var bindings

6.8. State and its unified access model

6.8.1. Watching for mutation

6.9. Deciding which reference type to use

6.10. Futures and promises

6.10.1. Futures

6.10.2. Promises

6.11. Summary

7. Evolving Clojure through macros

7.1. Macro basics

7.1.1. Textual substitution

7.1.2. The unless example

7.1.3. Macro templates

7.1.4. Recap—why macros?

7.2. Macros from within Clojure

7.2.1. comment

7.2.2. declare

7.2.3. defonce

7.2.4. and

7.2.5. time

7.3. Writing your own macros

7.3.1. infix

7.3.2. randomly

7.3.3. defwebmethod

7.3.4. defnn

7.3.5. assert-true

7.4. Summary

8. More on functional programming

8.1. Using higher-order functions

8.1.1. Collecting results of functions

8.1.2. Reducing lists of things

8.1.3. Filtering lists of things

8.2. Partial application

8.2.1. Adapting functions

8.2.2. Defining functions

8.3. Closures

8.3.1. Free variables and closures

8.3.2. Delayed computation and closures

8.3.3. Closures and objects

8.3.4. An object system for Clojure

8.4. Summary

9. Protocols, records, and types

9.1. The expression problem

9.1.1. Setting up the example scenario

9.1.2. A closer look at the expression problem, and some potential solutions

9.1.3. Clojure’s multimethods solution

9.2. Examining the operations side of the expression problem

9.2.1. def-modus-operandi

9.2.2. detail-modus-operandi

9.2.3. Tracking our modus operandi

9.2.4. Error handling and trouble spots in this solution

9.3. Examining the data types side of the expression problem with protocols

9.3.1. defprotocol and extend-protocol

9.3.2. Defining data types with deftype, defrecord, and reify

9.4. Summary

10. Test-driven development and more

10.1. Getting started with TDD

10.1.1. A simple example of TDD using dates and string

10.2. Improving tests through mocking and stubbing

10.2.1. Example: expense finders

10.2.2. Stubbing

10.2.3. Mocking

10.2.4. Mocks versus stubs

10.2.5. Managing stubbing and mocking state

10.3. Organizing tests

10.3.1. The testing macro

10.3.2. The are macro

10.4. Summary

11. More macros and DSLs

11.1. A quick review of macros

11.1.1. Anaphoric macros

11.1.2. The anaphoric if

11.1.3. The thread-it macro

11.2. Shifting computation to compile time

11.2.1. Example: Rotation ciphers without macros

11.2.2. Making the compiler work harder

11.3. Macro generating macros

11.3.1. Shifting computation to compile time

11.3.2. Macro-generating macros

11.4. Domain-specific languages (DSLs)

11.4.1. DSL-driven design

11.4.2. User classification

11.5. Summary


Appendix A: Installing Clojure

A.1. Try Clojure

A.2. Clojure.jar

A.3. Leiningen

A.3.1. Adding dependencies to a Leiningen project

About the Technology

Lisp gets a bad rap for being old school. In fact, so-called "modern" languages like Python, Java, Ruby, and C# are still catching up to Lisp's built-in concurrency features, full macro support, immutable data-structures, and high-performance elegance. Clojure is a new Lisp that runs on the JVM and offers full interoperability with Java libraries. It's perfect for creating small, fast, concurrent code that maximizes the potential of multi-core processors. While Clojure is most often used for large-scale, data-heavy, mission-critical applications, it is also perfect for writing DSLs and utilities.

What's inside

  • Understand what the big deal is all about
  • Learn Clojure without Lisp's academic baggage
  • Review functional programming basics
  • Learn concurrency programming with STM
  • Master the metaprogramming possibilities with Clojure's macro system
  • Interoperate safely and effectively with Java
  • Learn how to create a non-trivial Clojure application
  • Design systems as a set of mini languages (or DSLs)

About the reader

Written for developers familiar with a programming language like C, Java, Ruby, or Python. Prior experience with Lisp is helpful but not required.

About the author

Amit Rathore has 12 years of experience building large-scale, data-heavy applications for a variety of domains, from financial systems to health care to education. He's currently the vice president and head of the Innovation Lab at Staples Global E-Commerce.

Manning Early Access Program (MEAP) Read chapters as they are written, get the finished eBook as soon as it’s ready, and receive the pBook long before it's in bookstores.
MEAP combo $49.99 pBook + eBook
MEAP eBook $39.99 pdf + ePub + kindle

FREE domestic shipping on three or more pBooks