Practical Clojure (eBook)
232 Seiten
Apress (Verlag)
978-1-4302-7230-4 (ISBN)
This book is the first definitive reference for the Clojure language, providing both an introduction to functional programming in general and a more specific introduction to Clojure's features. This book demonstrates the use of the language through examples, including features such as software transactional memory (STM) and immutability, which may be new to programmers coming from other languages.
- Overview of functional programming and description of what sets Clojure apart from other languages
- Detailed explanation of Clojure's special features
- Examples of real-world tasks that are well-suited to Clojure's capabilities, starting with simple tasks and moving on to more complex applications
Luke VanderHart is a professional software developer living and working in Washington, D.C. as a consultant with NuWave Solutions. He has more than five years of experience working with the Java platform, and has worked on programs ranging from distributed client-server networks serving and synchronizing semantic XML data, to GUI development using Java Swing, to enterprise web portals serving tens of thousands of pages per day. He is a very active member of the Clojure community.
This book is the first definitive reference for the Clojure language, providing both an introduction to functional programming in general and a more specific introduction to Clojure's features. This book demonstrates the use of the language through examples, including features such as software transactional memory (STM) and immutability, which may be new to programmers coming from other languages. Overview of functional programming and description of what sets Clojure apart from other languages Detailed explanation of Clojure's special features Examples of real-world tasks that are well-suited to Clojure's capabilities, starting with simple tasks and moving on to more complex applications
Luke VanderHart is a professional software developer living and working in Washington, D.C. as a consultant with NuWave Solutions. He has more than five years of experience working with the Java platform, and has worked on programs ranging from distributed client-server networks serving and synchronizing semantic XML data, to GUI development using Java Swing, to enterprise web portals serving tens of thousands of pages per day. He is a very active member of the Clojure community.
Title Page 1
Copyright Page 2
Contents at a Glance 4
Table of Contents 5
About the Authors 14
About the Technical Reviewer 15
Acknowledgments 16
Chapter 1 The Clojure Way 17
Clojure’s Philosophy and Special Features 17
A Next-Generation Language 17
Dynamic and Powerful (Yes, It’s a Lisp) 17
The Java Platform 18
Functional Programming 18
Purely Functional Programming 20
Clojure’s Compromise 22
Immutability 23
What about Object-Oriented Programming? 25
Structure of a Clojure Program 26
State Management 27
State and Identity 28
Software Transactional Memory 29
Summary 31
Chapter 2 The Clojure Environment 32
"Hello World" in Clojure 32
Clojure Forms 33
Literals 33
Symbols 34
Composite Forms 34
Special Forms 34
Writing and Running Source Files 35
Vars, Namespaces, and the Environment 36
Symbols and Symbol Resolution 38
Symbol Names 38
Symbol Resolution and Scope 39
Namespaces 39
Declaring Namespaces 40
Referencing Namespaces 40
Structuring Source Files 41
Summary 41
Chapter 3 Controlling Program Flow 43
Functions 43
First-Class Functions 43
Defining Functions with fn 43
Defining Functions with defn 45
Functions of Multiple Arities 45
Functions with Variable Arguments 46
Shorthand Function Declaration 47
Conditional Expressions 48
Local Bindings 49
Looping and Recursion 50
Tail Recursion 53
Clojure’s recur 53
Using loop 55
Deliberate Side Effects 56
Using do 56
Side Effects in Function Definitions 57
Functional Programming Techniques 57
First-Class Functions 57
Consuming First-Class Functions 58
Producing First-Class Functions 59
Closures 60
Currying and Composing Functions 60
Using partial to Curry Functions 60
Using comp to Compose Functions 61
Putting It All Together 62
Chapter 4 Data in Clojure 64
How to Represent and Manipulate Data 64
Nil 65
Primitive Types 65
Numbers 65
Common Numeric Functions 66
Strings 70
Common String Functions 70
Regular Expression Functions 71
Boolean 73
Common Boolean Functions 73
Characters 73
char 74
Keywords 74
Collections 74
Lists 75
list 76
peek 76
pop 76
list? 76
Vectors 77
vector 77
vec 77
get 77
peek 78
vector? 78
conj 78
assoc 78
pop 78
subvec 79
Maps 79
Struct Maps 80
Maps As Objects 81
Sets 83
Common Set Functions 84
Summary 84
Chapter 5 Sequences 85
What Are Sequences? 85
Sequenceable Types 87
Anatomy of a Sequence 87
Constructing Sequences 88
Lazy Sequences 89
An Example of Laziness 90
Constructing Lazy Sequences 92
Constructing Lazy Sequences Directly 92
Constructing Lazy Sequences Using Sequence Generator Functions 93
Lazy Sequences and Memory Management 94
The Sequence API 95
Sequence Creation 95
seq 95
vals 95
keys 95
rseq 95
lazy-seq 96
repeatedly 96
iterate 96
repeat 96
range 97
distinct 97
filter 97
remove 97
cons 98
concat 98
lazy-cat 98
mapcat 98
cycle 99
interleave 99
interpose 99
rest 99
next 99
drop 100
drop-while 100
take 100
take-nth 100
take-while 101
drop-last 101
reverse 101
sort 101
sort-by 101
split-at 102
split-with 102
partition 102
map 103
Summary 106
Chapter 6 State Management 107
State in an Immutable World 107
The Old Way 107
State and Identity 108
State and Identity in Clojure 108
Coordinated vs. Independent State 109
Synchronous vs. Asynchronous Updates 109
Refs and Transactions 109
Creating and Accessing refs 110
Updating refs 110
Transactions 111
Tools for Updating refs 111
Examples 113
Atoms 116
Using Atoms 116
When to Use Atoms 117
Asynchronous Agents 117
Creating and Updating Agents 117
Update Semantics 118
Errors and Agents 119
Dealing with Agents in a Failed State 119
Waiting for Agents 120
Shutting Down Agents 120
When to Use Agents 121
Vars and Thread-Local State 121
When to Use Thread-Local Vars 122
Keeping Track of Identities 123
Validators 123
Watches 124
Summary 125
Chapter 7 Namespaces and Libraries 126
Organizing Clojure Code 126
Namespace Basics 126
Switching Namespaces with in-ns 126
Referring to Other Namespaces 127
Loading Other Namespaces 128
Loading from a File or Stream 128
Loading from the Classpath 129
Namespace Names vs. File Names 129
Loading Resources from the Classpath 129
Loading and Referring Namespaces in One Step 131
Importing Java Classes 131
Bringing It All Together: Namespace Declarations 132
Symbols and Namespaces 132
Namespace Metadata 133
Forward Declarations 133
Namespace-Qualified Symbols and Keywords 133
Constructing Symbols and Keywords 134
Public and Private Vars 134
Advanced Namespace Operations 135
Querying Namespaces 135
Manipulating Namespaces 136
Namespaces As References 137
Summary 137
Chapter 8 Metadata 138
Reading and Writing Metadata 138
Metadata-Preserving Operations 139
Read-Time Metadata 140
Metadata on Vars 140
Type Tags 142
Private Vars 142
Metadata on Reference Types 142
Summary 142
Chapter 9 Multimethods and Hierarchies 143
Multimethods 143
Multiple Dispatch 145
Default Dispatch Values 145
Hierarchies 146
Querying Hierarchies 147
Hierarchies with Multimethods 147
Hierarchies with Java Classes 148
More Hierarchy Queries 149
Resolving Conflicts 149
Type Tags 151
User-Defined Hierarchies 151
Summary 152
Chapter 10 Java Interoperability 153
Calling Java from Clojure 153
Java Interop Special Forms 153
Java Interop Preferred Forms 154
Clojure Types and Java Interfaces 155
Java Arrays 156
Creating Arrays 157
Manipulating Arrays 158
Iterating Over Arrays 158
Calling Clojure from Java 158
Loading and Evaluating Clojure Code 159
Using Clojure Functions and Vars 159
Creating Java Classes 160
Proxying Java Classes 160
Generating Java Classes 161
Ahead-of-Time Compilation 162
Basic gen-class Options 164
Defining Methods for the Generated Class 164
Adding State to the Generated Class 164
Adding Methods to the Generated Class 165
Adding Constructors and Factories 165
Exposing Superclass Members 166
Generating Command-Line Programs 166
Loading the Implementation 166
Namespace Declarations with gen-class 166
Simple Command-Line Program 167
Summary 167
Chapter 11 Parallel Programming 168
Parallelism in Clojure 168
Agents 168
Agent Thread Pools 168
Agent Example 169
Concurrent Agent Performance 170
Concurrency Functions 170
Overhead and Performance 171
Futures and Promises 172
Futures 172
Controlling Futures 173
Promises 173
Java-based Threading 174
Creating a Thread 174
Summary 175
Chapter 12 Macros and Metaprogramming 176
What Is Metaprogramming? 176
Code vs. Data 176
Homoiconicity 176
Macros 177
Working with Macros 178
Debugging Macros 179
Code Templating 180
Splicing Unquotes 180
Generating Symbols 181
When to Use Macros 182
Using Macros 182
Implementing a Control Structure 182
Implementing a Macro with Variadic Arguments 183
Implementing a Macro Using Recursion 185
Using Macros to Create DSLs 185
Summary 187
Chapter 13 Datatypes and Protocols 188
Protocols 188
Protocols As Interfaces 189
Datatypes 189
Implementing Protocols and Interfaces 190
In-Line Methods 190
Extending Java Interfaces 191
Datatypes As Classes 192
Extending Protocols to Pre-Existing Types 192
Extending Java Classes and Interfaces 193
Reifying Anonymous Datatypes 193
Working with Datatypes and Protocols 194
A Complete Example 195
Advanced Datatypes 195
Summary 196
Chapter 14 Performance 197
Profiling on the JVM 197
General Tips for Java Performance 197
Simple Profiling with Time 198
Using Java Profiling Tools 198
Memoization 199
Reflection and Type Hints 199
Working with Primitives 201
Loop Primitives 201
Unchecked Integer Arithmetic 202
Primitive Arrays 203
Transients 203
Var Lookups 204
Inlining 205
Macros and definline 205
Summary 205
Index 207
Erscheint lt. Verlag | 31.12.2010 |
---|---|
Zusatzinfo | 232 p. |
Verlagsort | Berkeley |
Sprache | englisch |
Themenwelt | Mathematik / Informatik ► Informatik ► Programmiersprachen / -werkzeuge |
Mathematik / Informatik ► Informatik ► Software Entwicklung | |
Mathematik / Informatik ► Informatik ► Theorie / Studium | |
Schlagworte | Clojure • Control • Controlling • Functional Programming • Java • Management • Parallel Programming • Performance • programming • Programming language |
ISBN-10 | 1-4302-7230-9 / 1430272309 |
ISBN-13 | 978-1-4302-7230-4 / 9781430272304 |
Haben Sie eine Frage zum Produkt? |
Größe: 1,0 MB
DRM: Digitales Wasserzeichen
Dieses eBook enthält ein digitales Wasserzeichen und ist damit für Sie personalisiert. Bei einer missbräuchlichen Weitergabe des eBooks an Dritte ist eine Rückverfolgung an die Quelle möglich.
Dateiformat: PDF (Portable Document Format)
Mit einem festen Seitenlayout eignet sich die PDF besonders für Fachbücher mit Spalten, Tabellen und Abbildungen. Eine PDF kann auf fast allen Geräten angezeigt werden, ist aber für kleine Displays (Smartphone, eReader) nur eingeschränkt geeignet.
Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür einen PDF-Viewer - z.B. den Adobe Reader oder Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür einen PDF-Viewer - z.B. die kostenlose Adobe Digital Editions-App.
Zusätzliches Feature: Online Lesen
Dieses eBook können Sie zusätzlich zum Download auch online im Webbrowser lesen.
Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.
aus dem Bereich