Slide 1

Slide 1 text

Clojure Data Structures Part One Wednesday, June 19, 13

Slide 2

Slide 2 text

Selling points • Dynamic programming language • Functional programming (Lisp dialect) • Pragmatic: runs on the JVM • Pragmatic: easy interop. with Java libs • Strong concurrency support Wednesday, June 19, 13

Slide 3

Slide 3 text

Selling points • Dynamic programming language • Functional programming (Lisp dialect) • Pragmatic: runs on the JVM • Pragmatic: easy interop. with Java libs • Strong concurrency support Wednesday, June 19, 13

Slide 4

Slide 4 text

In Clojure • All data structures are immutable • Functions return copies of the original • Copies share structure and are very performant • “persistent” data structures Wednesday, June 19, 13

Slide 5

Slide 5 text

Collection types Wednesday, June 19, 13

Slide 6

Slide 6 text

List • ‘(:alice :bob :carol) • The classic from Lisp • Fast access to • first item • rest of the list Wednesday, June 19, 13

Slide 7

Slide 7 text

Vector • [:alice :bob :carol] • Fast access to • any item in the list by its index • associative Wednesday, June 19, 13

Slide 8

Slide 8 text

Map • {:alice 24 :bob 34} • Fast access to • any value by its key • associative Wednesday, June 19, 13

Slide 9

Slide 9 text

Set • #{:alice :bob} • Fast membership check Wednesday, June 19, 13

Slide 10

Slide 10 text

The sequence abstraction 1. • “It is better to have 1 data structure and 100 functions than 10 data types and 10 functions” • Clojure adheres to the Lisp philosophy: • sequence abstraction • all collection types are “sequable” Wednesday, June 19, 13

Slide 11

Slide 11 text

The sequence abstraction 2. • all sequence functions can be used on all basic data structures • first, rest, map, filter, some, reduce • +30 more • some collection-specific functions • e.g subvec for vectors Wednesday, June 19, 13

Slide 12

Slide 12 text

REPL demo • “Adding” an item • “Deleting” an item • Getting an arbitrary item (association) • Getting an item more succinctly Wednesday, June 19, 13

Slide 13

Slide 13 text

Do you know Javascript? Wednesday, June 19, 13

Slide 14

Slide 14 text

Do you really know Javascript? Wednesday, June 19, 13

Slide 15

Slide 15 text

Do you really know Javascript? • x = [1,2,3] Wednesday, June 19, 13

Slide 16

Slide 16 text

Do you really know Javascript? • x = [1,2,3] • y = x.push(4) Wednesday, June 19, 13

Slide 17

Slide 17 text

Do you really know Javascript? • x = [1,2,3] • y = x.push(4) • x ? Wednesday, June 19, 13

Slide 18

Slide 18 text

Do you really know Javascript? • x = [1,2,3] • y = x.push(4) • x ? • [1,2,3,4] Wednesday, June 19, 13

Slide 19

Slide 19 text

Do you really know Javascript? • x = [1,2,3] • y = x.push(4) • x ? • [1,2,3,4] • y ? Wednesday, June 19, 13

Slide 20

Slide 20 text

Do you really know Javascript? • x = [1,2,3] • y = x.push(4) • x ? • [1,2,3,4] • y ? • 4 Wednesday, June 19, 13

Slide 21

Slide 21 text

Do you really know Javascript? Wednesday, June 19, 13

Slide 22

Slide 22 text

Do you really know Javascript? • x = [1,2,3] Wednesday, June 19, 13

Slide 23

Slide 23 text

Do you really know Javascript? • x = [1,2,3] • y = x.concat([4]) Wednesday, June 19, 13

Slide 24

Slide 24 text

Do you really know Javascript? • x = [1,2,3] • y = x.concat([4]) • x ? Wednesday, June 19, 13

Slide 25

Slide 25 text

Do you really know Javascript? • x = [1,2,3] • y = x.concat([4]) • x ? • [1,2,3] - did not change Wednesday, June 19, 13

Slide 26

Slide 26 text

Do you really know Javascript? • x = [1,2,3] • y = x.concat([4]) • x ? • [1,2,3] - did not change • y ? Wednesday, June 19, 13

Slide 27

Slide 27 text

Do you really know Javascript? • x = [1,2,3] • y = x.concat([4]) • x ? • [1,2,3] - did not change • y ? • [1,2,3,4] Wednesday, June 19, 13

Slide 28

Slide 28 text

Do you really know Javascript? Wednesday, June 19, 13

Slide 29

Slide 29 text

Do you really know Javascript? • x = [1,2] Wednesday, June 19, 13

Slide 30

Slide 30 text

Do you really know Javascript? • x = [1,2] • y = [3,4] Wednesday, June 19, 13

Slide 31

Slide 31 text

Do you really know Javascript? • x = [1,2] • y = [3,4] • z = x + y Wednesday, June 19, 13

Slide 32

Slide 32 text

Do you really know Javascript? • x = [1,2] • y = [3,4] • z = x + y • z ? Wednesday, June 19, 13

Slide 33

Slide 33 text

Do you really know Javascript? • x = [1,2] • y = [3,4] • z = x + y • z ? • “1,23,4” Wednesday, June 19, 13

Slide 34

Slide 34 text

Do you really know Javascript? • x = [1,2] • y = [3,4] • z = x + y • z ? • “1,23,4” • WAT? Wednesday, June 19, 13

Slide 35

Slide 35 text

Takeaways • All data structures in Clojure are immutable • A huge win for concurrency • Functions return copies of the original • The copies are very efficient ones • Collection types implement the sequable interface => “Few data types, lots of functions operating on them” Wednesday, June 19, 13

Slide 36

Slide 36 text

Further possible topics • Higher-level collection functions • Lazy seqs • Persistent data structures • Homoiconicity Wednesday, June 19, 13

Slide 37

Slide 37 text

Thank you. @baaz balinterdi.com Wednesday, June 19, 13