Intro Clojure

55
(intro "Clojure") Matteo Barbieri [email protected] @matteobarbieri

Transcript of Intro Clojure

Page 1: Intro Clojure

(intro "Clojure")

Matteo [email protected]

@matteobarbieri

Page 2: Intro Clojure

?

Page 3: Intro Clojure

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

Page 4: Intro Clojure

©2007 Rich Hickey

Page 5: Intro Clojure

LISP dialect

Page 6: Intro Clojure
Page 7: Intro Clojure

lang=knparens

Page 8: Intro Clojure
Page 9: Intro Clojure

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);

Page 10: Intro Clojure

13

Page 11: Intro Clojure
Page 12: Intro Clojure

(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)))

Page 13: Intro Clojure

12

Page 14: Intro Clojure

=12

=13

Page 15: Intro Clojure
Page 16: Intro Clojure
Page 17: Intro Clojure

SIMPLE

Page 18: Intro Clojure

SIMPLE MADE EASYhttps://www.infoq.com/presentations/Simple-Made-Easy

Page 19: Intro Clojure

SIMPLE ≠ EASY

Page 20: Intro Clojure

DIFFICULT → EASY

Page 21: Intro Clojure
Page 22: Intro Clojure
Page 23: Intro Clojure

SIMPLE | COMPLEX

Page 24: Intro Clojure
Page 25: Intro Clojure
Page 26: Intro Clojure

SIMPLE

Page 27: Intro Clojure
Page 28: Intro Clojure
Page 29: Intro Clojure

if (x + y > 0) x else y

Page 30: Intro Clojure

AST = Abstract Syntax Tree

Page 31: Intro Clojure

if

cond elsethen

Page 32: Intro Clojure

if

(x + y > 0) yx

Page 33: Intro Clojure

if

> yx

x + y 0

Page 34: Intro Clojure

if

> yx

+ 0

x y

Page 35: Intro Clojure

if

> yx

+ 0

x y

if (x + y > 0) x else y

Page 36: Intro Clojure

if

> yx

+ 0

x y

Page 37: Intro Clojure
Page 38: Intro Clojure

UNLESS

ANYONE?

Page 39: Intro Clojure

unless (x + y > 0) y else x

Page 40: Intro Clojure

if

> yx

+ 0

x y

Page 41: Intro Clojure

unless

> xy

+ 0

x y

Page 42: Intro Clojure

if

> yx

+ 0

x y

Page 43: Intro Clojure

every tree is a list ()

first element is the root

other elements are children

recur

Page 44: Intro Clojure

if

> yx

+ 0

x y

(if …)

Page 45: Intro Clojure

if

> yx

+ 0

x y

(if (> …) x y)

Page 46: Intro Clojure

if

> yx

+ 0

x y

(if (> (+ …) 0) x y)

Page 47: Intro Clojure

if

> yx

+ 0

x y

(if (> (+ x y) 0) x y)

Page 48: Intro Clojure
Page 49: Intro Clojure

CODE AS DATA

Page 50: Intro Clojure

HOMOICONICITY

Page 51: Intro Clojure

immutability, no side effects,

laziness, concurrency etc.

Page 52: Intro Clojure

INTERACTIVITY

Page 53: Intro Clojure

REPLRead Evaluate Print Loop

Page 54: Intro Clojure

DEMO

Page 55: Intro Clojure