Book Image

Clojure for Java Developers

Book Image

Clojure for Java Developers

Overview of this book

We have reached a point where machines are not getting much faster, software projects need to be delivered quickly, and high quality in software is more demanding as ever. We need to explore new ways of writing software that helps achieve those goals. Clojure offers a new possibility of writing high quality, multi-core software faster than ever, without having to leave your current platform. Clojure for Java developers aims at unleashing the true potential of the Clojure language to use it in your projects. The book begins with the installation and setup of the Clojure environment before moving on to explore the language in-depth. Get acquainted with its various features such as functional programming, concurrency, etc. with the help of example projects. Additionally, you will also, learn how the tooling works, and how it interacts with the Java environment. By the end of this book, you will have a firm grip on Clojure and its features, and use them effectively to write more robust programs.
Table of Contents (14 chapters)
Clojure for Java Developers
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Destructuring in Clojure


Destructuring is a feature in Clojure that is not common in other lisps; the idea is to allow you to write more concise code in scenarios where code doesn't really add value (for example, getting the first element from a list or the second parameter from a function) and concentrating only on what is important to you.

In order to understand this better, let's see an example of why destructuring can help you:

(let [v [1 2 3]] [(first v) (nth v 2)]) ;; [1 3]

What's wrong with the previous code? Nothing really, but you need to start thinking about what is v, what the first value of v is, what the nth function does, and at what index v starts.

Instead we can do this:

(let [[f s t] [1 2 3]] [f t]) ;; [1 3]

Once you are used to destructuring, you will see that you don't need to think about how to get the elements you need. In this case, we directly access the first, second, and third elements from our vector and use the first and third out of the three elements. With good naming...