Clojure in Action, 2nd Edition.pdf
(4.77 MB, 需要: 5 个论坛币)
About the bookClojure in Action, Second Edition is an expanded and improved version that’s been updated to cover the new features of Clojure 1.6. The book gives you a rapid introduction to the Clojure language, moving from abstract theory to practical examples. You’ll start by learning how to use Clojure as a general-purpose language. Next, you’ll explore Clojure’s efficient concurrency model, based on the database concept of Software Transactional Memory (STM). You’ll gain a new level of productivity through Clojure DSLs that can run on the JVM. Along the way, you’ll learn countless tips, tricks, and techniques for writing smaller, safer, and faster code.
What's inside
- Functional programming basics
- Metaprogramming with Clojure’s macros
- Interoperating with Java
- Covers Clojure 1.6
TABLE OF CONTENTS[url=]show full[/url]
PREFACE TO THE SECOND EDITION
PREFACE TO THE FIRST EDITION
ACKNOWLEDGMENTS
ABOUT THIS BOOK
AUTHOR ONLINE
ABOUT THE COVER ILLUSTRATION
1. INTRODUCING CLOJURE
1.1. Clojure: What and Why?1.1.1. Clojure: A modern Lisp1.1.2. Clojure: Pragmatic functional programming
1.1.3. Clojure on the JVM
1.2. Language basics1.2.1. Lisp syntax
1.2.2. Parentheses
1.3. Host interoperation: A JVM crash course1.3.1. Java types, classes, and objects
1.3.2. The dot and new operators
1.3.3. Threads and concurrency
1.4. Summary
2. CLOJURE ELEMENTS: DATA STRUCTURES AND FUNCTIONS2.1. Coding at the REPL2.1.1. The Clojure REPL
2.1.2. "Hello, world!"
2.1.3. Looking up documentation using doc, find-doc, and apropos
2.1.4. A few more points on Clojure syntax
2.2. Clojure data structures2.2.1. nil, truth, and falsehood
2.2.2. Characters and strings
2.2.3. Clojure numbers
2.2.4. Symbols and keywords
2.2.5. Lists
2.2.6. Vectors
2.2.7. Maps
2.2.8. Sequences
2.3. Program structure2.3.1. Functions
2.3.2. The let form
2.3.3. Side effects with do
2.3.4. Reader macros
2.4. Program flow2.4.1. Conditionals
2.4.2. Logical functions
2.4.3. Functional iteration
2.4.4. Threading macros
2.5. Summary
3. BUILDING BLOCKS OF CLOJURE3.1. Metadata3.1.1. Java Type Hints
3.2. Java exceptions: try and throw
3.3. Functions3.3.1. Defining functions
3.3.2. Calling functions
3.3.3. Higher-order functions
3.3.4. Writing higher-order functions
3.3.5. Anonymous functions
3.3.6. Keywords and symbols
3.4. Scope3.4.1. Vars and binding
3.4.2. The let form revisited
3.4.3. Lexical closures
3.5. Namespaces3.5.1. The ns macro
3.5.2. Working with namespaces
3.6. Destructuring3.6.1. Vector bindings
3.6.2. Map bindings
3.7. Reader literals
3.8. Summary
4. MULTIMETHOD POLYMORPHISM4.1. Polymorphism and its types4.1.1. Parametric Polymorphism
4.1.2. Ad hoc Polymorphism
4.1.3. Subtype Polymorphism
4.2. Polymorphism using multimethods4.2.1. Life without multimethods
4.2.2. Ad hoc polymorphism using multimethods
4.2.3. Multiple dispatch
4.2.4. Subtype polymorphism using multimethods
4.3. Summary
5. EXPLORING CLOJURE AND JAVA INTEROP5.1. Calling Java from Clojure5.1.1. Importing Java classes into Clojure
5.1.2. Creating instances
5.1.3. Accessing methods and fields
5.1.4. Macros and the dot special form
5.1.5. Helpful Clojure macros for working with Java
5.1.6. Implementing interfaces and extending classes
5.2. Compiling Clojure code to Java byte code5.2.1. Example: A tale of two calculators
5.2.2. Creating Java classes and interfaces using gen-class and gen-interface
5.3. Calling Clojure from Java
5.4. Summary
6. STATE AND THE CONCURRENT WORLD6.1. The problem with state6.1.1. Common problems with shared state
6.1.2. Traditional solution
6.2. Separating identities and values6.2.1. Immutable values
6.2.2. Objects and time
6.2.3. Immutability and concurrency
6.3. Clojure’s way6.3.1. Immutability and performance
6.3.2. Managed references
6.4. Refs6.4.1. Creating refs
6.4.2. Mutating refs
6.4.3. Software transactional memory
6.5. Agents6.5.1. Creating agents
6.5.2. Mutating agents
6.5.3. Working with agents
6.5.4. Side effects in STM transactions
6.6. Atoms6.6.1. Creating atoms
6.6.2. Mutating atoms
6.7. Vars6.7.1. Creating vars and root bindings
6.7.2. Var bindings
6.8. State and its unified access model6.8.1. Creating
6.8.2. Reading
6.8.3. Mutation
6.8.4. Transactions
6.8.5. Watching for mutation
6.9. Deciding which reference type to use
6.10. Futures and promises6.10.1. Futures
6.10.2. Promises
6.11. Summary
7. EVOLVING CLOJURE THROUGH MACROS7.1. Macro basics7.1.1. Textual substitution
7.1.2. The unless example
7.1.3. Macro templates
7.1.4. Recap: Why macros?
7.2. Macros from within Clojure7.2.1. comment
7.2.2. declare
7.2.3. defonce
7.2.4. and
7.2.5. time
7.3. Writing your own macros7.3.1. infix
7.3.2. randomly
7.3.3. defwebmethod
7.3.4. defnn
7.3.5. assert-true
7.4. Summary
8. MORE ON FUNCTIONAL PROGRAMMING
8.1. Using higher-order functions8.1.1. Collecting results of functions8.1.2. Reducing lists of things
8.1.3. Filtering lists of things
8.2. Partial application8.2.1. Adapting functions
8.2.2. Defining functions
8.3. Closures8.3.1. Free variables and closures
8.3.2. Delayed computation and closures
8.3.3. Closures and objects
8.3.4. An object system for Clojure
8.4. Summary
9. PROTOCOLS, RECORDS, AND TYPES9.1. The expression problem9.1.1. Setting up the example scenario
9.1.2. A closer look at the expression problem, and some potential solutions
9.1.3. Clojure’s multimethods solution
9.2. Examining the operations side of the expression problem9.2.1. def-modus-operandi
9.2.2. detail-modus-operandi
9.2.3. Tracking our modus operandi
9.2.4. Error handling and trouble spots in this solution
9.3. Examining the data types side of the expression problem with protocols9.3.1. defprotocol and extend-protocol
9.3.2. Defining data types with deftype, defrecord, and reify
9.4. Summary
10. TEST-DRIVEN DEVELOPMENT AND MORE10.1. Getting started with TDD:Manipulating dates in strings10.1.1. First assertion
10.1.2. month-from and year-from
10.1.3. as-string
10.1.4. Incrementing and decrementing
10.1.5. Refactor mercilessly
10.1.6. A simple example of TDD using dates and string
10.2. Improving tests through mocking and stubbing10.2.1. Example: expense finders
10.2.2. Stubbing
10.2.3. Mocking
10.2.4. Mocks versus stubs
10.2.5. Managing stubbing and mocking state
10.3. Organizing tests10.3.1. The testing macro
10.3.2. The are macro
10.4. Summary
11. MORE MACROS AND DSLS11.1. A quick review of macros11.1.1. Anaphoric macros
11.1.2. The anaphoric if
11.1.3. The thread-it macro
11.2. Shifting computation to compile time11.2.1. Example: Rotation ciphers without macros
11.2.2. Making the compiler work harder
11.3. Macro-generating macros11.3.1. Example template
11.3.2. Implementing make-synonym
11.3.3. Why macro-generating macros
11.4. Domain-specific languages11.4.1. DSL-driven design
11.4.2. User classification
11.5. Summary
CONCLUSION
APPENDIX A: INSTALLING CLOJUREA.1. Try Clojure
A.2. Clojure.jar
A.3. LeiningenA.3.1. lein tasks
A.3.2. lein repl
A.3.3. Adding dependencies to a Leiningen project
About the technology
Clojure is a modern Lisp for the JVM. It has the strengths you expect: first-class functions, macros, and Lisp’s clean programming style. It supports functional programming, making it ideal for concurrent programming and for creating domain-specific languages. Clojure lets you solve harder problems, make faster changes, and end up with a smaller code base. It’s no wonder that there are so many Clojure success stories.
About the reader
Assumes readers are familiar with a programming language like C, Java, Ruby, or Python.
About the author
Amit Rathore has 12 years of experience building large-scale, data-heavy applications for a variety of domains. Francis Avila is a software developer at Breeze with seven years of experience in back- and front-end web development.


雷达卡



京公网安备 11010802022788号







