官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 汉川市| 本溪市| 玉林市| 虞城县| 婺源县| 封开县| 壤塘县| 西乡县| 泸水县| 崇左市| 上思县| 金湖县| 黑龙江省| 平遥县| 淳化县| 襄城县| 房产| 乐昌市| 肃北| 伊宁市| 汝城县| 基隆市| 平湖市| 澄江县| 临西县| 苏州市| 原阳县| 普定县| 广宗县| 双辽市| 尉氏县| 安仁县| 邵武市| 晋宁县| 巴东县| 肇庆市| 临武县| 义马市| 东台市| 安新县| 安龙县|