programming clojure 2nd edition

284 520 0
programming clojure 2nd edition

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

www.it-ebooks.info 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 www.it-ebooks.info 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. www.it-ebooks.info Programming Clojure Second Edition Stuart Halloway Aaron Bedra The Pragmatic Bookshelf Dallas, Texas • Raleigh, North Carolina www.it-ebooks.info 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 http://pragprog.com . 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 tra ns mi tted, in a ny form , or by any me an s, elec troni c, mech an ical, phot oc op yi ng, 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 www.it-ebooks.info 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 www.it-ebooks.info Contents Foreword for the Second Edition . . . . . . . . xi Foreword for the First Edition . . . . . . . . xiii Acknowledgments . . . . . . . . . . . xv Preface . . . . . . . . . . . . . . xvii 1. Getting Started . . . . . . . . . . . . 1 Why Clojure? 21.1 1.2 Clojure Coding Quick Start 11 1.3 Exploring Clojure Libraries 16 1.4 Wrapping Up 20 2. Exploring Clojure . . . . . . . . . . . 21 Forms 212.1 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 Everything Is a Sequence 563.1 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 www.it-ebooks.info 4. Functional Programming . . . . . . . . . 85 Functional Programming Concepts 854.1 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 Concurrency, Parallelism, and Locking 1145.1 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 Programming to Abstractions 1436.1 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 When to Use Macros 1657.1 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 Living Without Multimethods 1878.1 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 Contents • viii www.it-ebooks.info 9. Java Down and Dirty . . . . . . . . . . 203 Exception Handling 2049.1 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 Scoring a Clojurebreaker Game 22810.1 10.2 Testing the Scorer 231 10.3 test.generative 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 ix • Contents www.it-ebooks.info 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 report erratum • discuss www.it-ebooks.info [...]... beginners learning functional programming and a pragmatic alternative to functional style when you need it Java invocation is covered in Chapter 9, Java Down and Dirty, on page 203 Clojure s approach to changing state enables concurrency without explicit locking and complements Clojure s functional core Clojure Simplifies Concurrent Programming Clojure s support for functional programming makes it easy... 1.3 Exploring Clojure Libraries Clojure code is packaged in libraries Each Clojure library belongs to a namespace, which is analogous to a Java package You can load a Clojure library with require: (require quoted-namespace-symbol) When you require a library named clojure. java.io, Clojure looks for a file named clojure/ java/io.clj on the CLASSPATH Try it: user=> (require 'clojure. java.io) -> nil The leading... ones • Clojure simplifies concurrent programming Many languages build a concurrency 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. .. www.it-ebooks.info report erratum • discuss 12 • Chapter 1 Getting Started Building Clojure Yourself You may want to build Clojure from source to get access to newer features and bug fixes Here’s how: git clone git://github.com /clojure/ clojure.git cd clojure mvn package The sample code is regularly updated to match the current development head of Clojure Check the README file in the sample code to see the revision... performance Clojure is helping to redefine what features belong in a general-purpose language 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... 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. .. program in Clojure www.it-ebooks.info 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. .. 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# perspective For Lisp Programmers • Some of Chapter 2, Exploring Clojure, on page 21 will be review, but read it anyway Clojure. .. 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 www.it-ebooks.info report erratum • discuss 2 1.1 • Chapter 1 Getting Started Why Clojure? All of the distinctive features in Clojure are there to provide simplicity, power, or both Here are a few examples: • Functional programming. .. yourself, thanks to the power of Lisp Clojure Is Lisp Reloaded Clojure is a Lisp For decades, Lisp advocates have pointed out the advantages that Lisp has over, well, everything else At the same time, Lisp’s world domination plan seems to be proceeding slowly www.it-ebooks.info report erratum • discuss Why Clojure? •5 Like any other Lisp, Clojure faces two challenges: • Clojure must succeed as a Lisp by . 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. 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. 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

Ngày đăng: 24/04/2014, 16:00

Từ khóa liên quan

Mục lục

  • Cover

  • Table of Contents

  • Foreword for the Second Edition

  • Foreword for the First Edition

  • Acknowledgments

  • Preface

    • Who This Book Is For

    • What Is in This Book

    • How to Read This Book

    • Notation Conventions

    • Web Resources and Feedback

    • Downloading Sample Code

    • 1. Getting Started

      • Why Clojure?

      • Clojure Coding Quick Start

      • Exploring Clojure Libraries

      • Wrapping Up

      • 2. Exploring Clojure

        • Forms

        • Reader Macros

        • Functions

        • Vars, Bindings, and Namespaces

        • Calling Java

Tài liệu cùng người dùng

Tài liệu liên quan