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

Clojure's data types


Now is when everything you know about Java pays off; even the list forms that you saw earlier implement the java.util.List interface. Clojure was designed to be embeddable and to have a great integration with the host platform, so it's only natural that you can use everything you already know about Java types and objects.

There are two data types in Clojure: scalars and collections.

Scalars

In every language you need primitive types; you use them in everyday life as they represent numbers, strings, and Booleans. These primitive types are called scalars in the Clojure world.

Clojure has a couple of very interesting types like ratios and keywords. In the following table, you get to know the different types of scalars, how they compare to Java and a simple example of how to use each of them.

Clojure data type

Java data type

Sample

Description

String

String

"This is a string"

"This is a multiline string"

A string of characters; in Clojure you can use multiline strings without a problem

Boolean

Boolean

true

false

Literal Boolean values

Character

Character

\c

\u0045 ;; Unicode char 45 E

Character values, they are java.lang.Character instances, you can define Unicode characters

Keywords

Doesn't exist in java

:key

:sample

:some-keyword

They evaluate themselves and they are often used as keys. They are also functions that look for themselves in a map.

Number

Numbers are automatically handled as BigDecimal, BigInteger or lower precision depending on what's necessary

42N ;;Big Integer

42 ;;long

0.1M ;;BigDecimal

It is important to remember the trade-offs of Java numbers, if precision is important, you should always use big decimals and bigintegers.

Ratio

Doesn't exist

22/7

Clojure provides great numerical precision; if necessary it can retain the ration and execute exact operation. The tradeoff when using ratios is speed.

Symbol

Doesn't exist

some-name

Symbols are identifiers in Clojure, very similar to a variable name in Java.

nil

null

nil

The null value

Regular expressions

java.util.regex.Pattern

#"\d"

Regular expressions, in Clojure you get free syntax to define regular expressions, but in the end it is a plain old Java reggae Pattern

Collection data types

In Clojure there are two types of collections: sequential and associative collections. Sequential are things you can iterate, such as lists. Associative collections are maps, sets, and things you can access by a certain index. Clojure's collections are fully compatible with Java and it can even implement the java.util interfaces, such as java.util.List and java.util.Map.

One of the main characteristics of collections in Clojure is that they are immutable; it has a lot of benefits that we'll see later.

Let's have a look at the characteristics of each collection data type available in Clojure and compare them with Java with the help of a sample (in Clojure) and its description.

Clojure data type

Java data type

Sample

Description

List

List

(1 2 3 4 5)

A simple list, notice the quote character before the list, if you don't specify it Clojure will try to evaluate the form as an instruction

Vector

Array

[1 2 3 4 5]

It is the main workhorse in Clojure, it is similar to an array because you can access elements in a random order

Set

HashSet

#{1 2 3 4}

A normal Java hash set

Map

HashMap

{:key 5 :key-2 "red"}

A Clojure map