Practical Clojure / Edition 1

Paperback (Print)
Buy New
Buy New from
Used and New from Other Sellers
Used and New from Other Sellers
from $4.46
Usually ships in 1-2 business days
(Save 91%)
Other sellers (Paperback)
  • All (19) from $4.46   
  • New (12) from $6.34   
  • Used (7) from $4.46   


This book is the first definitive reference for the Clojure language, providing both an introduction to functional programming in general and a more specific introduction to Clojure’s features. This book demonstrates the use of the language through examples, including features such assoftware transactional memory (STM)and immutability, which may be new to programmers coming from other languages.

  • Overview of functional programming and description of what sets Clojure apart from other languages
  • Detailed explanation of Clojure’s special features
  • Examples of real-world tasks that are well-suited to Clojure’s capabilities, starting with simple tasks and moving on to more complex applications

What you’ll learn

  • What Clojure is—more than just another Lisp
  • How to set up a Clojure environment
  • The structure and syntax of a Clojure program
  • Java interoperability
  • How to use Clojure for real-world tasks
  • Common idioms of Clojure code

Who this book is for

There are two audiences for this book: any technical person desiring to know what Clojure is and why they might want to use it, and any programmer desiring to learn and use the language. The goals of these two audiences mesh nicely, given that Clojure has several new, cutting-edge features that programmers are unlikely to have encountered before.

Table of Contents

  1. The Clojure Way
  2. The Clojure Environment
  3. Controlling Program Flow
  4. Data in Clojure
  5. Sequences
  6. State Management
  7. Namespaces and Libraries
  8. Metadata
  9. Multimethods and Hierarchies
  10. Java Interoperability
  11. Parallel Programming
  12. Macros and Metaprogramming
  13. Datatypes and Protocols
  14. Performance
Read More Show Less

Product Details

  • ISBN-13: 9781430272311
  • Publisher: Apress
  • Publication date: 6/7/2010
  • Edition description: 2010
  • Edition number: 1
  • Pages: 232
  • Product dimensions: 7.40 (w) x 9.20 (h) x 0.80 (d)

Meet the Author

Luke VanderHart is a professional software developer living and working in Washington, D.C. as a consultant with NuWave Solutions. He has more than five years of experience working with the Java platform, and has worked on programs ranging from distributed client-server networks serving and synchronizing semantic XML data, to GUI development using Java Swing, to enterprise web portals serving tens of thousands of pages per day. He is a very active member of the Clojure community.

bStuart Sierra/b is an actor, writer, musician, and programmer in New York City. As assistant director of the Program on Law and Technology at Columbia University, he was the lead developer of the groundbreaking legal search engine, one of the first production web sites using Clojure. He is the author of many popular open-source Clojure libraries, including a testing framework, I/O utilities and an HTTP client. Sometimes he blogs at

Read More Show Less

Table of Contents

About the Authors

About the Technical Reviewer


Chapter 1 The Clojure Way 1

Clojure's Philosophy and Special Features 1

A Next-Generation Language 1

Dynamic and Powerful (Yes, It's a Lisp) 1

The Java Platform 2

Functional Programming 2

Purely Functional Programming 4

Clojure's Compromise 6

Immutability 7

What about Object-Oriented Programming? 9

State Management 11

State and Identity 12

Software Transactional Memory 13

Summary 15

Chapter 2 The Clojure Environment 17

"Hello World" in Clojure 17

Clojure Forms 18

Literals 18

Symbols 19

Composite Forms 19

Special Forms 19

Writing and Running Source Files 20

Vars, Namespaces, and the Environment 21

Symbols and Symbol Resolution 23

Symbol Names 23

Symbol Resolution and Scope 24

Namespaces 24

Declaring Namespaces 25

Referencing Namespaces 25

Structuring Source Files 26

Summary 26

Chapter 3 Controlling Program Flow 29

Functions 29

First-Class Functions 29

Defining Functions with fn 29

Defining Functions with defn 31

Functions of Multiple Arities 31

Functions with Variable Arguments 32

Shorthand Function Declaration 33

Conditional Expressions 34

Local Bindings 35

Looping and Recursion 36

Tail Recursion 39

Deliberate Side Effects 42

Using do 42

Side Effects in Function Definitions 43

Functional Programming Techniques 43

First-Class Functions 43

Closures 46

Currying and Composing Functions 46

Putting It All Together 48

Chapter 4 Data in Clojure 51

How to Represent and Manipulate Data 51

Nil 52

Primitive Types 52

Numbers 52

Strings 57

Boolean 60

Characters 61

Keywords 61

Collections 62

Lists 63

Vectors 64

Maps 66

Sets 71

Summary 72

Chapter 5 Sequences 73

What Are Sequences? 73

Sequenceable Types 75

Anatomy of a Sequence 75

Constructing Sequences 76

Lazy Sequences 77

An Example of Laziness 78

Constructing Lazy Sequences 80

Lazy Sequences and Memory Management 82

The Sequence API 83

Sequence Creation 83

Summary 95

Chapter 6 State Management 95

State in an Immutable World 95

The Old Way 95

State and Identity 96

State and Identity in Clojure 96

Refs and Transactions 97

Creating and Accessing refs 98

Updating refs 98

Atoms 104

Using Atoms 104

When to Use Atoms 105

Asynchronous Agents 105

Creating and Updating Agents 105

Errors and Agents 107

Waiting for Agents 108

Shutting Down Agents 108

When to Use Agents 109

Vars and Thread-Local State 109

When to Use Thread-Local Vars 110

Keeping Track of Identities 111

Validators 111

Watches 112

Summary 113

Chapter 7 Namespaces and Libraries 115

Organizing Clojure Code 115

Namespace Basics 115

Switching Namespaces with in-ns 115

Referring to Other Namespaces 116

Loading Other Namespaces 117

Loading from a File or Stream 117

Loading from the Classpath 118

Loading and Referring Namespaces in One Step 120

Importing Java Classes 120

Bringing It All Together: Namespace Declarations 121

Symbols and Namespaces 121

Namespace Metadata 122

Forward Declarations 122

Namespace-Qualified Symbols and Keywords 122

Constructing Symbols and Keywords 123

Public and Private Vars 123

Advanced Namespace Operations 124

Querying Namespaces 124

Manipulating Namespaces 125

Namespaces As References 126

Summary 126

Chapter 8 Metadata 127

Reading and Writing Metadata 127

Metadata-Preserving Operations 128

Read-Time Metadata 129

Metadata on Vars 129

Type Tags 131

Private Vars 131

Metadata on Reference Types 131

Summary 131

Chapter 9 Multimethods and Hierarchies 133

Multimethods 133

Multiple Dispatch 135

Default Dispatch Values 135

Hierarchies 136

Querying Hierarchies 137

Hierarchies with Multimethods 137

Hierarchies with Java Classes 138

More Hierarchy Queries 139

Resolving Conflicts 139

Type Tags 141

User-Defined Hierarchies 141

Summary 142

Chapter 10 Java Interoperability 143

Calling Java from Clojure 143

Java Interop Special Forms 143

Java Interop Preferred Forms 144

Clojure Types and Java Interfaces 145

Java Arrays 146

Calling Clojure from Java 148

Loading and Evaluating Clojure Code 149

Using Clojure Functions and Vars 149

Creating Java Classes 150

Proxying Java Classes 150

Generating Java Classes 151

Summary 157

Chapter 11 Parallel Programming 159

Parallelism in Clojure 159

Agents 159

Agent Thread Pools 159

Agent Example 160

Concurrent Agent Performance 161

Concurrency Functions 161

Overhead and Performance 162

Futures and Promises 163

Futures 163

Promises 164

Java-based Threading 165

Creating a Thread 165

Summary 166

Chapter 12 Macros and Metaprogramming 167

What Is Metaprogramming? 167

Code vs. Data 167

Homoiconicity 167

Macros 168

Working with Macros 169

Code Templating 171

Generating Symbols 172

When to Use Macros 173

Using Macros 173

Using Macros to Create DSLs 177

Summary 178

Chapter 13 Datatypes and Protocols 179

Protocols 179

Protocols As Interfaces 180

Datatypes 180

Implementing Protocols and Interfaces 181

In-Line Methods 181

Extending Java Interfaces 182

Datatypes As Classes 183

Extending Protocols to Pre-Existing Types 183

Extending Java Classes and Interfaces 184

Reifying Anonymous Datatypes 184

Working with Datatypes and Protocols 185

A Complete Example 186

Advanced Datatypes 186

Summary 187

Chapter 14 Performance 189

Profiling on the JVM 189

General Tips for Java Performance 189

Simple Profiling with Time 190

Using Java Profiling Tools 190

Memoization 191

Reflection and Type Hints 191

Working with Primitives 193

Loop Primitives 193

Unchecked Integer Arithmetic 194

Primitive Arrays 195

Transients 195

Var Lookups 196

Inlining 197

Macros and definline 197

Summary 197

Index 199

Read More Show Less

Customer Reviews

Average Rating 5
( 1 )
Rating Distribution

5 Star


4 Star


3 Star


2 Star


1 Star


Your Rating:

Your Name: Create a Pen Name or

Barnes & Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation


  • - By submitting a review, you grant to Barnes & and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Terms of Use.
  • - Barnes & reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously

    If you find inappropriate content, please report it to Barnes & Noble
    Why is this product inappropriate?
    Comments (optional)