Clojure Cookbook doesn't just teach you Clojure, it also shows you how to use the language and many of its common libraries. The most difficult part of mastering any language is knowing how to apply it, in an idiomatic way, to tasks that real software developers encounter every day. This is especially true of Clojure. With code recipes that teach you how to use the language in a variety of domains, Clojure Cookbook is the first book that goes beyond simply teaching Clojure syntax and semantics. It contains annotated example code with detailed analysis and explanation for hundreds of real programming tasks. You can read the book straight through to gain insights about Clojure, or use it as a reference to solve particular problems.
Preface ix
1 Primitive Data 1 (64)
1.1 Changing the Capitalization of a String 3 (1)
1.2 Cleaning Up Whitespace in a String 4 (1)
1.3 Building a String from Parts 5 (2)
1.4 Treating a String as a Sequence of 7 (1)
Characters
1.5 Converting Between Characters and Integers 8 (2)
1.6 Formatting Strings 10 (2)
1.7 Searching a String by Pattern 12 (1)
1.8 Pulling Values Out of a String Using 13 (2)
Regular Expressions
1.9 Performing Find and Replace on Strings 15 (2)
1.10 Splitting a String into Parts 17 (1)
1.11 Pluralizing Strings Based on a Quantity 18 (2)
1.12 Converting Between Strings, Symbols, and 20 (2)
Keywords
1.13 Maintaining Accuracy with Extremely 22 (2)
Large/Small Numbers
1.14 Working with Rational Numbers 24 (1)
1.15 Parsing Numbers 25 (1)
1.16 Truncating and Rounding Numbers 26 (2)
1.17 Performing Fuzzy Comparison 28 (2)
1.18 Performing Trigonometry 30 (1)
1.19 Inputting and Outputting Integers with 31 (2)
Different Bases
1.20 Calculating Statistics on Collections of 33 (3)
Numbers
1.21 Performing Bitwise Operations 36 (2)
1.22 Generating Random Numbers 38 (1)
1.23 Working with Currency 39 (2)
1.24 Generating Unique IDs 41 (2)
1.25 Obtaining the Current Date and Time 43 (1)
1.26 Representing Dates as Literals 44 (2)
1.27 Parsing Dates and Times Using clj-time 46 (2)
1.28 Formatting Dates Using clj-time 48 (1)
1.29 Comparing Dates 49 (2)
1.30 Calculating the Length of a Time Interval 51 (1)
1.31 Generating Ranges of Dates and Times 52 (2)
1.32 Generating Ranges of Dates and Times 54 (3)
Using Native Java Types
1.33 Retrieving Dates Relative to One Another 57 (2)
1.34 Working with Time Zones 59 (1)
1.35 Converting a Unix Timestamp to a Date 60 (2)
1.36 Converting a Date to a Unix Timestamp 62 (3)
2 Composite Data 65 (58)
2.1 Creating a List 67 (1)
2.2 Creating a List from an Existing Data 68 (2)
Structure
2.3 "Adding" an Item to a List 70 (1)
2.4 "Removing" an Item from a List 71 (1)
2.5 Testing for a List 72 (1)
2.6 Creating a Vector 73 (1)
2.7 "Adding" an Item to a Vector 74 (1)
2.8 "Removing" an Item from a Vector 75 (1)
2.9 Getting the Value at an Index 76 (2)
2.10 Setting the Value at an Index 78 (1)
2.11 Creating a Set 79 (2)
2.12 Adding and Removing Items from Sets 81 (1)
2.13 Testing Set Membership 82 (2)
2.14 Using Set Operations 84 (2)
2.15 Creating a Map 86 (2)
2.16 Retrieving Values from a Map 88 (3)
2.17 Retrieving Multiple KeN from a Map 91 (1)
Simultaneously
2.18 Setting Keys in a Map 92 (4)
2.19 Using Composite Values as Map Keys 96 (2)
2.20 Treating Maps as Sequences (and Vice 98 (2)
Versa)
2.21 Applying Functions to Maps 100(2)
2.22 Keeping Multiple Values for a Key 102(3)
2.23 Combining Maps 105(2)
2.24 Comparing and Sorting Values 107(4)
2.25 Removing Duplicate Elements from a 111(2)
Collection
2.26 Determining if a Collection Holds One of 113(1)
Several Values
2.27 Implementing Custom Data Structures: 114(3)
Red-Black Trees猶art I
2.28 Implementing Custom Data Structures: 117(6)
Red-Black Trees猶art II
3 General Computing 123(44)
3.1 Running a Minimal Clojure REPL 123(2)
3.2 Interactive Documentation 125(2)
3.3 Exploring Namespaces 127(1)
3.4 Trying a Library Without Explicit 128(1)
Dependencies
3.5 Running Clojure Programs 129(3)
3.6 Running Programs from the Command Line 132(2)
3.7 Parsing command-Line Arguments 134(3)
3.8 Creating Custom Project Templates 137(4)
3.9 Building Functions with Polymorphic 141(6)
Behavior
3.10 Extending a Built-In Type 147(1)
3.11 Decoupling Consumers and Producers with 148(4)
core.async
3.12 Making a Parser for Clojure Expressions 152(3)
Using core.match
3.13 Querying Hierarchical Graphs with 155(6)
core.logic
3.14 Playing a Nursery Rhyme 161(6)
4 Local I/O 167(54)
4.1 Writing to STDOUT and STDERR 167(2)
4.2 Reading a Single Keystroke from the 169(1)
Console
4.3 Executing System Commands 170(3)
4.4 Accessing Resource Files 173(2)
4.5 Copying Files 175(2)
4.6 Deleting Files or Directories 177(1)
4.7 Listing Files in a Directory 178(2)
4.8 Memory Mapping a File 180(1)
4.9 Reading and Writing Text Files 181(2)
4.10 Using Temporary Files 183(1)
4.11 Reading and Writing Files at Arbitrary 184(1)
Positions
4.12 Parallelizing File Processing 185(2)
4.13 Parallelizing File Processing with 187(3)
Reducers
4.14 Reading and Writing Clojure Data 190(2)
4.15 Using edn for Configuration Files 192(4)
4.16 Emitting Records as edn Values 196(2)
4.17 Handling Unknown Tagged Literals When 198(3)
Reading Clojure Data
4.18 Reading Properties from a File 201(2)
4.19 Reading and Writing Binary Files 203(2)
4.20 Reading and Writing CSV Data 205(1)
4.21 Reading and Writing Compressed Files 206(2)
4.22 Working with XML Data 208(1)
4.23 Reading and Writing JSON Data 209(2)
4.24 Generating PDF Files 211(4)
4.25 Making a GUI Window with Scrollable Text 215(6)
5 Network I/O and Web Services 221(34)
5.1 Making HTTP Requests 221(2)
5.2 Performing Asynchronous HTTP Requests 223(2)
5.3 Sending a Ping Request 225(1)
5.4 Retrieving and Parsing RSS Data 226(2)
5.5 Sending Email 228(3)
5.6 Communicating over Queues Using RabbitMQ 231(7)
5.7 Communicating with Embedded Devices via 238(4)
MQTT
5.8 Using ZeroMQ Concurrently 242(3)
5.9 Creating a TCP Client 245(2)
5.10 Creating a TCP Server 247(3)
5.11 Sending and Receiving UDP Packets 250(5)
6 Databases 255(52)
6.1 Connecting to an SQL Database 256(3)
6.2 Connecting to an SQL Database with a 259(3)
Connection Pool
6.3 Manipulating an SQL Database 262(6)
6.4 Simplifying SQL with Korma 268(4)
6.5 Performing Full-Text Search with Lucene 272(2)
6.6 Indexing Data with ElasticSearch 274(5)
6.7 Working with Cassandra 279(3)
6.8 Working with MongoDB 282(4)
6.9 Working with Redis 286(3)
6.10 Connecting to a Datomic Database 289(2)
6.11 Defining a Schema for a Datomic Database 291(4)
6.12 Writing Data to Datomic 295(3)
6.13 Removing Data from Datomic Database 298(2)
6.14 Trying Datomic Transactions Without 300(2)
Committing Them
6.15 Traversing Datomic Indexes 302(5)
7 Web Applications 307(38)
7.1 Introduction to Ring 307(2)
7.2 Using Ring Middleware 309(2)
7.3 Serving Static Files with Ring 311(2)
7.4 Handling Form Data with Ring 313(1)
7.5 Handling Cookies with Ring 314(2)
7.6 Storing Sessions with Ring 316(2)
7.7 Reading and Writing Request and Response 318(2)
Headers in Ring
7.8 Routing Requests with Compojure 320(2)
7.9 Performing HTTP Redirects with Ring 322(1)
7.10 Building a RESTful Application with 323(2)
Liberator
7.11 Templating HTML with Enlive 325(7)
7.12 Templating with Selmer 332(4)
7.13 Templating with Hiccup 336(3)
7.14 Rendering Markdown Documents 339(3)
7.15 Building Applications with Luminus 342(3)
8 Performance and Production 345(32)
8.1 AOT Compilation 345(2)
8.2 Packaging a Project into a JAR File 347(3)
8.3 Creating a WAR File 350(4)
8.4 Running an Application as a Daemon 354(6)
8.5 Alleviating Performance Problems with 360(3)
Type Hinting
8.6 Fast Math with Primitive Java Arrays 363(3)
8.7 Simple Profiling with Timbre 366(1)
8.8 Logging with Timbre 367(2)
8.9 Releasing a Library to Clojars 369(2)
8.10 Using Macros to Simplify API Deprecations 371(6)
9 Distributed Computation 377(28)
9.1 Building an Activity Feed System with 378(9)
Storm
9.2 Processing Data with an Extract Transform 387(4)
Load (ETL) Pipeline
9.3 Aggregating Large Files 391(5)
9.4 Testing Cascalog Workflows 396(2)
9.5 Checkpointing Cascalog Jobs 398(2)
9.6 Explaining a Cascalog Query 400(3)
9.7 Running a Cascalog Job on Elastic 403(2)
MapReduce
10 Testing 405(34)
10.1 Unit Testing 406(4)
10.2 Testing with Midje 410(3)
10.3 Thoroughly Testing by Randomizing Inputs 413(4)
10.4 Finding Values That Cause Failure 417(3)
10.5 Running Browser-Based Tests 420(6)
10.6 Tracing Code Execution 426(3)
10.7 Avoiding Null-Pointer Exceptions with 429(2)
core.typed
10.8 Verifying Java Interop Using core.typed 431(4)
10.9 Type Checking Higher-Order Functions 435(4)
with core.typed
Index 439