Clojure Cookbook

Book description

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.

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. How This Book Was Written
    2. Audience
    3. Other Resources
    4. Structure
    5. Software Prerequisites
    6. Conventions Used in This Book
    7. Using Code Examples
    8. Safari® Books Online
    9. How to Contact Us
    10. Acknowledgments
  2. 1. Primitive Data
    1. 1.0. Introduction
    2. 1.1. Changing the Capitalization of a String
    3. 1.2. Cleaning Up Whitespace in a String
    4. 1.3. Building a String from Parts
    5. 1.4. Treating a String as a Sequence of Characters
    6. 1.5. Converting Between Characters and Integers
    7. 1.6. Formatting Strings
    8. 1.7. Searching a String by Pattern
    9. 1.8. Pulling Values Out of a String Using Regular Expressions
    10. 1.9. Performing Find and Replace on Strings
    11. 1.10. Splitting a String into Parts
    12. 1.11. Pluralizing Strings Based on a Quantity
    13. 1.12. Converting Between Strings, Symbols, and Keywords
    14. 1.13. Maintaining Accuracy with Extremely Large/Small Numbers
    15. 1.14. Working with Rational Numbers
    16. 1.15. Parsing Numbers
    17. 1.16. Truncating and Rounding Numbers
    18. 1.17. Performing Fuzzy Comparison
    19. 1.18. Performing Trigonometry
    20. 1.19. Inputting and Outputting Integers with Different Bases
    21. 1.20. Calculating Statistics on Collections of Numbers
    22. 1.21. Performing Bitwise Operations
    23. 1.22. Generating Random Numbers
    24. 1.23. Working with Currency
    25. 1.24. Generating Unique IDs
    26. 1.25. Obtaining the Current Date and Time
    27. 1.26. Representing Dates as Literals
    28. 1.27. Parsing Dates and Times Using clj-time
    29. 1.28. Formatting Dates Using clj-time
    30. 1.29. Comparing Dates
    31. 1.30. Calculating the Length of a Time Interval
    32. 1.31. Generating Ranges of Dates and Times
    33. 1.32. Generating Ranges of Dates and Times Using Native Java Types
    34. 1.33. Retrieving Dates Relative to One Another
    35. 1.34. Working with Time Zones
    36. 1.35. Converting a Unix Timestamp to a Date
    37. 1.36. Converting a Date to a Unix Timestamp
  3. 2. Composite Data
    1. 2.0. Introduction
    2. 2.1. Creating a List
    3. 2.2. Creating a List from an Existing Data Structure
    4. 2.3. “Adding” an Item to a List
    5. 2.4. “Removing” an Item from a List
    6. 2.5. Testing for a List
    7. 2.6. Creating a Vector
    8. 2.7. “Adding” an Item to a Vector
    9. 2.8. “Removing” an Item from a Vector
    10. 2.9. Getting the Value at an Index
    11. 2.10. Setting the Value at an Index
    12. 2.11. Creating a Set
    13. 2.12. Adding and Removing Items from Sets
    14. 2.13. Testing Set Membership
    15. 2.14. Using Set Operations
    16. 2.15. Creating a Map
    17. 2.16. Retrieving Values from a Map
    18. 2.17. Retrieving Multiple Keys from a Map Simultaneously
    19. 2.18. Setting Keys in a Map
    20. 2.19. Using Composite Values as Map Keys
    21. 2.20. Treating Maps as Sequences (and Vice Versa)
    22. 2.21. Applying Functions to Maps
    23. 2.22. Keeping Multiple Values for a Key
    24. 2.23. Combining Maps
    25. 2.24. Comparing and Sorting Values
    26. 2.25. Removing Duplicate Elements from a Collection
    27. 2.26. Determining if a Collection Holds One of Several Values
    28. 2.27. Implementing Custom Data Structures: Red-Black Trees—Part I
    29. 2.28. Implementing Custom Data Structures: Red-Black Trees—Part II
  4. 3. General Computing
    1. 3.0. Introduction
    2. 3.1. Running a Minimal Clojure REPL
    3. 3.2. Interactive Documentation
    4. 3.3. Exploring Namespaces
    5. 3.4. Trying a Library Without Explicit Dependencies
    6. 3.5. Running Clojure Programs
    7. 3.6. Running Programs from the Command Line
    8. 3.7. Parsing Command-Line Arguments
    9. 3.8. Creating Custom Project Templates
    10. 3.9. Building Functions with Polymorphic Behavior
    11. 3.10. Extending a Built-In Type
    12. 3.11. Decoupling Consumers and Producers with core.async
    13. 3.12. Making a Parser for Clojure Expressions Using core.match
    14. 3.13. Querying Hierarchical Graphs with core.logic
    15. 3.14. Playing a Nursery Rhyme
  5. 4. Local I/O
    1. 4.0. Introduction
    2. 4.1. Writing to STDOUT and STDERR
    3. 4.2. Reading a Single Keystroke from the Console
    4. 4.3. Executing System Commands
    5. 4.4. Accessing Resource Files
    6. 4.5. Copying Files
    7. 4.6. Deleting Files or Directories
    8. 4.7. Listing Files in a Directory
    9. 4.8. Memory Mapping a File
    10. 4.9. Reading and Writing Text Files
    11. 4.10. Using Temporary Files
    12. 4.11. Reading and Writing Files at Arbitrary Positions
    13. 4.12. Parallelizing File Processing
    14. 4.13. Parallelizing File Processing with Reducers
    15. 4.14. Reading and Writing Clojure Data
    16. 4.15. Using edn for Configuration Files
    17. 4.16. Emitting Records as edn Values
    18. 4.17. Handling Unknown Tagged Literals When Reading Clojure Data
    19. 4.18. Reading Properties from a File
    20. 4.19. Reading and Writing Binary Files
    21. 4.20. Reading and Writing CSV Data
    22. 4.21. Reading and Writing Compressed Files
    23. 4.22. Working with XML Data
    24. 4.23. Reading and Writing JSON Data
    25. 4.24. Generating PDF Files
    26. 4.25. Making a GUI Window with Scrollable Text
  6. 5. Network I/O and Web Services
    1. 5.0. Introduction
    2. 5.1. Making HTTP Requests
    3. 5.2. Performing Asynchronous HTTP Requests
    4. 5.3. Sending a Ping Request
    5. 5.4. Retrieving and Parsing RSS Data
    6. 5.5. Sending Email
    7. 5.6. Communicating over Queues Using RabbitMQ
    8. 5.7. Communicating with Embedded Devices via MQTT
    9. 5.8. Using ZeroMQ Concurrently
    10. 5.9. Creating a TCP Client
    11. 5.10. Creating a TCP Server
    12. 5.11. Sending and Receiving UDP Packets
  7. 6. Databases
    1. 6.0. Introduction
    2. 6.1. Connecting to an SQL Database
    3. 6.2. Connecting to an SQL Database with a Connection Pool
    4. 6.3. Manipulating an SQL Database
    5. 6.4. Simplifying SQL with Korma
    6. 6.5. Performing Full-Text Search with Lucene
    7. 6.6. Indexing Data with ElasticSearch
    8. 6.7. Working with Cassandra
    9. 6.8. Working with MongoDB
    10. 6.9. Working with Redis
    11. 6.10. Connecting to a Datomic Database
    12. 6.11. Defining a Schema for a Datomic Database
    13. 6.12. Writing Data to Datomic
    14. 6.13. Removing Data from a Datomic Database
    15. 6.14. Trying Datomic Transactions Without Committing Them
    16. 6.15. Traversing Datomic Indexes
  8. 7. Web Applications
    1. 7.0. Introduction
    2. 7.1. Introduction to Ring
    3. 7.2. Using Ring Middleware
    4. 7.3. Serving Static Files with Ring
    5. 7.4. Handling Form Data with Ring
    6. 7.5. Handling Cookies with Ring
    7. 7.6. Storing Sessions with Ring
    8. 7.7. Reading and Writing Request and Response Headers in Ring
    9. 7.8. Routing Requests with Compojure
    10. 7.9. Performing HTTP Redirects with Ring
    11. 7.10. Building a RESTful Application with Liberator
    12. 7.11. Templating HTML with Enlive
    13. 7.12. Templating with Selmer
    14. 7.13. Templating with Hiccup
    15. 7.14. Rendering Markdown Documents
    16. 7.15. Building Applications with Luminus
  9. 8. Performance and Production
    1. 8.0. Introduction
    2. 8.1. AOT Compilation
    3. 8.2. Packaging a Project into a JAR File
    4. 8.3. Creating a WAR File
    5. 8.4. Running an Application as a Daemon
    6. 8.5. Alleviating Performance Problems with Type Hinting
    7. 8.6. Fast Math with Primitive Java Arrays
    8. 8.7. Simple Profiling with Timbre
    9. 8.8. Logging with Timbre
    10. 8.9. Releasing a Library to Clojars
    11. 8.10. Using Macros to Simplify API Deprecations
  10. 9. Distributed Computation
    1. 9.0. Introduction
    2. 9.1. Building an Activity Feed System with Storm
    3. 9.2. Processing Data with an Extract Transform Load (ETL) Pipeline
    4. 9.3. Aggregating Large Files
    5. 9.4. Testing Cascalog Workflows
    6. 9.5. Checkpointing Cascalog Jobs
    7. 9.6. Explaining a Cascalog Query
    8. 9.7. Running a Cascalog Job on Elastic MapReduce
  11. 10. Testing
    1. 10.0. Introduction
    2. 10.1. Unit Testing
    3. 10.2. Testing with Midje
    4. 10.3. Thoroughly Testing by Randomizing Inputs
    5. 10.4. Finding Values That Cause Failure
    6. 10.5. Running Browser-Based Tests
    7. 10.6. Tracing Code Execution
    8. 10.7. Avoiding Null-Pointer Exceptions with core.typed
    9. 10.8. Verifying Java Interop Using core.typed
    10. 10.9. Type Checking Higher-Order Functions with core.typed
  12. Index
  13. About the Authors
  14. Colophon
  15. Copyright

Product information

  • Title: Clojure Cookbook
  • Author(s): Ryan Neufeld, Luke VanderHart
  • Release date: March 2014
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449366179