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

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.

主站蜘蛛池模板: 杭州市| 孝感市| 江门市| 青阳县| 措美县| 武川县| 婺源县| 湘阴县| 连州市| 乐都县| 青龙| 乌海市| 外汇| 宁河县| 镇宁| 轮台县| 麦盖提县| 启东市| 富阳市| 巴林左旗| 固安县| 麦盖提县| 固原市| 峨边| 宁南县| 临澧县| 黑山县| 塔城市| 崇仁县| 四会市| 勃利县| 宾川县| 华安县| 隆德县| 盖州市| 揭西县| 芮城县| 元谋县| 文水县| 义马市| 盈江县|