Living Clojure
by Carin Meier
Copyright 2015 Carin Meier. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Editor: Meghan Blanchette
- Production Editor: Matthew Hacker
- Copyeditor: Kim Cofer
- Proofreader: Jasmine Kwityn
- Indexer: WordCo Indexing Services, Inc.
- Interior Designer: David Futato
- Cover Designer: Ellie Volckhausen
- Illustrator: Rebecca Demarest
- April 2015: First Edition
Revision History for the First Edition
- 2015-04-09: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781491909041 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. Living Clojure, the cover image of a water rail, and related trade dress are trademarks of OReilly Media, Inc.
While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-491-90904-1
[LSI]
Dedication
Dedicated to Jim Weirich (19562014), whose love of life and learning inspired so many.
Preface
The inspiration for this book comes from two things. The first and most direct one is from my years of learning, exploring, and finally working professionally in Clojure. The second, more indirect one, is from my experience of taking up running for the first time. My first attempt on my own was an utter failure. I thought that maybe I just wasnt cut out to run. Luckily, I was introduced to a great program called Couch to 5k. The creators of the app realized that the most common reason people failed to achieve their fitness goals was because they were trying to do too much, too fast. To solve this problem, a gradual, 8-week training program was developed to help users achieve the goal of running for 30 minutes without stopping. Although it wasnt easy, I managed to successfully complete the program and could run for a 30-minute stretch.
A few weeks later, I was with a group of fellow developers after attending a community user group. The conversation turned to Clojure. A few of us were discussing how much we enjoyed using the language, although one of our friends complained how he had tried to learn Clojure, but just couldnt get it. I found out that he had tried to cram in learning it all in one weekend. Too much, too soon. The problems of learning how to run did not seem that much different from the problem of learning a new language. The process of learning a new language also involves learning a new way to think training the brain to process inputs and solve problems in a new way. This doesnt happen overnight. It comes, like training for running, after building up with practice.
This book combines these inspirations. The result is both a primer on learning Clojure and a structured training program to build up your brain to think a new way.
Who This Book Is For
Are you looking for a gentle introduction to Clojure? Do you already have a background in another programming language? Perfect! This book is for you. We will be covering programming concepts as they relate to Clojure and its unique design. We will also touch on object-oriented programming and concepts in comparison to functional programming. In the dicussion, we will be assuming familiarity with object-oriented development. So, if you are coming from another language like Ruby or Java, you should feel right at home. If you are a newer programmer, this book will be best paired with a more general introduction or reference to programming concepts.
If you are a language expert, or want a deep dive into every nook and cranny of Clojure, this book is most likely not for you. We will be concentrating on the major aspects of the language, not the minutiae, with the goal being to learn how to think Clojure.The same philosophy goes for tools and libraries in the Clojure ecosystem. We are striving for a primer that will present an overall holistic view of the most common and pragmatic parts, so that after finishing the book you will be comfortable and ready to start living Clojure.
How to Use This Book
The structure of this book built around training with knowledge and practice to live Clojure. Just like the inspirations for it, it is composed of two main parts. is the Living Clojure Training Program. It will take you through a weekly training program designed to give you the practice, knowledge, and tools you need to get up and running and thriving as a Clojure developer.
The first half of the book will introduce the language, along with some code examples. There are a few important things to keep in mind, as outlined in the following subsections.
Do Try the Examples
I know it is tempting to read through the book as fast as possible. But actually typing in the code examples and seeing the magic for yourself will help your understanding grow.Take the time to try out the new concepts and commands for yourself. We will be walking through setting up your environment to run Clojure later in this Preface.
Dont Feel Overwhelmed
We are going to cover a lot of material during the first half of the book. It is not all going to sink in right away. That is perfectly fine! In fact,this book was designed for that. The second half of the book is a training program that builds on the overview so that the basic language concepts really sink in with use.Once you start the training program, and questions start to come up, you will have all the tools and general knowledge to know where to look for the solutions.
Again, it takes time to learn to think in a new way. Be patient.
Dont Worry About the Parens
A common initial concern from people looking at Clojure is about the parentheses.
Dont worry about the parens.
Really.
After you get into Clojure with a good editor that supports a paredit mode that is, a mode that always inserts the matching parens for you they will seem to disappear. We will cover choosing and setting up an editor later on in .
The simplicity and elegance that the parens structure gives to Clojure is one of its main advantages. In fact, some people even come to think of the parens as a hug for their code.
One More Thing Have Fun!
Clojure is a delightful language to use. Learning a new language is an exciting adventure, full of wonder. Embrace it.
Code examples can sometimes be stodgy, dry, full of meaningless numbers, and quite frankly, boring. This book strives to fight against this by using thepower of storytelling to strengthen the code examples. Our brains become more active during stories. In fact, studies have shown that learning actually increaseswhen problems are presented through a narrative. Thus, we are going to call upon a famous story to accompany us through this book: