Clojure (pronounced closure) is a modern Lisp dialect, a general-purpose programming language with dynamic typing and encouraging functional programming. The author of the language Rich Hickey first presented his creation in 2007, since then the language has matured and reached version 1.7, released June 30, 2015.
One of the main concepts of the language is to work on any existing platform. Rich Hickey decided not to write his runtime environment, his garbage collector, etc. - it would require more labor, it is easier to use ready-made platforms, such as JVM, .NET, JavaScript. To date, the two most actively developed areas are: Clojure on the JVM (it was he who reached version 1.7 not so long ago) and ClojureSrcipt is a subset of the language that is compiled into JavaScript to work in the browser and on Node.js. The .NET version is not actively developing and lags behind the JVM implementation. I searched the Internet and found a few more dead Clojure implementations:
Go ,
PHP ,
Python, and
Perl .
')
In this article I want to talk about Clojure, showing examples in comparison with PHP, based on a series of screencasts in English
From PHP to Clojure .
PHP and Clojure are two completely different languages. When you see Clojure for the first time, you might think that this is some kind of drug JSON. In fact, Clojure is a very powerful and elegant language.
Comparing with PHP, many aspects of the language have direct analogues. Others will become clear if you look at them from the right angle.
Anonymous functions, Closure & Clojure
I will begin with a small digression to understand the terminological confusion. Anonymous functions and closures appeared in PHP 5.3, and if you look closely at the
documentation , you will see that anonymous functions in PHP are implemented using the Closure class. The word “Closure” in turn translates as “closure” and is very similar in spelling to the name of the Clojure language (note the letter
j in the middle). To prevent us from getting confused further, we will use the term “Anonymous function” for anonymous functions and “Closure” for the effect of lexical visibility of a variable — this is when the
use (...) construction is used in PHP to describe an anonymous function. Clojure, respectively, also has anonymous functions and closures.
Namespaces
There is a lot in common between PHP and Clojure: namespaces are made up of parts that correspond to the physical location of the files. There are only two differences: in Clojure, the delimiter is a period and the names of namespaces are usually called with a small letter.
namespace Foo\Bar\Baz;
(ns foo.bar.baz)
Clojure
,
(count foo)
,
count($foo)
:

! : xml- ,
if,
for :

:

, , :

Clojure! ( , Clojure, ):

, , , - -. Clojure , .
, , , :

, , .. IDE .
PHP, . !
, :

if, switch :

, , , :

:

Clojure, , ! :
Value,
Symbol List ( 100% , ).
Value () — , , :
2
"Hello, World"
#"\d+"
Symbol () — , , .. - ( Value):
def
map
db
my-symbol
List () — , (value), (symbol) (list). .. : , .
(some-symbol "Some Value" ("nested list with value" and-symbol))
Clojure!
, , ( if/else), ?
Clojure, , .
, if , , if, , . , , , :

. , , . , . , , , , .
, , .
PHP:

, ,
+, :
(+ 3 4)
— .
+ Clojure , .
:
4 + 3 / 2 = ?, , :
(/ (+ 4 3) 2)
:
(+ 4 (/ 3 2))
, .
Clojure . ,
, .. Clojure . , .
, . , :

( PHP
is):

, , :

« » — Clojure, , :

PHP, Clojure . , PHP, : Integer BitInt. , . , BigInt, M . , N .

:

Clojure ! PHP:
$x = 4 / 3;
$y = $x * 3;
Clojure:
(/ 4 3)
(* 4/3 3)
Clojure — Java . , .. - , ,
\n . , :

PHP, :

Clojure
Character — «», , . ,
\n — ,
n.
Characters, , ..:
\newline,
\tab,
\backspace.
unicode , ,
\u263a.
, :
\o003 — Ctrl+C.

# :
#"\d+". Java, Java-:
Nil nil ( Null PHP).
Bool true false. , PHP,
nil false . , 0 "" ( ) Clojure
, PHP
:

Keyword
Clojure Keyword, PHP (
if,
for ..). Keywords . — . - .
:pi —
:pi.

, ? PHP
define . , define, , - , .
, PHP
str_pad, . $pad_type : STR_PAD_RIGHT, STR_PAD_LEFT, STR_PAD_BOTH. 0, 1 2 . , 265, 1337 9000 — .
Clojure keywords
:str-pad-right,
:str-pad-left,
:str-pad-both — - , , !
, :
{:first-name "Irma", :last-name "Gerd"}
.
—
Clojure, -, :
«- Clojure» «- Clojure. 2».
Clojure PHP,
PHP vs Clojure.
FPCONF 15 2015 , - Clojure ClojureScript.