shelve in: PRogRamming languages/Clo JuRe C C l oJuRe l JJooiinn tthhee RRaannkkss ooff nnoobbllee CCllooJJuuRRiissttss o J For weeks, months —nay!—from the very moment you u were born, you’ve felt it calling to you. At long last you’ll R be united with the programming language you’ve been f o r T h e e longing for: Clojure! Tf As a Lisp-style functional programming language, Clojure lets you write robust ho b Ra v e ber and elegant code, and because it runs on the Java Virtual Machine, you can take advantage of the vast Java ecosystem. Clojure for the Brave and True offers R a “dessert-first” approach: you’ll start play ing with real programs immediately, a as you steadily acclimate to the abstract but powerful features of Lisp and functional programming. Inside you’ll find an offbeat, practical guide to Clojure, v filled with quirky sample programs that catch cheese thieves and track glittery e vampires. A n d learn how to A n • Wield Clojure’s core functions d t Ru e • Use Emacs for Clojure development t • Write macros to modify Clojure itself R • Use Clojure’s tools to simplify concurrency and parallel programming u Clojure for the Brave and True assumes no prior experience with Clojure, the e Java Virtual Machine, or functional programming. Are you ready, brave reader, learn the ultimate to meet your true destiny? Grab your best pair of parentheses—you’re about to embark on an epic journey into the world of Clojure! language and d about the authoR a n Daniel Higginbotham has been a professional programmer for 11 years, half of that at McKinsey & i e become a better Company, where he used Clojure to build mobile and web applications. He has also contributed to l the curriculum for ClojureBridge, an organization that offers free, beginner-friendly Clojure workshops h for women. Daniel blogs about life and programming at http://flyingmachinestudios.com/, and can be ig programmer found on Twitter, @nonrecursive. He lives in Durham, North Carolina, with his wife and four cats. g i n b Covers Clojure 1.7 o requires java 1.6 or later t h a m $34.95 ($40.95 CDN) www.nostarch.com daniel higginbotham THE FINEST IN GEEK ENTERTAINMENT™ Clojure for the Brave and true Clojure for the Brave and true. Copyright © 2015 by Daniel Higginbotham. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. Printed in USA First printing 19 18 17 16 15 1 2 3 4 5 6 7 8 9 ISBN-10: 1-59327-591-9 ISBN-13: 978-1-59327-591-4 Publisher: William Pollock Production Editor: Riley Hoffman Cover Design: Beth Middleworth and Daniel and Jessica Higginbotham Cover and Interior Illustrations: Jessica Higginbotham Interior Design: Octopod Studios Developmental Editors: Hayley Baker and Seph Kramer Technical Reviewer: Alan Dipert Copyeditor: Anne Marie Walker Compositors: Riley Hoffman and Susan Glinert Stevens Proofreader: Emelie Burnette For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: 415.863.9900; [email protected] www.nostarch.com Library of Congress Cataloging-in-Publication Data Higginbotham, Daniel. Clojure for the brave and true : learn the ultimate language and become a better programmer / by Daniel Higginbotham. pages cm Includes index. Summary: "Guide to the functional programming language Clojure. Teaches tools and techniques for writing programs in Clojure. Covers how to wield and compose Clojure's core functions; use Emacs for Clojure development; write macros to modify the Clojure programming language; and use Clojure's tools to simplify concurrency and parallel programming"-- Provided by publisher. ISBN 978-1-59327-591-4 -- ISBN 1-59327-591-9 1. Clojure (Computer program language) I. Title. QA76.73.C565H54 2015 005.13'3--dc23 2015014205 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it. about the author Daniel Higginbotham has been a professional programmer for 11 years, half of that at McKinsey & Company, where he used Clojure to build mobile and web applications. He has also contributed to the curriculum for ClojureBridge, an organization that offers free, beginner-friendly Clojure workshops for women. Daniel blogs about life and program- ming at http://flyingmachinestudios.com/, and can be found on Twitter, @nonrecursive. He lives in Durham, North Carolina, with his wife and four cats. about the technical reviewer Alan Dipert first heard about Lisp when he was 10 years old. After it was described to him, he said “That sounds dumb.” In 2009, he learned Clojure and revised his opinion. Alan has designed and built Clojure systems, conducted Clojure trainings, and spoken at Clojure conferences. You can keep track of Alan’s work and recent opinions by visiting http:// tailrecursion.com/~alan or by following him on Twitter, @alandipert. For Jess Brief Contents Foreword by Alan Dipert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi Part I: EnvIronmEnt SEtuP Chapter 1: Building, Running, and the REPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Chapter 2: How to Use Emacs, an Excellent Clojure Editor . . . . . . . . . . . . . . . . . . . . . . . . 11 Part II: LanguagE FundamEntaLS Chapter 3: Do Things: A Clojure Crash Course . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Chapter 4: Core Functions in Depth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Chapter 5: Functional Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .97 Chapter 6: Organizing Your Project: A Librarian’s Tale . . . . . . . . . . . . . . . . . . . . . . . . . 125 Chapter 7: Clojure Alchemy: Reading, Evaluation, and Macros . . . . . . . . . . . . . . . . . . . .147 Chapter 8: Writing Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165 Part III: advancEd toPIcS Chapter 9: The Sacred Art of Concurrent and Parallel Programming . . . . . . . . . . . . . . . . 189 Chapter 10: Clojure Metaphysics: Atoms, Refs, Vars, and Cuddle Zombies . . . . . . . . . . . 207 Chapter 11: Mastering Concurrent Processes with core .async . . . . . . . . . . . . . . . . . . . . .233 Chapter 12: Working with the JVM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Chapter 13: Creating and Extending Abstractions with Multimethods, Protocols, and Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .265