Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

About this Book

About this Book

Programming languages vary a great deal on the productivity spectrum. On the one extreme, we have machine code and assembly language. Then come languages like C, eventually giving way to C++, Java, Scala, and others. On the other side of the spectrum are the functional and dynamic languages. Some of the favorites here include Ruby, Python, Perl, Erlang, Haskell, ML, and others. And these are just a tiny fraction of the landscape—there are dozens of other popular languages, and hundreds more that aren’t in as much use.

With this dizzying alphabet soup of options, how does one choose the right language? Quite rightly, a lot of folks realize that there is no single correct choice, and that it depends on the job at hand. Even so, most people have one general purpose language that they use for most tasks. This book is about a new language called Clojure—one that is a compelling choice for general purpose programming.

The Clojure programming language has been influenced by dozens of languages and has taken the best of many worlds to become what it is today. Clojure is a modern Lisp, and it embraces the functional paradigm. It also runs on the JVM. This makes for a very potent combination. In today’s world, where programmer productivity is paramount, Clojure shines. All else being equal, a good Clojure team can run circles around significantly larger teams using other languages. I’ve seen this in my own startup over the past three years.

How to use this book

Learning Clojure can be quite a leap for a lot of programmers. The drastically different syntax, the move from imperative to functional programming, immutability, the macro system ... these can be daunting. This book takes a slow and steady approach to learning the language and the various pieces. It assumes no prior experience with Lisp or with any functional programming language. It starts out with the absolute basics, and slowly layers on the different features of the language in a way to make it all fit together in an intuitive manner. It takes a first-principles approach to all the topics, first explaining why something needs to be done a certain way, and only then talking about the Clojure way.

Once you get past the basics, the book moves onto real-world usage of Clojure. You’ll see how to write test-driven Clojure, access data-stores of various kinds (both relational and the NoSQL variety), create web services, use messaging to scale your applications to handle large volumes of traffic, use map/reduce to process data, understand distributed computing, and build up your business logic through domain-specific languages (DSLs).

To get the most out of the book, I’ve assumed you’re familiar with an OO language like Java, C#, or C++, but no background in Lisp or Clojure is required.

Roadmap

Chapter 1 whets your appetite by giving a high-level description of the language, and what to expect from the remainder of the book

Chapter 2 goes over the the basics of installing and getting started with Clojure. It then dives into the most fundamental concept in Clojure—that of a first-class function. It completes the introduction by addressing program flow and the core data-structures.

Chapter 3 is about going deeper: it addresses functions in more depth, and then describes the scoping rules of the language. You’ll discover that in addition to lexical scoping, Clojure supports dynamic scope, which behaves very differently. This chapter also addresses another unique feature of the language: destructuring. This lets you disintegrate data-structures to get at just those parts that you care about. Finally, it looks at Clojure’s support of meta-data, that is data that can be used to annotate other data. A lot of this will be different from what you may be used to, but at the end of this chapter, you’ll be able to read and write most simple Clojure programs.

Chapter 4 discusses Clojure’s approach to polymorphism. If you’re coming from the Java/C++ world, this is going to be quite different. Clojure’s multimethods are an extremely open-ended way to implement polymorphic behavior, and they give the control of method dispatch directly to the programmer.

Chapter 5 covers how Clojure embraces the JVM. No programming language can succeed without a strong set of libraries, and Clojure neatly sidesteps this problem. It makes it trivial to use any Java library in your programs, giving you instant access to the thousands of battle-tested frameworks and libraries available. It also lets you continue to benefit from your previous investment in the Java stack.

Chapter 6 addresses Clojure’s approach to state-management and concurrency. Again, this is a fresh take on the problem of mutable state. Clojure sports extremely performant immutable data-structures and implements an efficient STM system (software transactional memory). This combination lets the language offer built-in support for correct, safe, and lock-free concurrency. This is a big deal! Your programs can take advantage of multiple cores without any of the problems associated with traditional multithreaded code.

Chapter 7 looks at yet another feature of Clojure that is different from most other programming languages. This is the macro system (not to be confused with C macros and the like). Clojure essentially provides language-level support for code-generation. It has a hook in its runtime that allows programmers to transform and generate code any way they like. This is an incredibly powerful feature that blurs the line between the language designer and an application programmer. It allows anyone to add features to the language.

Chapter 8 shows how you can raise your productivity level significantly by combining the process of writing test-driven code with the Clojure REPL (read-eval-print-loop, which is Clojure’s command prompt shell). It also addresses mocking and stubbing functions to enable better unit-testing tactics.

Chapter 9 is about data storage. It not only talks about traditional relational databases such as MySQL, but also newer NoSQL ones such as HBase and Redis. With this information, you’ll be able to pick the right one for your project, and know how to access them from Clojure in an idiomatic manner.

Chapter 10 looks at Clojure and the web. In this chapter, you’ll build a simple web-service framework on your own. You’ll also explore a few open-source projects that make it trivial to talk HTTP in your own projects—whether it is an API server or a dynamic website.

Chapter 11 shows how to use messaging systems to communicate between multiple Clojure processes. Specifically, you’ll use RabbitMQ to build a distributed computing framework that can form the basis of your own little Clojure compute cluster.

Chapter 12 is about data processing with Clojure. It explains the map/reduce paradigm using the Clojure functions of the same name. You’ll build a little map/reduce library that can be used as the basis for your own data-processing programs. Finally, you’ll create a distributed master/slave data-processing framework that will allow you to process large volumes of data by harnessing Clojure worker processes running on multiple computers.

Chapter 13 dives deep into the functional programming paradigm. You’ll create your own versions of the core higher-order functions: map, reduce, and filter. You’ll also get a thorough understanding of partial application and currying of functions. Finally, you’ll build your own OOP system on top of Clojure, and will lay to rest the concern about how Clojure relates to the OO paradigm. In fact, you’ll not think of OO in the same way again.

Chapter 14 deals with the expression problem. You’ll first review what this age-old problem is, and then you’ll use Clojure multimethods to solve it in an elegant fashion. Then, you’ll look at Clojure’s own high-performance solution to it.

Chapter 15 is the last chapter and focuses on advanced macros and DSLs. This will bring you full circle: we started out in search of a tool that minimizes accidental complexity. Clojure allows you to bend the programming language to your will through the macro system, and this chapter takes a deeper dive into this feature. You’ll design an internal DSL that will serve as an example of how you can use DSLs to drive core business logic in your Clojure applications.

Code conventions and downloads

All code in the book is presented in a fixed-width font like this to separate it from ordinary text. Code annotations accompany many of the listings, highlighting important concepts. In some cases, numbered bullets link to explanations that follow the listing.

Please see chapter 2 for instructions on how to download and install Clojure. You will find the full code for all the examples in the book available for download from the publisher’s website at http://www.manning.com/ClojureinAction.

Author Online

The purchase of Clojure in Action includes free access to a private forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the author and other users. You can access and subscribe to the forum at http://www.manning.com/ClojureinAction. This page provides information on how to get on the forum once you’re registered, what kind of help is available, and the rules of conduct in the forum.

Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the author can take place. It isn’t a commitment to any specific amount of participation on the part of the author, whose contributions to the book’s forum remain voluntary (and unpaid). We suggest you try asking the author some challenging questions, lest his interest stray!

The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.

About the cover illustration

On the cover of Clojure in Action is “A woman from Sinj,” a town in Croatia about 30 kilometers north of Split. The illustration is taken from a reproduction of an album of Croatian traditional costumes from the mid-nineteenth century by Nikola Arsenovic, published by the Ethnographic Museum in Split, Croatia, in 2003. The illustrations were obtained from a helpful librarian at the Ethnographic Museum in Split, itself situated in the Roman core of the medieval center of the town: the ruins of Emperor Diocletian’s retirement palace from around AD 304. The book includes finely colored illustrations of figures from different regions of Croatia, accompanied by descriptions of the costumes and of everyday life.

Sinj is located in the Dalmatian region of Croatia and women’s costumes in Dalmatia consist of layers of clothing worn over more clothing: a white blouse, skirt, or tunic is most common, with a colorful, embroidered apron decorated with complicated geometric patterns and fringes worn on top, as well as a red vest and black coat with colorful stitching added to stand out from the white blouse underneath. Jewelry consists mainly of beads worn around the neck or silver coins added as adornments to the costume. Both men and women wear a red or white pillbox cap (called a bareta or crvenkapa), with a white veil attached to the women’s cap, like in the illustration on this cover.

Dress codes and lifestyles have changed over the last 200 years, and the diversity by region, so rich at the time, has faded away. It is now hard to tell apart the inhabitants of different continents, let alone of different hamlets or towns separated by only a few miles. Perhaps we have traded cultural diversity for a more varied personal life—certainly for a more varied and fast-paced technological life.

Manning celebrates the inventiveness and initiative of the computer business with book covers based on the rich diversity of regional life of two centuries ago, brought back to life by illustrations from old books and collections like this one.

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint