intro clojure
TRANSCRIPT
?
Clojure is a dinamically-typed functional
language (LISP dialect) running as an hosted
language on the JVM (and JS) with a focus on
interactivity, immutability, no side effects,
laziness and concurrency
©2007 Rich Hickey
LISP dialect
lang=knparens
var people = [{name: "Matteo", age: 33},
{name: "Paolo", age: 25},
{name: "Luca", age: 55}];
people.filter(function isYoung(p) {
return p.age < 34;
}).map(function(p) {
return p.age;
}).reduce(function(a,b) {
return a + b;
},0);
13
(def people [{:name "Matteo" :age 33}
{:name "Paolo" :age 25}
{:name "Luca" :age 55}])
(defn young? [p] (< (:age p) 34))
(apply + (map :age (filter young? people)))
12
=12
=13
SIMPLE
SIMPLE MADE EASYhttps://www.infoq.com/presentations/Simple-Made-Easy
SIMPLE ≠ EASY
DIFFICULT → EASY
SIMPLE | COMPLEX
SIMPLE
if (x + y > 0) x else y
AST = Abstract Syntax Tree
if
cond elsethen
if
(x + y > 0) yx
if
> yx
x + y 0
if
> yx
+ 0
x y
if
> yx
+ 0
x y
if (x + y > 0) x else y
if
> yx
+ 0
x y
UNLESS
ANYONE?
unless (x + y > 0) y else x
if
> yx
+ 0
x y
unless
> xy
+ 0
x y
if
> yx
+ 0
x y
every tree is a list ()
first element is the root
other elements are children
recur
if
> yx
+ 0
x y
(if …)
if
> yx
+ 0
x y
(if (> …) x y)
if
> yx
+ 0
x y
(if (> (+ …) 0) x y)
if
> yx
+ 0
x y
(if (> (+ x y) 0) x y)
CODE AS DATA
HOMOICONICITY
immutability, no side effects,
laziness, concurrency etc.
INTERACTIVITY
REPLRead Evaluate Print Loop
DEMO