Chapter 1. Clojure philosophy
Listing 1.1. Function that solves math equations, evaluated right to left
Listing 1.2. Function that solves math equations, evaluated left to right
Listing 1.3. Function that changes evaluation order depending on operation weights
Listing 1.4. Polymorphic Concatenatable protocol
Listing 1.5. Simple chessboard representation in Clojure
Listing 1.6. Querying the squares of a chessboard
Chapter 4. On scalars
Chapter 5. Collection types
Listing 5.1. Function for finding the neighbors of a spot on a 2D matrix
Chapter 6. Being lazy and set in your ways
Listing 6.1. Short-circuiting if expression
Listing 6.2. Using lazy-seq to avoid stack overflows
Listing 6.3. Infinite sequences fostering declarative solutions
Chapter 7. Functional programming
Listing 7.1. Function to recursively convert units of measure
Listing 7.2. Using mutually recursive functions to implement a finite state machine
Listing 7.3. Function to estimate the straight-line remaining path cost
Listing 7.4. Function to calculate the cost of the path traversed so far
Listing 7.5. Function to calculate the estimated total cost of the path
Listing 7.6. Function to retrieve the minimum value based on a criteria function
Listing 7.7. Main A* algorithm
Chapter 8. Macros
Listing 8.1. Contract top-level macro
Listing 8.2. Contract auxiliary function build-contract
Listing 8.3. Composition of the contract function and constrained function
Chapter 9. Combining data and code
Listing 9.1. Persistent binary tree built from records
Listing 9.2. Complete implementations of FIXO for TreeNode and vector
Listing 9.3. Using a map to extend FIXO to TreeNode
Listing 9.4. Size-limited stack FIXO using reify
Chapter 10. Mutation and concurrency
Listing 10.1. 3 x 3 chess board representation using Clojure refs
Listing 10.2. Setting up the refs example
Listing 10.3. Using alter to update refs in a transaction
Listing 10.4. How to make a ref squirm
Listing 10.5. Controlling I/O with an agent
Listing 10.6. Resettable memoize function
Listing 10.7. Simple SafeArray protocol
Listing 10.8. Implementating SafeArray using the locking macro
Chapter 11. Parallelism
Listing 11.1. Converting an XML feed to an XML zipper
Listing 11.2. Normalizing RSS and Atom feed entries to a common structure
Listing 11.3. Retrieving the title text from the normalized feed structure
Listing 11.4. Function to count the number of occurrences of text
Listing 11.5. Manually spreading tasks over a sequence of futures
Listing 11.6. Macro to dispatch a sequence of futures
Listing 11.7. Counting text occurrences in feed titles fetched in parallel
Listing 11.8. Dispatching a sequence of promises across threads
Listing 11.9. Parallel test runner using with-promises
Listing 11.10. Transforming a callback-based function to a blocking call
Chapter 12. Java.next
Listing 12.1. Importing Java classes to create a simple, dynamic web server
Listing 12.2. Simplest possible web server using Java
Listing 12.3. Defining a default web handler using proxy
Listing 12.4. Web handler that echoes the request headers
Listing 12.5. “Around” filter that wraps an output stream with head and body tags
Listing 12.6. Quick and dirty function to generate HTML file listings
Listing 12.7. Function to build a string representation of a file-size listing
Listing 12.8. Function to convert a relative-path URI into a file
Listing 12.9. Web handler to list and navigate a local file system
Listing 12.10. DynaFrame class ns block showing rich gen-class usage
Listing 12.11. Simple GUI containers
Listing 12.12. Set of simple widgets
Listing 12.13. A more complex GUI example
Listing 12.14. java.util.List conformance for sequences and seqs
Listing 12.15. Interface defining a sliceable object
Listing 12.16. Dummy reified ISliceable
Listing 12.17. Using a protocol to extend ISliceable
Listing 12.18. Extending strings along the Sliceable protocol
Chapter 13. Why ClojureScript?
Listing 13.1. Extending ICounted to LinkedMap
Listing 13.2. Extending IFn to LinkedMap
Listing 13.3. Definition of the print-ast function
Listing 13.4. project.clj for music
Listing 13.5. HTML contents of music.html
Listing 13.6. Web Audio functions in ClojureScript
Listing 13.7. Tune-construction functions
Listing 13.8. Using analyze to generate an AST for a ClojureScript file
Listing 13.9. consecutive-notes function fixed to work in Clojure
Chapter 14. Data-oriented programming
Listing 14.1. Function to read distance units
Listing 14.2. Checking the form of an event
Listing 14.3. Event-sourcing function that affects state
Listing 14.4. Function that applies an effect only when the event is valid
Listing 14.5. Event-sourcing, mass-effect function
Listing 14.6. Data model for representing baseball player abilities
Listing 14.8. Applying a baseball result event to a database
Listing 14.9. Transactionally applying result events to a data store
Listing 14.10. Generating a random baseball event based on player ability
Listing 14.11. Generating a number of random baseball events
Listing 14.12. Creating or retrieving a unique agent for a given player name
Listing 14.13. Feeding an event into the data store and player event store
Listing 14.14. Feeding all events into a data store and player event stores
Listing 14.15. Simulation driver
Listing 14.16. Driving the calculation of compositional units of measure
Chapter 15. Performance
Listing 15.1. Protocol for caching
Listing 15.3. Type implementing pluggable memoization
Listing 15.4. Applying pluggable memoization to a function
Listing 15.5. Tail-recursive factorial, with no type declarations
Listing 15.6. Factorial with a coerced local
Listing 15.7. Factorial with a primitive long argument
Listing 15.8. Factorial without overflow checking
Listing 15.9. Factorial with a primitive double argument
Listing 15.10. Factorial with auto-promotion
Listing 15.11. Reimplementing Clojure’s range function using lazy-seq
Listing 15.12. Reimplementation of range that returns a reducible
Listing 15.13. Essence of mapping, bundled to be used with a reducible
Listing 15.14. Essence of filtering, bundled to be used with a reducible
Listing 15.15. Essence of mapcatting, bundled to be used with a reducible
Listing 15.16. A couple of reducible transformers
Listing 15.17. Converting transformers to core reducibles
Listing 15.18. Implementing a reducible-range via the CollReduce protocol
Chapter 16. Thinking programs
Listing 16.1. Printing the starting position of a Sudoku board
Listing 16.2. Brute-force Sudoku solver
Listing 16.3. Identifying logic variables
Listing 16.4. Simplified satisfiability function
Listing 16.5. Function that satisfies seqs
Listing 16.6. Walking a data structure and substituting logic variables for bound values
Listing 16.7. Melding two seqs, substituting logic variables
Listing 16.8. Querying planetary bodies
Listing 16.9. Recursively initializing a Sudoku board filled with logic variables
Chapter 17. Clojure changes the way you think
Listing 17.1. select * example using Clojure’s relational algebra functions
Listing 17.2. Shuffling SQL-like operators into infix position
Listing 17.3. Looking up syntax processors in the processor table
Listing 17.4. Building a SQL-like SELECT statement DSL
Listing 17.5. Using with-redefs to create stubs
Listing 17.6. clojure.test as a partial specification
Listing 17.7. Creating formulas that are like spreadsheet cells
Listing 17.8. defformula to track baseball averages
Listing 17.9. Basis for an abstract factory implementation using multimethods
Listing 17.10. Defining concrete factories using multimethods
Listing 17.11. Protocols describing system-level and simulation capabilities
Listing 17.12. Using abstract system construction and a system-level protocol
Listing 17.13. Extending an existing type to the Sys and Sim protocols
Listing 17.14. Extending the existing HiFiSim type to Sys and Sim
Listing 17.15. Calculating both a low- and a high-fidelity answer
Listing 17.16. Creating a mock of a system
Listing 17.17. Extending a mock system to existing protocols.
Listing 17.18. Construction function for the mock system
Listing 17.19. Tying together a system via configuration
Listing 17.20. Injecting dependencies
Listing 17.21. Handling nefarious tree nodes with exceptions
Listing 17.22. Example of dynamic delegation
Listing 17.23. A modest debug console reader
Listing 17.24. Creating a map of the local context using &env
Listing 17.25. Implementation of a breakpoint macro
Listing 17.26. Using multiple breakpoints in the keys-apply function