Preface

Who This Book Is For

Clojure Recipes is for people who have started on their journey into Clojure but haven’t quite found their feet. Ideally, you are aware that Clojure has lots of parentheses, but the prospect of integrating some libraries to build a working project is still a bit daunting.

If you’re comfortable with Clojure and feel like you could easily build a project in a weekend without assistance, then this book is still useful. This is the book you give to someone in the office who is just curious, or who has seen Clojure and wants to get started but needs a helping hand.

Finally, if you’re a pragmatist who just needs to get some working code running, then this book is for you. There are lots of examples for you to copy and paste to get your project working.

What This Book Is Not

Clojure Recipes is not an “introduction to Clojure” book. There are some really brilliant books and online resources that cover this topic area. If you want an in-depth explanation of Clojure, then read one of those. This is a “learn by doing” type of book.

What This Book Is About

Clojure Recipes is about “the weekend project.” It’s about getting something running in a short amount of time. The book makes no assumptions about background knowledge of Clojure, but provides all you need in packaged bites.

The aim of the book is to provide self-contained projects that would have “just enough” for you to get running and see all the pieces hang together. The idea is that you can tweak and extend these projects for your needs.

Why Clojure?

So why should you use Clojure for your next project? Here are a few reasons:

Image Clojure focuses on isolating side effects. Whether it is regular business logic or a concurrency scenario, immutability makes your program easier to reason about and cancels out a whole class of bugs due to state mutation. You can still modify state, but the language encourages you to use it only where necessary.

Image Clojure was one of the first languages to make serious use of the ideas in Chris Okasaki’s book Purely Functional Data Structures (Cambridge University Press, 1999). The big revelation in that book was that data structures implemented in a functional way could be done with an upper bound of time O(n) on costs for reads and writes.

Image The benefit of these purely functional data structures was to enable another way to think about Software Transactional Memory (STM) in your program. Clojure introduces constructs like Multiversion Concurrency Control (MVCC) at the application level that previously developers typically only relied upon at the database level.

Image Clojure is great at concurrency. In Clojure it is easy to reason about how your program will behave in a concurrency scenario even when there are multiple processes making changes to the one data structure.

Image Clojure is pragmatic. Clojure runs on the JVM (in addition to the .NET CLR and JavaScript execution environments). This brings a wealth of libraries from the Java world that can be reused. What’s more, when you deploy it, it can look like a jar file so there is no need to tell anyone you’re using Clojure at all!

Image Clojure has great tooling. Because Clojure runs on the JVM, much of the tooling associated with the JVM for deployment and monitoring is still available to you in the Clojure domain.

Image Clojure is a Lisp—one of the oldest programming languages around. This brings a rich heritage of distinctive problem-solving styles and the wisdom of many graybeards who have been chipping away at computing problems for many years.

Image Clojure is great at Domain Specific Languages (DSLs). Clojure makes it easy to define languages specifically targeted to the problem you’re dealing with. You can easily represent your problem in a completely new way.

Image Clojure is fast for developing an end product. Clojure gives you the ability to incrementally compile your program at the REPL, to experiment with it, and to interact with it. With hundreds of functions in the Core libraries, plus access to all the JDK libraries, developers can get a lot done.

Image Clojure enables Lean Software Development because, more than other languages, it allows you to delay making decisions about the structure of your program. This comes from a focus on Composition over Inheritance in its idiomatic style, as well as from being able to start writing your program without tying yourself down to a particular expression of the core data.

Image Clojure is dynamically typed. Clojure keeps track of the type at run time, so the programmer doesn’t have to. This point is enormously controversial at present for three reasons:

1. Types enable you to reason about the behavior of your program in a large system at Compile time. For this reason Clojure enables gradual typing via the core.typed library. This way you can add types to your Clojure program as you need it.

2. Clojure is built to interact with Java, which is typed. The Clojure interop with Java allows type hints and type inference.

3. Much talk is still being made about Philip Wadler’s papers from the early 1990s on Types and Monads. In particular, proponents claim that Wadler’s ideas enable a compiler-based approach to Edsger Dijkstra’s claim that “[m]uch of the essence of building a program is in fact the debugging of the specification.” Clojure answers this third reason for controversy in two ways. First, Clojure borrows much from the Haskell language and so in a way pays it great homage. Second, the Clojure community values simplicity, probably to the extent that you should know what you want your program to do and whether it is correct, rather than relying on a Compiler to tell you that. (But of course Clojure has a limited ability to use Monads if that’s what you really want.)

Image Clojure has the REPL, which is an enormously powerful tool for rapid prototyping, testing, and making changes to your program while it is running.

Image Clojure has uniform syntax (S-expressions), where the primary representation of the program is a data structure in the language itself (sometimes called homo-iconic). This might not seem like a big deal, or you might find all the parentheses enormously obnoxious. (Regarding the latter point, several people have suggested that Clojure has fewer parentheses than Java for the equivalent code!) The real benefit of uniform syntax is macros. You can transform and generate Clojure code at Compile time or at run time. One might wonder at the fuss being made over “code as data” until you contrast it with other languages and see the excitement that comes when a new language syntax feature is introduced. Nine times out of ten, that language syntax feature could be implemented as a macro in Clojure. (A key example being core.async.)

Image Clojure is highly expressive and extensible. What does the expressiveness of a programming language even refer to? The claim is that you can achieve more with fewer lines of code. Paul Graham made particularly high claims with his Arc challenge. Whether his Arc language was superior was never really answered, but his broader point was that homoiconic languages have a natural advantage in being able to express more ideas in fewer lines of code with the power of homoiconic syntax and macros.

Image The Clojure community has been enormously innovative. Particular projects of note have been the following:

– Cascalog—a DSL to generate Hadoop queries with far fewer lines of code.

– Storm—a highly available, distributed system for processing real-time data.

– Datomic—a distributed, no-SQL database with point-in-time reproduction of all data, even after “updates.”

– ClojureScript—a JavaScript generator using Clojure syntax enabling DOM transformations and better ways to solve the “callback hell” problem. This is now self-hosting.

core.typed—enables “gradual typing” for your application so you can add types as required in order to reason about your program.

core.async—restructures code to provide an inversion of the calling paradigm, allowing the developer to avoid “callback hell,” to better enable the processing of multiple real-time threads of information, and complementing the Clojure implementation with similar syntax and functionality in the browser with ClojureScript.

Image Clojure allows lots of different programming styles. Of course, it is idiomatic to write immutable functional code in Clojure. But you could write in a stack-based coding style like FORTH if you wanted. Clojure does have some ability to provide compile-time guarantees via Monads. It is even possible to write procedural blocks of code that mutate state. Clojure is flexible.

So take a look at Clojure! It will be great for your next project.

Coding Conventions Used in This Book

Sometimes we run a command on the command line. It looks like this:

lein new myproject

Then sometimes we show the result of running a command on the command line. It looks like this:

Generating a project called myproject based on the 'default' template.

Sometimes we show some code in a Clojure file. It looks like this:

(ns myproject)

(defn -main[& args]
 (prn "running"))

Sometimes we show a command run on a Clojure REPL. It looks like like this:

user=> (prn "Hello World")

Then sometimes we show the result of running a command on the Clojure REPL. It looks like this:

"Hello World"

Errata

There is an errata page on the Clojure Recipes website here:

http://clojurerecipes.net/errata.html

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.71.211