- Clojure for Java Developers
- Eduardo Díaz
- 756字
- 2021-07-16 09:49:47
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 callsome-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.
- JSP網絡編程(學習筆記)
- Vue.js設計與實現
- Vue.js 3.0源碼解析(微課視頻版)
- PLC編程及應用實戰
- Java編程技術與項目實戰(第2版)
- 硅谷Python工程師面試指南:數據結構、算法與系統設計
- 編程菜鳥學Python數據分析
- Mastering Linux Security and Hardening
- Julia 1.0 Programming Complete Reference Guide
- Building Dynamics CRM 2015 Dashboards with Power BI
- Machine Learning for Developers
- Java7程序設計入門經典
- 大學計算機應用基礎(Windows 7+Office 2010)(IC3)
- Java程序設計
- 大象:Thinking in UML(第二版)