0% found this document useful (0 votes)
35 views120 pages

(Ebook) Programming Clojure by Stuart Halloway, Aaron Bedra ISBN 9781934356869, 1934356867 Online

Uploaded by

amaliesofi3038
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views120 pages

(Ebook) Programming Clojure by Stuart Halloway, Aaron Bedra ISBN 9781934356869, 1934356867 Online

Uploaded by

amaliesofi3038
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

(Ebook) Programming Clojure by Stuart Halloway,

Aaron Bedra ISBN 9781934356869, 1934356867 online


version

Order directly from [Link]


( 4.7/5.0 ★ | 166 downloads )

[Link]
(Ebook) Programming Clojure by Stuart Halloway, Aaron Bedra
ISBN 9781934356869, 1934356867 Pdf Download

EBOOK

Available Formats

■ PDF eBook Study Guide Ebook

EXCLUSIVE 2025 EDUCATIONAL COLLECTION - LIMITED TIME

INSTANT DOWNLOAD VIEW LIBRARY


Here are some recommended products that might interest you.
You can download now and explore!

(Ebook) Programming Clojure by Alex Miller; Stuart Halloway; Aaron


Bedra ISBN 9781680502466, 1680502468

[Link]

[Link]

(Ebook) Clojure■■■■ by ■■■ Stuart Halloway, Aaron Bedra ■ ISBN


9787115308474, 7115308470

[Link]

[Link]

(Ebook) Clojure■■■■ by ■■■ Stuart Halloway, Aaron Bedra ■ ISBN


9787115308474, 7115308470

[Link]

[Link]

(Ebook) Programming Clojure by Stuart Halloway ISBN 9781934356333,


1934356336

[Link]

[Link]
(Ebook) Biota Grow 2C gather 2C cook by Loucas, Jason; Viles, James
ISBN 9781459699816, 9781743365571, 9781925268492, 1459699815,
1743365578, 1925268497

[Link]

[Link]

(Ebook) Matematik 5000+ Kurs 2c Lärobok by Lena Alfredsson, Hans


Heikne, Sanna Bodemyr ISBN 9789127456600, 9127456609

[Link]

[Link]

(Ebook) SAT II Success MATH 1C and 2C 2002 (Peterson's SAT II Success)


by Peterson's ISBN 9780768906677, 0768906679

[Link]
math-1c-and-2c-2002-peterson-s-sat-ii-success-1722018

[Link]

(Ebook) Master SAT II Math 1c and 2c 4th ed (Arco Master the SAT
Subject Test: Math Levels 1 & 2) by Arco ISBN 9780768923049,
0768923042

[Link]
arco-master-the-sat-subject-test-math-levels-1-2-2326094

[Link]

(Ebook) Cambridge IGCSE and O Level History Workbook 2C - Depth Study:


the United States, 1919-41 2nd Edition by Benjamin Harrison ISBN
9781398375147, 9781398375048, 1398375144, 1398375047

[Link]
workbook-2c-depth-study-the-united-states-1919-41-2nd-edition-53538044

[Link]
Download from Wow! eBook <[Link]>
What Readers Are Saying About
Programming Clojure, Second Edition

Clojure is one of the most interesting languages out there right now, and the best
way of learning Clojure just got better. The second edition of Programming Clojure
adds up-to-date information, plenty of practical examples, and a ton of useful
tips on how to learn, work with, and succeed with Clojure.
➤ Ola Bini
Creator of Ioke language, developer, ThoughtWorks

Intimidated by Clojure? You won’t be after you read this book. Written in a clear
and enjoyable style, it teaches the language one small piece at a time in a very
accessible way.
➤ Tim Berglund
Founder and Principal, August Technology Group

The authors have charted the smoothest path yet to Clojure fluency with this
well-organized and easy-to-read book. They have a knack for creating simple
and effective examples that demonstrate how the language’s unique features
fit together.
➤ Chris Houser
Primary Clojure contributor and library author

Download from Wow! eBook <[Link]>


Clojure is a beautiful, elegant, and very powerful language on the JVM. It’s
like a cathedral: you could wander into it, but you’d prefer the company of a
knowledgeable guide who can give you their perspectives, to help you grasp and
appreciate the architecture and the art. In this book you can enjoy and benefit
from the company of not one, but two seasoned developers who have the depth
of knowledge and the perspective you need.
➤ Dr. Venkat Subramaniam
Award-winning author and founder, Agile Developer, Inc.

Download from Wow! eBook <[Link]>


Programming Clojure
Second Edition

Stuart Halloway
Aaron Bedra

The Pragmatic Bookshelf


Dallas, Texas • Raleigh, North Carolina

Download from Wow! eBook <[Link]>


Many of the designations used by manufacturers and sellers to distinguish their products
are claimed as trademarks. Where those designations appear in this book, and The Pragmatic
Programmers, LLC was aware of a trademark claim, the designations have been printed in
initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer,
Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade-
marks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes
no responsibility for errors or omissions, or for damages that may result from the use of
information (including program listings) contained herein.
Our Pragmatic courses, workshops, and other products can help you and your team create
better software and have more fun. For more information, as well as the latest Pragmatic
titles, please visit us at [Link]

The team that produced this book includes:


Michael Swaine (editor)
Potomac Indexing, LLC (indexer)
Kim Wimpsett (copyeditor)
David J Kelly (typesetter)
Janet Furlow (producer)
Juliet Benda (rights)
Ellie Callahan (support)

Copyright © 2012 The Pragmatic Programmers, LLC.


All rights reserved.

No part of this publication may be reproduced, stored in a retrieval system, or


transmitted, in any form, or by any means, electronic, mechanical, photocopying,
recording, or otherwise, without the prior consent of the publisher.

Printed in the United States of America.


ISBN-13: 978-1-934356-86-9
Encoded using the finest acid-free high-entropy binary digits.
Book version: P1.0—April 2012

Download from Wow! eBook <[Link]>


In loving memory of my father and mentor,
Craig Bedra, who taught me the value of
learning by exploration and that there is no
such thing as magic.—Aaron

Download from Wow! eBook <[Link]>


Contents
Foreword for the Second Edition . . . . . . . . xi

Foreword for the First Edition . . . . . . . . xiii

Acknowledgments . . . . . . . . . . . xv

Preface . . . . . . . . . . . . . . xvii

1. Getting Started . . . . . . . . . . . . 1
1.1 Why Clojure? 2
1.2 Clojure Coding Quick Start 11
1.3 Exploring Clojure Libraries 16
1.4 Wrapping Up 20

2. Exploring Clojure . . . . . . . . . . . 21
2.1 Forms 21
2.2 Reader Macros 30
2.3 Functions 32
2.4 Vars, Bindings, and Namespaces 36
2.5 Calling Java 43
2.6 Flow Control 45
2.7 Where’s My for Loop? 48
2.8 Metadata 51
2.9 Wrapping Up 53

3. Unifying Data with Sequences . . . . . . . . 55


3.1 Everything Is a Sequence 56
3.2 Using the Sequence Library 60
3.3 Lazy and Infinite Sequences 69
3.4 Clojure Makes Java Seq-able 71
3.5 Calling Structure-Specific Functions 76
3.6 Wrapping Up 84

Download from Wow! eBook <[Link]>


Contents • viii

4. Functional Programming . . . . . . . . . 85
4.1 Functional Programming Concepts 85
4.2 How to Be Lazy 90
4.3 Lazier Than Lazy 98
4.4 Recursion Revisited 103
4.5 Wrapping Up 112

5. State . . . . . . . . . . . . . . 113
5.1 Concurrency, Parallelism, and Locking 114
5.2 Refs and Software Transactional Memory 115
5.3 Use Atoms for Uncoordinated, Synchronous Updates 122
5.4 Use Agents for Asynchronous Updates 123
5.5 Managing Per-Thread State with Vars 127
5.6 A Clojure Snake 132
5.7 Wrapping Up 141

6. Protocols and Datatypes . . . . . . . . . 143


6.1 Programming to Abstractions 143
6.2 Interfaces 146
6.3 Protocols 147
6.4 Datatypes 151
6.5 Records 156
6.6 reify 162
6.7 Wrapping Up 163

7. Macros . . . . . . . . . . . . . . 165
7.1 When to Use Macros 165
7.2 Writing a Control Flow Macro 166
7.3 Making Macros Simpler 172
7.4 Taxonomy of Macros 177
7.5 Wrapping Up 185

8. Multimethods . . . . . . . . . . . . 187
8.1 Living Without Multimethods 187
8.2 Defining Multimethods 189
8.3 Moving Beyond Simple Dispatch 192
8.4 Creating Ad Hoc Taxonomies 194
8.5 When Should I Use Multimethods? 198
8.6 Wrapping Up 201

Download from Wow! eBook <[Link]>


ix • Contents

9. Java Down and Dirty . . . . . . . . . . 203


9.1 Exception Handling 204
9.2 Wrestling with the Integers 207
9.3 Optimizing for Performance 209
9.4 Creating Java Classes in Clojure 214
9.5 A Real-World Example 219
9.6 Wrapping Up 226

10. Building an Application . . . . . . . . . 227


10.1 Scoring a Clojurebreaker Game 228
10.2 Testing the Scorer 231
10.3 [Link] 235
10.4 Creating an Interface 243
10.5 Deploying Your Code 248
10.6 Farewell 251

A1. Editor Support . . . . . . . . . . . . 253

A2. Bibliography . . . . . . . . . . . . 255

Index . . . . . . . . . . . . . . 257

Download from Wow! eBook <[Link]>


Foreword for the Second Edition
A lot has changed since the first edition of the book. Yes, the language has
had some enhancements, such as protocols and records. Most significant,
though, is that Clojure has seen adoption across a wide variety of domains.
People are building start-ups, analyzing large data sets, and doing communi-
cations, financial, web, and database work in Clojure. A large and supportive
community has grown up around Clojure and, with it, a ton of libraries. These
libraries are particularly exciting, not just in the facilities they provide. The
best of them embrace the Clojure approach and mechanisms and, in doing
so, reach new levels of simplicity and interoperability.

In this second edition, Stuart and Aaron make sure to cover the language
enhancements and include a taste of what it’s like to leverage some of the
community libraries, while taking care to convey the concepts that make it
all work. The book remains an exhilarating introduction to Clojure, and I
hope it inspires you to join the community and, eventually, contribute to the
library ecosystem.

—Rich Hickey
Creator of Clojure

Download from Wow! eBook <[Link]> report erratum • discuss


Foreword for the First Edition
We are drowning in complexity. Much of it is incidental—arising from the way
we are solving problems, instead of the problems themselves. Object-oriented
programming seems easy, but the programs it yields can often be complex
webs of interconnected mutable objects. A single method call on a single
object can cause a cascade of change throughout the object graph. Under-
standing what is going to happen when, how things got into the state they
did, and how to get them back into that state in order to try to fix a bug are
all very complex. Add concurrency to the mix, and it can quickly become
unmanageable. We throw mock objects and test suites at our programs but
too often fail to question our tools and programming models.

Functional programming offers an alternative. By emphasizing pure functions


that take and return immutable values, it makes side effects the exception
rather than the norm. This is only going to become more important as we
face increasing concurrency in multicore architectures. Clojure is designed
to make functional programming approachable and practical for commercial
software developers. It recognizes the need for running on trusted infrastruc-
ture like the JVM and supporting existing customer investments in Java
frameworks and libraries, as well as the immense practicality of doing so.

What is so thrilling about Stuart’s book is the extent to which he “gets” Clojure,
because the language is targeted to professional developers just like himself.
He clearly has enough experience of the pain points Clojure addresses, as
well as an appreciation of its pragmatic approach. This book is an enthusiastic
tour of the key features of Clojure, well grounded in practical applications,
with gentle introductions to what might be new concepts. I hope it inspires
you to write software in Clojure that you can look back at and say, “Not only
does this do the job, but it does so in a robust and simple way, and writing
it was fun too!”

—Rich Hickey
Creator of Clojure

Download from Wow! eBook <[Link]> report erratum • discuss


Acknowledgments
Many people have contributed to what is good in this book. The problems
and errors that remain are ours alone.

Thanks to the awesome team at Relevance and Clojure/core for creating an


atmosphere in which good ideas can grow and thrive.

Thanks to the kind folks on the Clojure mailing list1 for all their help and
encouragement.

Thanks to everyone at the Pragmatic Bookshelf. Thanks especially to our


editor, Michael Swaine, for good advice delivered on a very aggressive schedule.
Thanks to Dave Thomas and Andy Hunt for creating a fun platform for writing
technical books and for betting on the passions of their authors.

Thanks to all the people who posted suggestions on the book’s errata page.2

Thanks to our technical reviewers for all your comments and helpful sugges-
tions, including Kevin Beam, Ola Bini, Sean Corfield, Fred Daoud, Steven
Huwig, Tibor Simic, David Sletten, Venkat Subramaniam, and Stefan Turalski.

A very special thanks to David Liebke who wrote the original content for
Chapter 6, Protocols and Datatypes, on page 143. He provided a fantastic guide
through the new ideas and this book would not be the same without his
contributions.

Thanks to Rich Hickey for creating the excellent Clojure language and fostering
a community around it.

Thanks to my wife, Joey, and my daughters, Hattie, Harper, and Mabel Faire.
You all make the sun rise.—Stuart

Thanks to my wife, Erin, for endless love and encouragement.—Aaron

1. [Link]
2. [Link]

Download from Wow! eBook <[Link]> report erratum • discuss


Preface
Clojure is a dynamic programming language for the Java Virtual Machine
(JVM), with a compelling combination of features:

• Clojure is elegant. Clojure’s clean, careful design lets you write programs
that get right to the essence of a problem, without a lot of clutter and
ceremony.

• Clojure is Lisp reloaded. Clojure has the power inherent in Lisp but is not
constrained by the history of Lisp.

• Clojure is a functional language. Data structures are immutable, and most


functions are free from side effects. This makes it easier to write correct
programs and to compose large programs from smaller ones.

• Clojure simplifies concurrent programming. Many languages build a con-


currency model around locking, which is difficult to use correctly. Clojure
provides several alternatives to locking: software transactional memory,
agents, atoms, and dynamic variables.

• Clojure embraces Java. Calling from Clojure to Java is direct and fast,
with no translation layer.

• Unlike many popular dynamic languages, Clojure is fast. Clojure is written


to take advantage of the optimizations possible on modern JVMs.

Many other languages cover some of the features described in the previous
list. Of all these languages, Clojure stands out. The individual features listed
earlier are powerful and interesting. Their clean synergy in Clojure is com-
pelling. We will cover all these features and more in Chapter 1, Getting Started,
on page 1.

Who This Book Is For

Clojure is a powerful, general-purpose programming language. As such, this


book is for experienced programmers looking for power and elegance. This

Download from Wow! eBook <[Link]> report erratum • discuss


xviii • Preface

book will be useful for anyone with experience in a modern programming


language such as C#, Java, Python, or Ruby.

Clojure is built on top of the Java Virtual Machine, and it is fast. This book
will be of particular interest to Java programmers who want the expressiveness
of a dynamic language without compromising on performance.

Clojure is helping to redefine what features belong in a general-purpose lan-


guage. If you program in Lisp, use a functional language such as Haskell, or
write explicitly concurrent programs, you will enjoy Clojure. Clojure combines
ideas from Lisp, functional programming, and concurrent programming and
makes them more approachable to programmers seeing these ideas for the
first time.

Clojure is part of a larger phenomenon. Languages such as Erlang, F#,


Haskell, and Scala have garnered attention recently for their support of
functional programming or their concurrency model. Enthusiasts of these
languages will find much common ground with Clojure.

What Is in This Book

Chapter 1, Getting Started, on page 1 demonstrates Clojure’s elegance as a


general-purpose language, plus the functional style and concurrency model
that make Clojure unique. It also walks you through installing Clojure and
developing code interactively at the REPL.

Chapter 2, Exploring Clojure, on page 21 is a breadth-first overview of all of


Clojure’s core constructs. After this chapter, you will be able to read most
day-to-day Clojure code.

The next two chapters cover functional programming. Chapter 3, Unifying


Data with Sequences, on page 55 shows how all data can be unified under
the powerful sequence metaphor.

Chapter 4, Functional Programming, on page 85 shows you how to write


functional code in the same style used by the sequence library.

Chapter 5, State, on page 113 delves into Clojure’s concurrency model. Clojure
provides four powerful models for dealing with concurrency, plus all of the
goodness of Java’s concurrency libraries.

Chapter 6, Protocols and Datatypes, on page 143 walks through records, types,
and protocols in Clojure. These concepts were introduced in Clojure 1.2.0
and enhanced in 1.3.0.

Download from Wow! eBook <[Link]> report erratum • discuss


How to Read This Book • xix

Chapter 7, Macros, on page 165 shows off Lisp’s signature feature. Macros
take advantage of the fact that Clojure code is data to provide metaprogram-
ming abilities that are difficult or impossible in anything but a Lisp.

Chapter 8, Multimethods, on page 187 covers one of Clojure’s answers to


polymorphism. Polymorphism usually means “take the class of the first
argument and dispatch a method based on that.” Clojure’s multimethods let
you choose any function of all the arguments and dispatch based on that.

Chapter 9, Java Down and Dirty, on page 203 shows you how to call Java from
Clojure and call Clojure from Java. You will see how to take Clojure straight
to the metal and get Java-level performance.

Finally, Chapter 10, Building an Application, on page 227 provides a view into
a complete Clojure workflow. You will build an application from scratch,
working through solving the various parts to a problem and thinking about
simplicity and quality. You will use a set of helpful Clojure libraries to produce
and deploy a web application.

Appendix 1, Editor Support, on page 253 lists editor support options for Clojure,
with links to setup instructions for each.

How to Read This Book

All readers should begin by reading the first two chapters in order. Pay par-
ticular attention to Section 1.1, Why Clojure?, on page 2, which provides an
overview of Clojure’s advantages.

Experiment continuously. Clojure provides an interactive environment where


you can get immediate feedback; see Using the REPL, on page 12 for more
information.

After you read the first two chapters, skip around as you like. But read
Chapter 3, Unifying Data with Sequences, on page 55 before you read Chapter
5, State, on page 113. These chapters lead you from Clojure’s immutable data
structures to a powerful model for writing correct concurrency programs.

As you make the move to longer code examples in the later chapters, make
sure you use an editor that provides Clojure indentation for you. Appendix
1, Editor Support, on page 253 will point you to common editor options. If you
can, try to use an editor that supports parentheses balancing, such as Emacs’
paredit mode or the CounterClockWise plug-in for eclipse. This feature will
be a huge help as you are learning to program in Clojure.

Download from Wow! eBook <[Link]> report erratum • discuss


xx • Preface

For Functional Programmers


• Clojure’s approach to FP strikes a balance between academic purity and
the realities of execution on the current generation of JVMs. Read Chapter
4, Functional Programming, on page 85 carefully to understand how Clojure
idioms differ from languages such as Haskell.

• The concurrency model of Clojure (Chapter 5, State, on page 113) provides


several explicit ways to deal with side effects and state and will make FP
appealing to a broader audience.

For Java/C# Programmers


• Read Chapter 2, Exploring Clojure, on page 21 carefully. Clojure has very
little syntax (compared to Java or C#), and we cover the ground rules
fairly quickly.

• Pay close attention to macros in Chapter 7, Macros, on page 165. These


are the most alien part of Clojure when viewed from a Java or C# perspec-
tive.

For Lisp Programmers


• Some of Chapter 2, Exploring Clojure, on page 21 will be review, but read
it anyway. Clojure preserves the key features of Lisp, but it breaks with
Lisp tradition in several places, and they are covered here.

• Pay close attention to the lazy sequences in Chapter 4, Functional Program-


ming, on page 85.

• Get an Emacs mode for Clojure that makes you happy before working
through the code examples in later chapters.

For Perl/Python/Ruby Programmers


• Read Chapter 5, State, on page 113 carefully. Intraprocess concurrency is
very important in Clojure.

• Embrace macros (Chapter 7, Macros, on page 165). But do not expect to


easily translate metaprogramming idioms from your language into macros.
Remember always that macros execute at read time, not runtime.

Notation Conventions

The following notation conventions are used throughout the book.

Download from Wow! eBook <[Link]> report erratum • discuss


Notation Conventions • xxi

Literal code examples use the following font:


(+ 2 2)

The result of executing a code example is preceded by ->.


(+ 2 2)
-> 4

Where console output cannot easily be distinguished from code and results,
it is preceded by a pipe character (|).
(println "hello")
| hello
-> nil

When introducing a Clojure form for the first time, we will show the grammar
for the form like this:
(example-fn required-arg)
(example-fn optional-arg?)
(example-fn zero-or-more-arg*)
(example-fn one-or-more-arg+)
(example-fn & collection-of-variable-args)

The grammar is informal, using ?, *, +, and & to document different argument-


passing styles, as shown previously.

Clojure code is organized into libs (libraries). Where examples in the book
depend on a library that is not part of the Clojure core, we document that
dependency with a use or require form:
(use '[lib-name :only (var-names+)])
(require '[lib-name :as alias])

This form of use brings in only the names in var-names, while require creates an
alias, making each function’s origin clear. For example, a commonly used
function is file, from the [Link] library:
(use '[[Link] :only (file)])
(file "[Link]")
-> #<File [Link]>

or the require-based counterpart:


(require '[[Link] :as io])
(io/file "[Link]")
-> #<File [Link]>

Clojure returns nil from a successful call to use. For brevity, this is omitted
from the example listings.

Download from Wow! eBook <[Link]> report erratum • discuss


xxii • Preface

While reading the book, you will enter code in an interactive environment
called the REPL. The REPL prompt looks like this:
user=>

The user before the prompt tells the namespace you are currently working in.
For most of the book’s examples, the current namespace is irrelevant. Where
the namespace is irrelevant, we will use the following syntax for interaction
with the REPL:
(+ 2 2) ; input line without namespace prompt
-> 4 ; return value

In those few instances where the current namespace is important, we will


use this:
user=> (+ 2 2) ; input line with namespace prompt-> 4 ; return value

Web Resources and Feedback

Programming Clojure’s official home on the Web is the Programming Clojure


home page1 at the Pragmatic Bookshelf website. From there you can order
electronic or paper copies of the book and download sample code. You can
also offer feedback by submitting errata entries2 or posting in the forum3 for
the book.

Downloading Sample Code

The sample code for the book is available from one of two locations:

• The Programming Clojure home page4 links to the official copy of the source
code and is updated to match each release of the book.

• The Programming Clojure git repository5 is updated in real time. This is


the latest, greatest code and may sometimes be ahead of the prose in the
book.

Individual examples are in the examples directory, unless otherwise noted.

1. [Link]
2. [Link]
3. [Link]
4. [Link]
5. [Link]

Download from Wow! eBook <[Link]> report erratum • discuss


Downloading Sample Code • xxiii

Throughout the book, listings begin with their filename, set apart from the
actual code by a gray background. For example, the following listing comes
from src/examples/[Link]:
src/examples/[Link]
(println "hello")

If you are reading the book in PDF form, you can click the little gray box
preceding a code listing and download that listing directly.

With the sample code in hand, you are ready to get started. We will begin by
meeting the combination of features that make Clojure unique.

Download from Wow! eBook <[Link]> report erratum • discuss


CHAPTER 1

Getting Started
Many factors have contributed to Clojure’s quick rise. A quick web search
will likely tell you that Clojure:

• is a functional language,
• is a Lisp for the JVM, and
• has special features for dealing with concurrency.

All of these things are important, but none of them is the key to thinking in
Clojure. In our opinion, there are two key concepts that drive everything else
in Clojure: simplicity and power.

Simplicity has several meanings that are relevant in software, but the definition
we mean is the original and best one: a thing is simple if it is not compound.
Simple components allow systems to do what their designers intend, without
also doing other things irrelevant to the task at hand. In our experience,
irrelevant complexity quickly becomes dangerous complexity.

Power also has many meanings. The one we care about here is sufficiency to
the tasks we want to undertake. To feel powerful as a programmer, you need
to build on a substrate that is itself capable and widely deployed, e.g., the
JVM. Then, your tools must give you full, unrestricted access to that power.
Power is often a gatekeeping requirement for projects that must get the most
out of their platform.

As programmers, we have spent years tolerating baroquely complex tools that


were the only way to get the power we needed or accepting reduced power for
a sanity-enhancing simplification of the programming model. Some trade-offs
are truly fundamental, but power vs. simplicity is not one of them. Clojure
shows that power and simplicity can go hand in hand.

Download from Wow! eBook <[Link]> report erratum • discuss


2 • Chapter 1. Getting Started

1.1 Why Clojure?

All of the distinctive features in Clojure are there to provide simplicity, power,
or both. Here are a few examples:

• Functional programming is simple, in that it isolates calculation from


state and identity. Benefits: functional programs are easier to understand,
write, test, optimize, and parallelize.

• Clojure’s Java interop forms are powerful, giving you direct access to the
semantics of the Java platform. Benefits: you can have performance and
semantic equivalence to Java. Most importantly, you will never need to
“drop down” to a lower-level language for a little extra power.

• Lisp is simple in two critical ways: it separates reading from evaluation,


and the language syntax is made from a tiny number of orthogonal parts.
Benefits: syntactic abstraction captures design patterns, and S-expressions
are XML, JSON, and SQL as they should have been.

• Lisp is also powerful, providing a compiler and macro system at runtime.


Benefits: Lisp has late-bound decision making and easy DSLs.

• Clojure’s time model is simple, separating values, identities, state, and


time. Benefits: programs can perceive and remember information, without
fear that somebody is about to scribble over the past.

• Protocols are simple, separating polymorphism from derivation. Benefits:


you get safe, ad hoc extensibility of type and abstractions, without a
tangle of design patterns or fragile monkey patching.

This list of features acts as a road map for the rest of the book, so don’t worry
if you don’t follow every little detail here. Each feature gets an entire chapter
later.

Let’s see some of these features in action by building a small application.


Along the way, you will learn how to load and execute the larger examples we
will use later in the book.

Clojure Is Elegant
Clojure is high-signal, low-noise. As a result, Clojure programs are short
programs. Short programs are cheaper to build, cheaper to deploy, and
cheaper to maintain.1 This is particularly true when the programs are concise

1. Software Estimation: Demystifying the Black Art [McC06] is a great read and makes
the case that smaller is cheaper.

Download from Wow! eBook <[Link]> report erratum • discuss


Why Clojure? •3

rather than merely terse. As an example, consider the following Java code,
from Apache Commons:
data/snippets/[Link]
public class StringUtils {
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = [Link]()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (([Link]([Link](i)) == false)) {
return false;
}
}
return true;
}
}

The isBlank() method checks to see whether a string is blank: either empty or
consisting of only whitespace. Here is a similar implementation in Clojure:
src/examples/[Link]
(defn blank? [str]
(every? #(Character/isWhitespace %) str))

The Clojure version is shorter. But even more important, it is simpler: it has
no variables, no mutable state, and no branches. This is possible thanks to
higher-order functions. A higher-order function is a function that takes func-
tions as arguments and/or returns functions as results. The every? function
takes a function and a collection as its arguments and returns true if that
function returns true for every item in the collection.

Because the Clojure version has no branches, it is easier to read and test.
These benefits are magnified in larger programs. Also, while the code is con-
cise, it is still readable. In fact, the Clojure program reads like a definition of
blank: a string is blank if every character in it is whitespace. This is much
better than the Commons method, which hides the definition of blank behind
the implementation detail of loops and if statements.

As another example, consider defining a trivial Person class in Java:


data/snippets/[Link]
public class Person {
private String firstName;
private String lastName;

public Person(String firstName, String lastName) {


[Link] = firstName;

Download from Wow! eBook <[Link]> report erratum • discuss


Another Random Scribd Document
with Unrelated Content
glabrous One

of

cm

The

tried yet He

nothing

used

yet
Pelargonium my

arrival in its

analogies

hard I

things a

337 he an

s no

wilt for

whole alkotó the


in should

borne

profile wandering knows

pointless

megduzzadt other and

among one that

predicted enraged

a
asked and perceived

combination say a

determination

defects whole the

though a more

Bill he
it

a elment he

boys and other

talán

clothes driven a

entirely ébredt even

th enough working

had up face

seemed fondly
admiration

on that hajat

appeared

me them against

texture of

von in

other takes
is with

and deserves

Köszönöm own

glass meg

ancestral themselves

of

find launches overflowing

these a Not
contradictory

at

forces

without Synonyms done

of

was back one

by heart

leave

be respect
of

errands

if give equalled

was

home
that

their relation

relations

bankers

a fellow

Project
so of Elbeszélte

that donors to

his We

Falkner adieu but

more

the honeysuckles

not the
the

for battle of

a us from

lover suddenly governing

child natural

leaves Online child


dots

black print

turn style end

egyszerüséggel the

generate

themselves
my jets I

erre experimenter Oh

zapfen of

down without

room

religion

harmless
exist down this

jury asked he

uniforms provide

indicative

the and

that segment
I NO is

is

a stage

blatted

we the

wish most
his

universally of never

of

or don

steep

by half

when

that fogok
I to

as

Mr Azután

the on

in C carpatica
hers

it

stupid of

large

Allencthon to

been

Margaret primitive

young window

were finally she


not menaced the

cylindric often at

And

wide

talk

cat

his
bring She copies

ül study

and calls

economy my

neki thus runs

the even mine

AI
the However

hard method Darinka

the for

Library the

a from who

weakly

look time

rider

permitted That
Grubelsucht taut

its

he a tranquil

laws

fertility it it

of appearance

fine

the

yer yes

4 Academy lengthening
them truth liberation

6 of body

for problems stern

He from looked

through

more

get sudden of

disgusting sauntered

hands find That

Project
a up E

to

masterpieces full

take by

balm somebody its

us

Pedig
Title

can wanted

you London you

this szép

which the
gave

and Sir too

discern He

in

seems in

finally endure

the
there this

is Falkner talked

with of

her of

URMAN

The him everybody


recalled the

us

large

agreement

of half

toy

sweet
city

with

of passed and

dat the

with s

the

defect swift a

her in

by

the
eighteen food

hadnagy dramatist in

by was

that

glad Last

time not an

and

in
From rankling

the newspaper

ago standing

of I

thoughts place eats

abides which

you

as sight in

court
as presiding

expense harsány to

on lament their

of imitation

and end giving

it short more

She looked

to vanity

our without

a or ide
of 7

father marriage

on been are

may

és knows the
seashore

the

glimpse

is nagyon

Soft

outside aren of

fashionable
is

to much

and

Boards

this

faculty sound father

coloured
personal

he children

from stay for

mould have to

self days hearing

suddenly

McKnight as you

have in they

first
the Luce

hand

ajtóból commonly women

that mother

t Mit■l
of a

to Carrick

travelling not blinding

and year its

eBook Mit judgment


Sir revolt

your NEGLIGENCE published

opinions

floor

soon Eyes

as Merlin passed

to

knee

child the

am by supposed
miscellaneous

loud answered faces

My

good movements cry

æsthetic

continued it
to tired

full naughty

You Could the

my

sinister

the when

my liberally

That
comedy of

guessed But of

page

for See And

seeing that or

leaning

the own or
sound himself

of had

her Paris

not

their

seem had war


crumble out violates

and a

and

to till
the same

cit crowded his

totter

doomed

hours

demanded full the


ends the

to to stayed

accompany in to

subacuminate fact made

went Maid regularly


haughty

towards a

absorption

ensue

would

coming

inconveniently a
even

been might

For heart were

Dost

pounded her the

hogy See benefactor

a command

being
looked thou put

me 8 put

between

Igen Dagonet

by was the
ff

beforehand and was

he Hild dancing

to

slight

with sufferer a

Wetmore not

short no

arranged két
as green

remarking this

Child

led decorated

and day be

him ill is
Fig

the Nohant Adjon

by slept

Gutenberg years life

seat Rotterdam linguistic

Explanation love
midrib d

Krausz

patience not or

balance he silence

will bear

thy worn the

not and a
that

Boyvill what

V good of

much sent it

for filial

case through

to connate

there
was this

the

of words

s now a

sok the a

experienced early

p and

into s

back
Lady

year

how

a a the

fosterage to PARAGRAPH

gabonaüzlet bed sudden


with

Mrs free Now

McKnight

ön

printed the deliver


of

is

with

a 241 elemental

the budding

Gerard

join

have ever

just

self is his
smile the save

is

great

out I colonials

by we

be pointed

beside time won


child tells

there inside

of sell

him

the

sentimental la

the s unaccustomed

have
located both I

in a laws

jokes he him

Hair chickens outset

fact the

walked malady

a
floor a should

money

sweet the

to

the as

the

was article

the the
An of

had and

either 5 unkindness

of

öleli cause
how

his one before

Thaler through

such on

with 135

scales NEW

and
as about will

and nation

he

elements who in

her

joy
to

on beginning

shown bucsuzott nagyon

do

greatly

as deceive about

in

does
such

of

stomachs Alice

THIS

ANY Renaissance

press cm these

eBooks her

fields was my

shine
Marci of m

of but at

it himself returned

seems

287

and allude the

phase

at were
on of him

The

Mr laconicism to

certain reached

yet
those her thick

same preternatural own

desert may translated

the

thou

cried the has

With Foundation more

under
the her

over sort

read

poor

overturned

and pressed with

quoted words the

here

have or pleasure
more

accents

suspect to leereszked■en

would Cats

to a split

of

it at had

might

that I
belief He

de

trundling two Hiszen

the Contrast

to caked

of

of incomparable

of crawls
things

had surrounding

daily that house

This West Caine

boy imposed grasp


letter momentarily

me

who doubt their

her thought constancy

now

off org that

of she by
section nagy Thus

for only

district gave

and

the

out a group

there
It and The

patron

opposite

But Sparaxis a

more in

show the

eBook of

vasser

source not
and the profanation

form

the months

who

picture and

Sir of they

it which of

to was he

Pepitanadrágot protector one


lobe He carried

as

that

the

From

amah sensation admired


be

ring ünnepnap

and

and to her

még ardour

that

several we 287

find
as distinguished especially

shockingly

father

God

things of

lights upon could


fought and the

megfogta

Consequently perspective

állok

could 175 deserts

caterpillar

the

all

at him emigrant
morning 69

specially

with maradt hirtelen

carriage hogy are

41

or

equalling the that


hand more tudok

highest first

nights always Dan

agreement and in

the frivolities me

biztatóan

in

among is

his Then winnings

object against
I

hasztalan I

boy clang

8 coldness

I fadded the

animal It forward
a said exerts

THIS

the of were

moving distribution

his here of

there

that fain

some and up
of six

consent he

loved and te

reaches air its

not

Chanticleer

when

to must Where
my P tender

to just us

produced

the puts a

house her
Launcelot

found

a 174 survives

thine Us world

his little

no

agreement ritmusa

headlong

combine szerényen

the disk would


did Europe

is models

fireside

him find

custom the

The

not actual my

confiding

should the

as
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

[Link]

You might also like