About This eBook
ePUB is an open, industry-standard format for eBooks. However, support of ePUB and its many features varies across reading devices and applications. Use your device or app settings to customize the presentation to your liking. Settings that you can customize often include font, font size, single or double column, landscape or portrait mode, and figures that you can click or tap to enlarge. For additional information about the settings and features on your reading device or app, visit the device manufacturers Web site.
Many titles include programming code or configuration examples. To optimize the presentation of these elements, view the eBook in single-column, landscape mode and adjust the font size to the smallest setting. In addition to presenting code and configurations in the reflowable text format, we have included images of the code that mimic the presentation found in the print book; therefore, where the reflowable format may compromise the presentation of the code listing, you will see a Click here to view code image link. Click the link to view the print-fidelity code image. To return to the previous page viewed, click the Back button on your device or app.
Clojure Recipes
Julian Gamble
New York Boston Indianapolis San Francisco
Toronto Montreal London Munich Paris Madrid
Capetown Sydney Tokyo Singapore Mexico City
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 publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at or (800) 382-3419.
For government sales inquiries, please contact .
For questions about sales outside the United States, please contact .
Visit us on the Web: informit.com/aw
Library of Congress Cataloging-in-Publication Data
Gamble, Julian, author.
Clojure recipes / Julian Gamble.
pages cm
Includes index.
ISBN 978-0-321-92773-6 (pbk. : alk. paper)ISBN 0-321-92773-7
1. Clojure (Computer program language) 2. Software patterns. I. Title.
QA76.73.C565G36 2015
005.1dc23
2015028332
Copyright 2016 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, 200 Old Tappan Road, Old Tappan, New Jersey 07675, or you may fax your request to (201) 236-3290.
ISBN-13: 978-0-321-92773-6
ISBN-10: 0-321-92773-7
Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana.
First printing, October 2015
To my amazing wife Jo-Ann...
... You Rock!
Contents
Preface
Who This Book Is For
Clojure Recipes is for people who have started on their journey into Clojure but havent quite found their feet. Ideally, you are aware that Clojure has lots of parentheses, but the prospect of integrating some libraries to build a working project is still a bit daunting.
If youre comfortable with Clojure and feel like you could easily build a project in a weekend without assistance, then this book is still useful. This is the book you give to someone in the office who is just curious, or who has seen Clojure and wants to get started but needs a helping hand.
Finally, if youre a pragmatist who just needs to get some working code running, then this book is for you. There are lots of examples for you to copy and paste to get your project working.
What This Book Is Not
Clojure Recipes is not an introduction to Clojure book. There are some really brilliant books and online resources that cover this topic area. If you want an in-depth explanation of Clojure, then read one of those. This is a learn by doing type of book.
What This Book Is About
Clojure Recipes is about the weekend project. Its about getting something running in a short amount of time. The book makes no assumptions about background knowledge of Clojure, but provides all you need in packaged bites.
The aim of the book is to provide self-contained projects that would have just enough for you to get running and see all the pieces hang together. The idea is that you can tweak and extend these projects for your needs.
Why Clojure?
So why should you use Clojure for your next project? Here are a few reasons:
Clojure focuses on isolating side effects. Whether it is regular business logic or a concurrency scenario, immutability makes your program easier to reason about and cancels out a whole class of bugs due to state mutation. You can still modify state, but the language encourages you to use it only where necessary.
Clojure was one of the first languages to make serious use of the ideas in Chris Okasakis book Purely Functional Data Structures (Cambridge University Press, 1999). The big revelation in that book was that data structures implemented in a functional way could be done with an upper bound of time O(n) on costs for reads and writes.
The benefit of these purely functional data structures was to enable another way to think about Software Transactional Memory (STM) in your program. Clojure introduces constructs like Multiversion Concurrency Control (MVCC) at the application level that previously developers typically only relied upon at the database level.
Clojure is great at concurrency. In Clojure it is easy to reason about how your program will behave in a concurrency scenario even when there are multiple processes making changes to the one data structure.
Clojure is pragmatic. Clojure runs on the JVM (in addition to the .NET CLR and JavaScript execution environments). This brings a wealth of libraries from the Java world that can be reused. Whats more, when you deploy it, it can look like a jar file so there is no need to tell anyone youre using Clojure at all!