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

Preface

In the last few years, we have seen a widespread tendency to create new languages for JVM. There are all sorts of new languages with different paradigms and different ways of working.

Clojure is one of those languages, one that we believe is worth learning.

Over the course of this book, you will learn about Clojure and how opinionated it is. You will learn why immutable objects are not only possible, but it is a good idea to use them.

You will learn about functional programming and see how it fits the concept of immutable programs.

You will understand the very powerful idea of representing your code as a data structure of the same language.

It is important to note that we will build all this knowledge on top of what you already know; this book assumes that you understand the Java language and a bit of how it works. It assumes that you understand how to create classes and objects, how to call methods, and also a bit about the JVM. We will find similarities and differences from the Java language that you already know, so you can understand how the Clojure world works.

It is said that a great programmer is not the one who knows a lot of different programming languages, it is someone who knows different programming paradigms.

Clojure brings ideas from Lisp and functional programming, which are different to what you are used to. This book will help you understand the power behind these ideas and why a language so old still exists and has a place.

Learning Lisp is one of the great pleasures of life, I hope you enjoy it!

What this book covers

Chapter 1, Getting Started with Clojure, is your first step with Clojure, from how to install an interpreter, how to use the IntelliJ plugin Cursive Clojure, and how to start writing your first lines of code in Clojure.

Chapter 2, Namespaces, Packages, and Tests, deals with how every other language needs a way to organize our code. We do it with namespaces, and we start learning by doing little experiments and by comparing with the Java packages we already know.

Chapter 3, Interacting with Java, discusses the fact that one of the most useful features of Clojure is that it can be hosted on top of other platforms. Java is probably one of the most common platforms, and here we learn how to interact with Java code and libraries and how to expose our Clojure code to Java.

Chapter 4, Collections and Functional Programming, tells us that functional programming and immutable data structures (or collections) are fundamental to programming in the Clojure language; here we understand how we can write meaningful programs using immutable data structures and how functional programming is ideal for that.

Chapter 5, Multimethods and Protocols, introduces new tools and ideas of Clojure that help you write much more flexible and simple codes. Destructuring allows you to gain instant access to the data you need from a data structure. Multimethods and protocols are similar to Java's polymorphism but give you a whole new level of flexibility that you could only dream about.

Chapter 6, Concurrency, tells us about how in the modern world concurrency plays an extremely important part. This chapter also tells us about Clojure's native primitives for concurrency and again you will learn why immutable data structures are a great ally when writing concurrent programs.

Chapter 7, Macros in Clojure, deals with the fact that these are not the Excel macros you are used to, they are a way in which you can modify your program's source code at compile time. This is one of Lisp's big ideas and one that gives you immense flexibility. Here we understand a little of how to use this feature.

What you need for this book

You need the Java 8 SDK.

You should be able to run samples on any OS; our samples should be easier to follow in environments where there is a shell available. (We focus mainly on Mac OS X.)

Who this book is for

This book is for developers who are familiar with Java and the JVM.

Ideally, you know how the classloader works, how to generate JAR files and consume them; you are familiar with Java's most common libraries and classes.

If you are not familiar with this, you will be able to follow this book but you won't get the most from the comparisons and samples.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We are using :require to include functions from the clojure.test and the ns-playground.core packages."

A block of code is set as follows:

curl -O https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein
# The next step just set up the lein script in your path, you can do it any way you wish
mv lein ~/bin
echo "export PATH=$PATH:~/bin/">> ~/.bashrc
source ~/.bashrc
# Everything should be running now, let's test it
lein help

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

lein new app getting-started
cd getting-started
lein run
# Hello, world!

Any command-line input or output is written as follows:

lein uberjar

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "After that you can run any tests, just open your test file and go to Tools | Run Tests in the current NS in REPL."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/ClojureforJavaDevelopers_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.