With more than 150 detailed recipes, this cookbook shows experienced Clojure developers how to solve a variety of programming tasks with this JVM language. The solutions cover everything from building dynamic websites and working with databases to network communication, cloud computing, and advanced testing strategies. And more than 60 of the world’s best Clojurians contributed recipes.
Each recipe includes code that you can use right away, along with a discussion on how and why the solution works, so you can adapt these patterns, approaches, and techniques to situations not specifically covered in this cookbook.
- Master built-in primitive and composite data structures
- Create, develop and publish libraries, using the Leiningen tool
- Interact with the local computer that’s running your application
- Manage network communication protocols and libraries
- Use techniques for connecting to and using a variety of databases
- Build and maintain dynamic websites, using the Ring HTTP server library
- Tackle application tasks such as packaging, distributing, profiling, and logging
- Take on cloud computing and heavyweight distributed data crunching
- Dive into unit, integration, simulation, and property-based testing
Clojure Cookbook is a collaborative project with contributions from some of the world’s best Clojurians, whose backgrounds range from aerospace to social media, banking to robotics, AI research to e-commerce.
Author(s): Luke VanderHart, Ryan Neufeld
Publisher: O'Reilly Media
Year: 2014
Copyright
Table of Contents
Preface
How This Book Was Written
Audience
Other Resources
Structure
Chapter Listing
Software Prerequisites
Conventions Used in This Book
REPL Sessions
Console/Terminal Sessions
Typesetting Conventions
Using Code Examples
Safari® Books Online
How to Contact Us
Acknowledgments
Ryan Neufeld
Luke VanderHart
Chapter 1. Primitive Data
1.0. Introduction
Strings
Numeric Types
Dates
1.1. Changing the Capitalization of a String
Problem
Solution
Discussion
See Also
1.2. Cleaning Up Whitespace in a String
Problem
Solution
Discussion
See Also
1.3. Building a String from Parts
Problem
Solution
Discussion
See Also
1.4. Treating a String as a Sequence of Characters
Problem
Solution
Discussion
See Also
1.5. Converting Between Characters and Integers
Problem
Solution
Discussion
See Also
1.6. Formatting Strings
Problem
Solution
Discussion
See Also
1.7. Searching a String by Pattern
Problem
Solution
Discussion
See Also
1.8. Pulling Values Out of a String Using Regular Expressions
Problem
Solution
Discussion
See Also
1.9. Performing Find and Replace on Strings
Problem
Solution
Discussion
See Also
1.10. Splitting a String into Parts
Problem
Solution
Discussion
See Also
1.11. Pluralizing Strings Based on a Quantity
Problem
Solution
Discussion
See Also
1.12. Converting Between Strings, Symbols, and Keywords
Problem
Solution
Discussion
See Also
1.13. Maintaining Accuracy with Extremely Large/Small Numbers
Problem
Solution
Discussion
See Also
1.14. Working with Rational Numbers
Problem
Solution
Discussion
See Also
1.15. Parsing Numbers
Problem
Solution
Discussion
See Also
1.16. Truncating and Rounding Numbers
Problem
Solution
Discussion
See Also
1.17. Performing Fuzzy Comparison
Problem
Solution
Discussion
See Also
1.18. Performing Trigonometry
Problem
Solution
Discussion
See Also
1.19. Inputting and Outputting Integers with Different Bases
Problem
Solution
Discussion
See Also
1.20. Calculating Statistics on Collections of Numbers
Problem
Solution
Discussion
See Also
1.21. Performing Bitwise Operations
Problem
Solution
Discussion
See Also
1.22. Generating Random Numbers
Problem
Solution
Discussion
See Also
1.23. Working with Currency
Problem
Solution
Discussion
See Also
1.24. Generating Unique IDs
Problem
Solution
Discussion
See Also
1.25. Obtaining the Current Date and Time
Problem
Solution
Discussion
See Also
1.26. Representing Dates as Literals
Problem
Solution
Discussion
See Also
1.27. Parsing Dates and Times Using clj-time
Problem
Solution
Discussion
See Also
1.28. Formatting Dates Using clj-time
Problem
Solution
Discussion
See Also
1.29. Comparing Dates
Problem
Solution
Discussion
See Also
1.30. Calculating the Length of a Time Interval
Problem
Solution
Discussion
See Also
1.31. Generating Ranges of Dates and Times
Problem
Solution
Discussion
See Also
1.32. Generating Ranges of Dates and Times Using Native Java Types
Problem
Solution
Discussion
See Also
1.33. Retrieving Dates Relative to One Another
Problem
Solution
Discussion
See Also
1.34. Working with Time Zones
Problem
Solution
Discussion
See Also
1.35. Converting a Unix Timestamp to a Date
Problem
Solution
Discussion
See Also
1.36. Converting a Date to a Unix Timestamp
Problem
Solution
Discussion
See Also
Chapter 2. Composite Data
2.0. Introduction
Immutability
Persistence
The Sequence Abstraction
2.1. Creating a List
Problem
Solution
Discussion
See Also
2.2. Creating a List from an Existing Data Structure
Problem
Solution
Discussion
See Also
2.3. “Adding” an Item to a List
Problem
Solution
Discussion
See Also
2.4. “Removing” an Item from a List
Problem
Solution
Discussion
See Also
2.5. Testing for a List
Problem
Solution
Discussion
See Also
2.6. Creating a Vector
Problem
Solution
Discussion
See Also
2.7. “Adding” an Item to a Vector
Problem
Solution
Discussion
See Also
2.8. “Removing” an Item from a Vector
Problem
Solution
Discussion
See Also
2.9. Getting the Value at an Index
Problem
Solution
Discussion
See Also
2.10. Setting the Value at an Index
Problem
Solution
Discussion
See Also
2.11. Creating a Set
Problem
Solution
Discussion
See Also
2.12. Adding and Removing Items from Sets
Problem
Solution
Discussion
See Also
2.13. Testing Set Membership
Problem
Solution
Discussion
See Also
2.14. Using Set Operations
Problem
Solution
Discussion
See Also
2.15. Creating a Map
Problem
Solution
Discussion
See Also
2.16. Retrieving Values from a Map
Problem
Solution
Discussion
See Also
2.17. Retrieving Multiple Keys from a Map Simultaneously
Problem
Solution
Discussion
See Also
2.18. Setting Keys in a Map
Problem
Solution
Discussion
See Also
2.19. Using Composite Values as Map Keys
Problem
Solution
Discussion
See Also
2.20. Treating Maps as Sequences (and Vice Versa)
Problem
Solution
Discussion
See Also
2.21. Applying Functions to Maps
Problem
Solution
Discussion
See Also
2.22. Keeping Multiple Values for a Key
Problem
Solution
Discussion
See Also
2.23. Combining Maps
Problem
Solution
Discussion
See Also
2.24. Comparing and Sorting Values
Problem
Solution
Discussion
See Also
2.25. Removing Duplicate Elements from a Collection
Problem
Solution
Discussion
See Also
2.26. Determining if a Collection Holds One of Several Values
Problem
Solution
Discussion
See Also
2.27. Implementing Custom Data Structures: Red-Black Trees—Part I
Problem
Solution
Discussion
See Also
2.28. Implementing Custom Data Structures: Red-Black Trees—Part II
Problem
Solution
Discussion
See Also
Chapter 3. General Computing
3.0. Introduction
3.1. Running a Minimal Clojure REPL
Problem
Solution
Discussion
See Also
3.2. Interactive Documentation
Problem
Solution
Discussion
See Also
3.3. Exploring Namespaces
Problem
Solution
Discussion
See Also
3.4. Trying a Library Without Explicit Dependencies
Problem
Solution
Discussion
See Also
3.5. Running Clojure Programs
Problem
Solution
Discussion
See Also
3.6. Running Programs from the Command Line
Problem
Solution
Discussion
See Also
3.7. Parsing Command-Line Arguments
Problem
Solution
Discussion
See Also
3.8. Creating Custom Project Templates
Problem
Solution
Discussion
See Also
3.9. Building Functions with Polymorphic Behavior
Problem
Solution
Discussion
See Also
3.10. Extending a Built-In Type
Problem
Solution
Discussion
See Also
3.11. Decoupling Consumers and Producers with core.async
Problem
Solution
Discussion
See Also
3.12. Making a Parser for Clojure Expressions Using core.match
Problem
Solution
Discussion
See Also
3.13. Querying Hierarchical Graphs with core.logic
Problem
Solution
Discussion
See Also
3.14. Playing a Nursery Rhyme
Problem
Solution
Discussion
See Also
Chapter 4. Local I/O
4.0. Introduction
4.1. Writing to STDOUT and STDERR
Problem
Solution
Discussion
See Also
4.2. Reading a Single Keystroke from the Console
Problem
Solution
Discussion
See Also
4.3. Executing System Commands
Problem
Solution
Discussion
See Also
4.4. Accessing Resource Files
Problem
Solution
Discussion
See Also
4.5. Copying Files
Problem
Solution
Discussion
See Also
4.6. Deleting Files or Directories
Problem
Solution
Discussion
See Also
4.7. Listing Files in a Directory
Problem
Solution
Discussion
See Also
4.8. Memory Mapping a File
Problem
Solution
Discussion
See Also
4.9. Reading and Writing Text Files
Problem
Solution
Discussion
See Also
4.10. Using Temporary Files
Problem
Solution
Discussion
See Also
4.11. Reading and Writing Files at Arbitrary Positions
Problem
Solution
Discussion
See Also
4.12. Parallelizing File Processing
Problem
Solution
Discussion
See Also
4.13. Parallelizing File Processing with Reducers
Problem
Solution
Discussion
See Also
4.14. Reading and Writing Clojure Data
Problem
Solution
Discussion
See Also
4.15. Using edn for Configuration Files
Problem
Solution
Discussion
See Also
4.16. Emitting Records as edn Values
Problem
Solution
Discussion
See Also
4.17. Handling Unknown Tagged Literals When Reading Clojure Data
Problem
Solution
Discussion
See Also
4.18. Reading Properties from a File
Problem
Solution
Discussion
See Also
4.19. Reading and Writing Binary Files
Problem
Solution
Discussion
See Also
4.20. Reading and Writing CSV Data
Problem
Solution
Discussion
See Also
4.21. Reading and Writing Compressed Files
Problem
Solution
Discussion
See Also
4.22. Working with XML Data
Problem
Solution
Discussion
See Also
4.23. Reading and Writing JSON Data
Problem
Solution
Discussion
See Also
4.24. Generating PDF Files
Problem
Solution
Discussion
See Also
4.25. Making a GUI Window with Scrollable Text
Problem
Solution
Discussion
See Also
Chapter 5. Network I/O and Web Services
5.0. Introduction
5.1. Making HTTP Requests
Problem
Solution
Discussion
See Also
5.2. Performing Asynchronous HTTP Requests
Problem
Solution
Discussion
See Also
5.3. Sending a Ping Request
Problem
Solution
Discussion
See Also
5.4. Retrieving and Parsing RSS Data
Problem
Solution
Discussion
See Also
5.5. Sending Email
Problem
Solution
Discussion
See Also
5.6. Communicating over Queues Using RabbitMQ
Problem
Solution
Discussion
See Also
5.7. Communicating with Embedded Devices via MQTT
Problem
Solution
Discussion
See Also
5.8. Using ZeroMQ Concurrently
Problem
Solution
Discussion
See Also
5.9. Creating a TCP Client
Problem
Solution
Discussion
See Also
5.10. Creating a TCP Server
Problem
Solution
Discussion
See Also
5.11. Sending and Receiving UDP Packets
Problem
Solution
See Also
Chapter 6. Databases
6.0. Introduction
6.1. Connecting to an SQL Database
Problem
Solution
Discussion
See Also
6.2. Connecting to an SQL Database with a Connection Pool
Problem
Solution
Discussion
See Also
6.3. Manipulating an SQL Database
Problem
Solution
Discussion
See Also
6.4. Simplifying SQL with Korma
Problem
Solution
Discussion
See Also
6.5. Performing Full-Text Search with Lucene
Problem
Solution
Discussion
See Also
6.6. Indexing Data with ElasticSearch
Problem
Solution
Discussion
See Also
6.7. Working with Cassandra
Problem
Solution
Discussion
See Also
6.8. Working with MongoDB
Problem
Solution
Discussion
See Also
6.9. Working with Redis
Problem
Solution
Discussion
See Also
6.10. Connecting to a Datomic Database
Problem
Solution
Discussion
See Also
6.11. Defining a Schema for a Datomic Database
Problem
Solution
Discussion
See Also
6.12. Writing Data to Datomic
Problem
Solution
Discussion
See Also
6.13. Removing Data from a Datomic Database
Problem
Solution
Discussion
See Also
6.14. Trying Datomic Transactions Without Committing Them
Problem
Solution
Discussion
See Also
6.15. Traversing Datomic Indexes
Problem
Solution
Discussion
See Also
Chapter 7. Web Applications
7.0. Introduction
7.1. Introduction to Ring
Problem
Solution
Discussion
See Also
7.2. Using Ring Middleware
Problem
Solution
Discussion
See Also
7.3. Serving Static Files with Ring
Problem
Solution
Discussion
See Also
7.4. Handling Form Data with Ring
Problem
Solution
Discussion
See Also
7.5. Handling Cookies with Ring
Problem
Solution
Discussion
See Also
7.6. Storing Sessions with Ring
Problem
Solution
Discussion
See Also
7.7. Reading and Writing Request and Response Headers in Ring
Problem
Solution
Discussion
See Also
7.8. Routing Requests with Compojure
Problem
Solution
Discussion
See Also
7.9. Performing HTTP Redirects with Ring
Problem
Solution
Discussion
See Also
7.10. Building a RESTful Application with Liberator
Problem
Solution
Discussion
See Also
7.11. Templating HTML with Enlive
Problem
Solution
Discussion
See Also
7.12. Templating with Selmer
Problem
Solution
Discussion
See Also
7.13. Templating with Hiccup
Problem
Solution
Discussion
See Also
7.14. Rendering Markdown Documents
Problem
Solution
Discussion
See Also
7.15. Building Applications with Luminus
Problem
Solution
Discussion
See Also
Chapter 8. Performance and Production
8.0. Introduction
8.1. AOT Compilation
Problem
Solution
Discussion
See Also
8.2. Packaging a Project into a JAR File
Problem
Solution
Discussion
See Also
8.3. Creating a WAR File
Problem
Solution
Discussion
See Also
8.4. Running an Application as a Daemon
Problem
Solution
Discussion
See Also
8.5. Alleviating Performance Problems with Type Hinting
Problem
Solution
Discussion
See Also
8.6. Fast Math with Primitive Java Arrays
Problem
Solution
Discussion
See Also
8.7. Simple Profiling with Timbre
Problem
Solution
Discussion
See Also
8.8. Logging with Timbre
Problem
Solution
Discussion
See Also
8.9. Releasing a Library to Clojars
Problem
Solution
Discussion
See Also
8.10. Using Macros to Simplify API Deprecations
Problem
Solution
Discussion
See Also
Chapter 9. Distributed Computation
9.0. Introduction
Cascalog
9.1. Building an Activity Feed System with Storm
Problem
Solution
Discussion
See Also
9.2. Processing Data with an Extract Transform Load (ETL) Pipeline
Problem
Solution
Discussion
See Also
9.3. Aggregating Large Files
Problem
Solution
Discussion
See Also
9.4. Testing Cascalog Workflows
Problem
Solution
Discussion
See Also
9.5. Checkpointing Cascalog Jobs
Problem
Solution
Discussion
See Also
9.6. Explaining a Cascalog Query
Problem
Solution
Discussion
See Also
9.7. Running a Cascalog Job on Elastic MapReduce
Problem
Solution
Discussion
See Also
Chapter 10. Testing
10.0. Introduction
10.1. Unit Testing
Problem
Solution
Discussion
See Also
10.2. Testing with Midje
Problem
Solution
Discussion
See Also
10.3. Thoroughly Testing by Randomizing Inputs
Problem
Solution
Discussion
See Also
10.4. Finding Values That Cause Failure
Problem
Solution
Discussion
See Also
10.5. Running Browser-Based Tests
Problem
Solution
Discussion
See Also
10.6. Tracing Code Execution
Problem
Solution
Discussion
See Also
10.7. Avoiding Null-Pointer Exceptions with core.typed
Problem
Solution
Discussion
See Also
10.8. Verifying Java Interop Using core.typed
Problem
Solution
Discussion
See Also
10.9. Type Checking Higher-Order Functions with core.typed
Problem
Solution
Discussion
See Also
Index
About the Authors