Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Clojure for Java Developers

You're reading from   Clojure for Java Developers Transition smoothly from Java to the most widely used functional JVM-based language – Clojure

Arrow left icon
Product type Paperback
Published in Feb 2016
Publisher
ISBN-13 9781785281501
Length 156 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Getting started with Clojure code and data

Let's take a deep dive into Clojure's syntax now, it is pretty different from other languages but it is actually much simpler. Lisps have a very regular syntax, with few special rules. As we said earlier, Clojure code is made of S-expressions and S-expressions are just lists. Let's look at some examples of lists to become familiar with lists in Lisp.

(1 2 3 4)
(println "Hello world")
(one two three)
("one" two three)

All of the above are lists, but not all of them are valid code. Remember, only lists where the first element is a function can be considered valid expressions. So, here only the following could be valid expressions:

(println "Hello world")
(one two three)

If println and one are defined as functions.

Let's see a piece of Clojure code, to finally explain how everything works.

(defn some-function [times parameter]
"Prints a string certain number of times"
  (dotimes [x times]
    (println parameter)))

Lists in Clojure

Clojure is based around "forms" or lists. In Clojure, same as every Lisp, the way to denote a list is with parentheses, so here are some examples of lists in the last code:

(println parameter)
(dotimes [x times] (println parameter))
(defn some-function [times parameter] (dotimes [x times] (println parameter)))

Lists are one data type in Clojure and they are also the way to express code; you will learn later about all the benefits of expressing code as data. The first one is that it is really simple, anything you can do must be expressed as a list! Let's look at some other examples of executable code:

(* 1 2 3)
(+ 5 9 7)
(/ 4 5)
(- 2 3 4)
(map inc [1 2 3 4 5 6])

I encourage you to write everything into the REPL, so you get a good notion of what's happening.

Operations in Clojure

In Clojure, MOST of the executable forms have this structure:

(op parameter-1parameter-2 ….)

op is the operation to be executed followed by all the parameters it needs, let's analyze each of our previous forms in this new light:

(+ 1 2 3)

We are asking to execute the + (addition) operation with the parameters 1, 2, and 3. The expected result is 6.

Let's analyze something a bit more complicated:

(map inc [1 2 3 4 5 6])

In this, we are asking to execute the clojure.core/map function with two parameters:

  • inc is a function name, it takes a number and increments it
  • [1 2 3 4 5 6] is a collection of numbers

Map applies the inc function to each member of the passed collection and returns a new collection, what we expect is a collection containing [2 3 4 5 6 7].

Functions in Clojure

Now let's check how a function definition is essentially the same as the previous two forms:

(defn some-function [times parameter]
"Prints a string certain number of times"
  (dotimes [x times]
    (println parameter)))

The defn is the operation that we are asking for. It has several parameters, such as:

  • some-function is the name of the function that we are defining
  • [times parameter] is a collection of parameters
  • "Prints a string certain number of times" is the docstring, it is actually an optional parameter
  • (dotimes [x times] (println parameter)) is the body of the function that gets executed when you call some-function

The defn calls a function into existence. After this piece of code is executed, some-function exists in the current namespace and you can use it with the defined parameters.

The defn is actually written in Clojure and supports a few nice things. Let's now define a multi-arity function:

(defn hello
  ([] (hello "Clojure"))
  ([name] (str "Hello " name)))

Over here we are defining a function with two bodies, one of them has no arguments and the other one has one argument. It is actually pretty simple to understand what's happening.

Try changing the source in your project's core.clj file similar to the following example:

(ns getting-started.core
  (:gen-class))

(defn hello
  ([] (hello "Clojure"))
  ([name] (str "Hello " name)))

(defn -main
"I don't do a whole lot ... yet."
  [& args]
  (println "Hello, World!")
  (println (hello))
  (println (hello "Edu")))

Now run it, you'll get three different Hello outputs.

As you can see, Clojure has a very regular syntax and even if it's a little strange for newcomers, it is actually quite simple.

Here, we have used a few data types that we haven't properly introduced; in the next section we'll take a look at them.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Clojure for Java Developers
You have been reading a chapter from
Clojure for Java Developers
Published in: Feb 2016
Publisher:
ISBN-13: 9781785281501
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at R$50/month. Cancel anytime
Modal Close icon
Modal Close icon