The Joy of Clojure
Michael Fogus, Chris Houser
Format: PDF / Kindle (mobi) / ePub
The Joy of Clojure, Second Edition is a deep look at the Clojure language. Fully updated for Clojure 1.6, this new edition goes beyond just syntax to show you the "why" of Clojure and how to write fluent Clojure code. You'll learn functional and declarative approaches to programming and will master the techniques that make Clojure so elegant and efficient.
Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.
About the Technology
About the Book
The Joy of Clojure, Second Edition is a deep account of the Clojure language. Fully updated for Clojure 1.6, this new edition goes beyond the syntax to show you how to write fluent Clojure code. You'll learn functional and declarative approaches to programming and will master techniques that make Clojure elegant and efficient. The book shows you how to solve hard problems related to concurrency, interoperability, and performance, and how great it can be to think in the Clojure way.
Appropriate for readers with some experience using Clojure or common Lisp.
- Build web apps using ClojureScript
- Master functional programming techniques
- Simplify concurrency
- Covers Clojure 1.6
About the Authors
Michael Fogus and Chris Houser are contributors to the Clojure and ClojureScript programming languages and the authors of various Clojure libraries and language features.
Table of Contents
- Clojure philosophy
- Drinking from the Clojure fire hose
- Dipping your toes in the pool
- On scalars
- Collection types
- Being lazy and set in your ways
- Functional programming
- Combining data and code
- Mutation and concurrency
- Why ClojureScript?
- Data-oriented programming
- Thinking programs
- Clojure changes the way you think
PART 1 FOUNDATIONS
PART 2 DATA TYPES
PART 3 FUNCTIONAL PROGRAMMING
PART 4 LARGE-SCALE DESIGN
PART 5 HOST SYMBIOSIS
PART 6 TANGENTIAL CONSIDERATIONS
1998.8 Although the essay is interesting as a survey of startup culture, the truly memorable part is the description of how Viaweb used the programming language Lisp as an advantage over its competition. How could a programming language more than 50 years old provide any market advantage versus Viaweb’s competitors, which were surely using modern enterprise technologies? We won’t repeat the exact terms of the essay, but Graham makes a compelling case for the capability of Lisp to facilitate a
“You keep using that word. I do not think it means what you think it means.” Although storage to disk may be the more common meaning of persistent today, Clojure uses an older meaning of the word having to do with immutable in-memory collections with specific properties. In particular, a persistent collection in Clojure allows you to preserve historical versions (Okasaki 1999) of its state and promises that all versions will have the same update and lookup complexity guarantees. The specific
value, is in a collection. So for a vector, the “keys” are the indices of its elements (the first element is at index 0, and so on). Although it’s sometimes useful to check whether an index is contained in a vector using contains?, more often than not you want to find a value. In this section, we showed how to create vectors using literal syntax or by building them up programmatically. We looked at how to push them, pop them, and slice them. We also looked at some of the things vectors can’t do
of structure is simple and powerful12 and may be warranted in some situations, Clojure provides other ways of associating functions with data objects that are more flexible. In fact, the desire to avoid a widespread need for this type of ad hoc implementation inspired the creation of Clojure’s reify macro, which we’ll cover in section 9.3.2. COMPILE-TIME VS. RUNTIME When you look at code that includes a closure, it’s not immediately obvious how the work is distributed between compile-time and
RECURSION The following function calculates the greatest common denominator of two numbers: (defn gcd (cond (> x (< x :else [x y] y) (gcd (- x y) y) y) (gcd x (- y x)) x)) The implementation of gcd is straightforward, but notice that it uses mundane recursion instead of tail recursion via recur. In a language such as Scheme, containing generalized tail-call optimization, the recursive calls are optimized automatically. On the other hand, because of the JVM’s lack of tail-call optimization, the