Slide 1

Slide 1 text

Intro to Clojure Balint Erdi twitter: @baaz http://github.com/balinterdi http://codigoergosum.com => http://balinterdi.com

Slide 2

Slide 2 text

• Elevator pitch • Rationale • 10-minute crash course • REPL playtime

Slide 3

Slide 3 text

Clojure is a dynamic programming language that targets the JVM and is a dialect of Lisp with a strong focus on concurrency.

Slide 4

Slide 4 text

Clojure is a dynamic programming language that targets the JVM and is a dialect of Lisp with a strong focus on concurrency.

Slide 5

Slide 5 text

Rich Hickey http://www.tbray.org/ongoing/When/200x/2008/09/25/-big/R0010774.jpg.html

Slide 6

Slide 6 text

Rich Hickey (RH): Why did I write yet another programming language? Basically because I wanted: • A Lisp • for Functional Programming • symbiotic with an established platform • designed for concurrency • ... and couldn’t find one

Slide 7

Slide 7 text

History • Rich Hickey sends an email to Common Lisp friends in 2007 • After working on it for 2.5 years • Gets picked up, now at 1.4 (Apr 2012) • Targets other platforms, too (Javascript, CLR, Python)

Slide 8

Slide 8 text

Rationale (http://clojure.org/rationale) • Lisp is a good thing • Functional programming is a good thing • Languages and platforms • Object Orientation is overrated • Polymorphism is a good thing • Concurrency and the multi-core future

Slide 9

Slide 9 text

10-minute crash course

Slide 10

Slide 10 text

Atomic types • Number (3, 3.14, 1e6) • Character (\a, \b, \c) • String (“foo”) • Symbol (‘a, ‘b, ‘c) • Keywords • for very fast equality test (:cool, :bad) • Regex (#”clo[s|j]ure”) • Boolean (true, false) • Only nil and false are falsey

Slide 11

Slide 11 text

Collections • A handful (list, vector, map, set) • Sequential, counted, associative • A large number of functions that operate on them • All of them are immutable • Extensible: deftype, defrecord, reify

Slide 12

Slide 12 text

Vectors • Like an array • Fast random access by index • Sequential, counted and associative (by index) • [:a :b :a :c :c :d] • or (vector :a :b :a :c :c :d)

Slide 13

Slide 13 text

Maps • Hash, dictionary, “associative” array • Fast access by key • Counted and associative, not sequential • {“bob” 35, “alice” 28, “charlie” 19} • (hash-map “bob” 35, “alice” 28, “charlie” 19)

Slide 14

Slide 14 text

Sets • Distinct collection of elements • Fast lookup for containment • Counted, not sequential, not associative • #{“alice” “bob” “charlie”} • (set '("alice" "bob" "charlie"))

Slide 15

Slide 15 text

Lists • First and rest (car and cdr in Lisp) • Counted and sequential, not associative • Fast access to first and rest • ‘(:apple :banana :cherry) • (list :apple :banana :cherry)

Slide 16

Slide 16 text

Functions • First class citizens (no surprise there) • Defined by (defn fn-name [...] body) • Anonymous functions • (fn [...] body) • (map #(+ % 2) collection)

Slide 17

Slide 17 text

Laziness • Only gets evaluated when the result is needed • Makes for elegant, readable programming • Enables working with infinite sequences • Most functions are lazy (map, filter, for, take, drop, partition, etc.) • Use eager functions if you need side effects

Slide 18

Slide 18 text

Prefix notation • Function is always the first argument • Needs some getting used to • Makes variable-length arg functions simple • e.g (+ 1 -7 3 15) • Operator precedence is done away with

Slide 19

Slide 19 text

Homoiconicity • Clojure programs are written in Clojure data structures • Makes macros “easy”

Slide 20

Slide 20 text

Java interop • The whole Java ecosystem at your fingertips • A few succinct forms to reach them (. .. new doto) • Enterprise-ready :)

Slide 21

Slide 21 text

Polymorphism • Multimethods • dispatch on type • Protocols • if you need more flexibility

Slide 22

Slide 22 text

Namespaces • Encapsulation • See packages in other languages

Slide 23

Slide 23 text

Concurrency • Data structures are immutable • Mechanisms • Refs/Transactions (STM) • Atoms (thread isolation) • Agents (independent/asynchronous change)

Slide 24

Slide 24 text

Parallelism • pmap • Reducers (1.5) • e.g ideal for map-reduce tasks

Slide 25

Slide 25 text

REPL session

Slide 26

Slide 26 text

Thank you. Questions? @baaz