Skip to main content
Top

2010 | Book

Practical Clojure

Authors: Luke VanderHart, Stuart Sierra

Editors: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh, Jim Markham, Tracy Brown, Katie Stence

Publisher: Apress

insite
SEARCH

About this book

This book is the first definitive reference for the Clojure language, providing both an introduction to functional programming in general and a more specific introduction to Clojure’s features. This book demonstrates the use of the language through examples, including features such as software transactional memory (STM) and immutability, which may be new to programmers coming from other languages.

Overview of functional programming and description of what sets Clojure apart from other languages Detailed explanation of Clojure’s special features Examples of real-world tasks that are well-suited to Clojure’s capabilities, starting with simple tasks and moving on to more complex applications

Table of Contents

Frontmatter
Chapter 1. The Clojure Way
Abstract
What is Clojure and why would someone want to learn it? At first glance, some may vote Clojure the least likely to succeed among modern programming languages, because it is new and complicated. Worst of all, it is just flat-out strange, a bewildering soup of parentheses and brackets to anyone not already familiar with the Lisp family of languages.
Luke VanderHart, Stuart Sierra
Chapter 2. The Clojure Environment
Abstract
To start programming in Clojure immediately, simply open a Clojure REPL, which stands for Read Evaluate Print Loop. The REPL is a simple yet powerful way to create programs interactively as well as interact with already running programs.
Luke VanderHart, Stuart Sierra
Chapter 3. Controlling Program Flow
Abstract
As a functional language, functions are the beginning and end of every Clojure program. The “shape” of any Clojure program is like a tree, each function branching out and calling other functions. Understanding a Clojure program means understanding its functions and the patterns in which they are called. Use functions carelessly and your Clojure programs will be incomprehensible spaghetti. Use them thoughtfully and your Clojure programs will be fast, elegant, and a genuine joy both to write and to read.
Luke VanderHart, Stuart Sierra
Chapter 4. Data in Clojure
Abstract
Clojure is a dynamically typed language, which means that you never need to explicitly define the data type of symbols, functions, or arguments in your programs. However, all values still have a type. Strings are strings, numbers are numbers, lists are lists, etc. If you try to perform an unsupported operation on a type, it will cause an error at runtime. It is the programmer’s responsibility to write code in such a way that this does not happen. This should be very natural to those with a dynamic language background, while it will no doubt take some getting used to for those who have only used static languages in the past.
Luke VanderHart, Stuart Sierra
Chapter 5. Sequences
Abstract
In Clojure, sequences are a unified way to read, write, and modify any data structure that is logically a collection of items. They are built into Clojure at a very basic level, and are by far the most convenient and idiomatic way to handle collections. They fill the role occupied by lists in other Lisp dialects. More than just a collection API, they are the framework around which program flow and logic are often constructed, and are designed to be as easy-to-use as the basis for recursion and higher-order function application.
Luke VanderHart, Stuart Sierra
Chapter 6. State Management
Abstract
As much as possible, Clojure advocates eliminating state from programs. In general, data should be passed and returned from functions in a purely functional way. It keeps things clean, protected, and parallelizable.
Luke VanderHart, Stuart Sierra
Chapter 7. Namespaces and Libraries
Abstract
Namespaces are the means by which you divide your Clojure code into logical groups, similar to packages in Java or modules in other languages. Almost every Clojure source file begins with a namespace declaration using the ns macro. The following code is an example of a namespace declaration:
$$ \begin{gathered} \left( {ns clojure.contrib.gen - html - docs} \right. \hfill \\ \left( {:require\left[ {clojure.contrib.duck - streams :as duck - streams} \right]} \right) \hfill \\ \left( {:use \left( {clojure.contrib seq - utils str - utils repl - utils def prxml} \right)} \right) \hfill \\ \left( {:import \left( {java.lang Exception} \right)} \right. \hfill \\ \left. { \left. { \left( {java.util.regex Pattern} \right)} \right)} \right) \hfill \\ \end{gathered} $$
Luke VanderHart, Stuart Sierra
Chapter 8. Metadata
Abstract
Programmers often talk about metadata, or data about data. The definition of metadata varies in different contexts. Clojure provides mechanisms to attach metadata to objects, but it has a very specific definition: metadata is a map of data attached to an object that does not affect the value of the object.
Luke VanderHart, Stuart Sierra
Chapter 9. Multimethods and Hierarchies
Abstract
Clojure is not an object-oriented language in the traditional sense of classes and methods, although it is built on Java’s object-oriented foundation.
Luke VanderHart, Stuart Sierra
Chapter 10. Java Interoperability
Abstract
Clojure is built on Java not only because it is a portable, feature-rich platform, but because thousands of libraries, both open-source and commercial, are written in Java. Clojure can leverage all this existing code to get past the “library problem” that plagues most new programming languages.
Luke VanderHart, Stuart Sierra
Chapter 11. Parallel Programming
Abstract
Chapter 6 spends a lot of time discussing how Clojure manages state safely in a concurrent environment. State management is definitely the trickiest part of concurrent programming, and the attention Clojure pays to getting state management right is well spent.
Luke VanderHart, Stuart Sierra
Chapter 12. Macros and Metaprogramming
Abstract
Metaprogramming is the use of code to modify or create other code. It is primarily a developer tool and acts as a force multiplier, allowing large amounts of predictable code to be generated from just a few statements in the host language (or “metalanguage”). It is extremely useful for automating repetitive, boilerplate code.
Luke VanderHart, Stuart Sierra
Chapter 13. Datatypes and Protocols
Abstract
Clojure is built on abstractions: sequences, references, macros, and so forth. However, most of those abstractions are implemented in Java, as classes and interfaces. It is difficult to add new abstractions to the language (for example, a queue data structure) without implementing them in Java.
Luke VanderHart, Stuart Sierra
Chapter 14. Performance
Abstract
In principle, Clojure can be just as fast as Java: both are compiled to Java bytecode instructions, which are executed by a Java Virtual Machine. Clojure’s design is careful to avoid features-such as continuations or a Common Lisp-like condition system-that would severely compromise performance on the JVM. But Clojure is still a young language, and has not had the benefit of hundreds of thousands of programmer-hours spent optimizing the compiler. As a result, Clojure code will generally run slower than equivalent Java code. However, with some minor adjustments, Clojure performance can usually be brought near Java performance. Don’t forget that Java is always available as a fallback for performancecritical sections of code.
Luke VanderHart, Stuart Sierra
Backmatter
Metadata
Title
Practical Clojure
Authors
Luke VanderHart
Stuart Sierra
Editors
Clay Andres
Steve Anglin
Mark Beckner
Ewan Buckingham
Gary Cornell
Jonathan Gennick
Jonathan Hassell
Michelle Lowman
Matthew Moodie
Duncan Parkes
Jeffrey Pepper
Frank Pohlmann
Douglas Pundick
Ben Renow-Clarke
Dominic Shakeshaft
Matt Wade
Tom Welsh
Jim Markham
Tracy Brown
Katie Stence
Copyright Year
2010
Publisher
Apress
Electronic ISBN
978-1-4302-7230-4
Print ISBN
978-1-4302-7231-1
DOI
https://doi.org/10.1007/978-1-4302-7230-4

Premium Partner