Slide 1

Slide 1 text

The Design of Clojure Shantanu Kumar, Concur 1

Slide 2

Slide 2 text

Who am I? • Engineer @ Concur • Author of “Clojure High Performance Programming” • Open Source enthusiast: https://github.com/ kumarshantanu • @kumarshantanu on Twitter 2

Slide 3

Slide 3 text

Clojure • Created by Rich Hickey, 2007 • Eclipse Public License (OSS) • Website — https://clojure.org/ • Lisp • Functional • Hosted on JVM (and CLR, JS) 3

Slide 4

Slide 4 text

Big ideas • Pervasive immutability (Epochal time model) • First-class values • First-class functions • Performance • Host interoperability • First-class state management 4

Slide 5

Slide 5 text

–Stuart Halloway “Clojure feels like a general-purpose language beamed back from the near future.” 5

Slide 6

Slide 6 text

Getting started with Clojure http://leiningen.org/ https://learnxinyminutes.com/docs/clojure/ http://www.braveclojure.com/ https://www.4clojure.com/ 6

Slide 7

Slide 7 text

Interactive REPL 7

Slide 8

Slide 8 text

Data types • Number: 42, -5.67, 22/7 • Boolean: true, false • Character: \x, \y, \newline, \space • String: “foo”, “bar” (multi-line literals are supported) • Symbol: ‘do-something, ‘bizfn • Keyword: :red, :blue • Nothing: nil 8

Slide 9

Slide 9 text

Data structures • List: ‘(“Harry” 34 :male) — sequential access • Map: {:name “Harry” :age 34} • Set: #{:red :blue} • Vector: [“Harry” 34 :male] 9

Slide 10

Slide 10 text

Working with data structures 10

Slide 11

Slide 11 text

Functions 11

Slide 12

Slide 12 text

Code is data 12

Slide 13

Slide 13 text

Persistent Data-structures • Immutable (updates return new instances) • Structure sharing (Hash array mapped trie) • 32-way branching for O(<7) performance • Sustained performance across updates • Reference: “Ideal Hash Trees” (Phil Bagwell) 13

Slide 14

Slide 14 text

Working with collections 14

Slide 15

Slide 15 text

I want a loop! No automatic tail-call optimization 15

Slide 16

Slide 16 text

–Alan J Perlis “A language that doesn't affect the way you think about programming, is not worth knowing.” 16

Slide 17

Slide 17 text

Records 17

Slide 18

Slide 18 text

Records • Class semantics (memory layout) • Behave as maps • Immutable in the same way as maps 18

Slide 19

Slide 19 text

Protocols 19

Slide 20

Slide 20 text

Protocols • Behaviour contract • Polymorphism • Dispatch on target object type • Used as implementation details 20

Slide 21

Slide 21 text

No structural inheritance! But, there are value hierarchies Image source: https://xspblog.files.wordpress.com/2008/06/owl_yarly.jpg 21

Slide 22

Slide 22 text

Macros 22

Slide 23

Slide 23 text

Sample output 23

Slide 24

Slide 24 text

Macros • Compile-time code manipulation • Because code is data 24

Slide 25

Slide 25 text

Java interop 25

Slide 26

Slide 26 text

Concurrent execution • CPU-bound and I/O-bound thread pools • `future`: Asynchronous one-time access • `pmap`: Concurrent CPU-bound processing • `promise/deliver`: Single producer-consumer • Java’s concurrent collections considered idiomatic 26

Slide 27

Slide 27 text

Concurrency and state • Atoms — uncoordinated compare and swap • Agents — asynchronous sequential operations • Refs — Software Transactional Memory • Dynamic vars — Thread-local access 27

Slide 28

Slide 28 text

Thank you! @kumarshantanu 28