Intro Clojure
-
Upload
matteo-barbieri -
Category
Software
-
view
339 -
download
1
Transcript of Intro Clojure
![Page 2: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/2.jpg)
?
![Page 3: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/3.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/4.jpg)
©2007 Rich Hickey
![Page 5: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/5.jpg)
LISP dialect
![Page 6: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/6.jpg)
![Page 7: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/7.jpg)
lang=knparens
![Page 8: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/8.jpg)
![Page 9: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/10.jpg)
13
![Page 11: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/11.jpg)
![Page 12: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/12.jpg)
(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](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/13.jpg)
12
![Page 14: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/14.jpg)
=12
=13
![Page 15: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/15.jpg)
![Page 16: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/16.jpg)
![Page 17: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/17.jpg)
SIMPLE
![Page 18: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/18.jpg)
SIMPLE MADE EASYhttps://www.infoq.com/presentations/Simple-Made-Easy
![Page 19: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/19.jpg)
SIMPLE ≠ EASY
![Page 20: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/20.jpg)
DIFFICULT → EASY
![Page 21: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/21.jpg)
![Page 22: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/22.jpg)
![Page 23: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/23.jpg)
SIMPLE | COMPLEX
![Page 24: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/24.jpg)
![Page 25: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/25.jpg)
![Page 26: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/26.jpg)
SIMPLE
![Page 27: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/27.jpg)
![Page 28: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/28.jpg)
![Page 29: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/29.jpg)
if (x + y > 0) x else y
![Page 30: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/30.jpg)
AST = Abstract Syntax Tree
![Page 31: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/31.jpg)
if
cond elsethen
![Page 32: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/32.jpg)
if
(x + y > 0) yx
![Page 33: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/33.jpg)
if
> yx
x + y 0
![Page 34: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/34.jpg)
if
> yx
+ 0
x y
![Page 35: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/35.jpg)
if
> yx
+ 0
x y
if (x + y > 0) x else y
![Page 36: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/36.jpg)
if
> yx
+ 0
x y
![Page 37: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/37.jpg)
![Page 38: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/38.jpg)
UNLESS
ANYONE?
![Page 39: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/39.jpg)
unless (x + y > 0) y else x
![Page 40: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/40.jpg)
if
> yx
+ 0
x y
![Page 41: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/41.jpg)
unless
> xy
+ 0
x y
![Page 42: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/42.jpg)
if
> yx
+ 0
x y
![Page 43: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/43.jpg)
every tree is a list ()
first element is the root
other elements are children
recur
![Page 44: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/44.jpg)
if
> yx
+ 0
x y
(if …)
![Page 45: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/45.jpg)
if
> yx
+ 0
x y
(if (> …) x y)
![Page 46: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/46.jpg)
if
> yx
+ 0
x y
(if (> (+ …) 0) x y)
![Page 47: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/47.jpg)
if
> yx
+ 0
x y
(if (> (+ x y) 0) x y)
![Page 48: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/48.jpg)
![Page 49: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/49.jpg)
CODE AS DATA
![Page 50: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/50.jpg)
HOMOICONICITY
![Page 51: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/51.jpg)
immutability, no side effects,
laziness, concurrency etc.
![Page 52: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/52.jpg)
INTERACTIVITY
![Page 53: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/53.jpg)
REPLRead Evaluate Print Loop
![Page 54: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/54.jpg)
DEMO
![Page 55: Intro Clojure](https://reader033.fdocuments.net/reader033/viewer/2022050806/5a6d40c97f8b9af2418b5141/html5/thumbnails/55.jpg)