Slide 1

Slide 1 text

Is this talk about Clojure? Howard M. Lewis Ship — [email protected] — @hlship

Slide 2

Slide 2 text

a: Yes, because Howard loves to talk about Clojure.

Slide 3

Slide 3 text

q: Does Howard know what he's talking about?

Slide 4

Slide 4 text

a: Hopefully. He's been coding Clojure full time for several years. And he's done some interesting stuff before that. He's also written some well-favored open source libraries for Clojure.

Slide 5

Slide 5 text

syntax

Slide 6

Slide 6 text

q: Is "Zanzibar" a form?

Slide 7

Slide 7 text

a: Yes, because "Zanzibar" is a string. In fact, it is an instance of java.lang.String. Clojure uses the underlying host's facilities where possible.

Slide 8

Slide 8 text

q: Is 5431 a form?

Slide 9

Slide 9 text

a: Yes, because 5431 is a literal number. In fact, it is an instance of java.lang.Long.

Slide 10

Slide 10 text

q: Is true a form?

Slide 11

Slide 11 text

a: Yes, because true and false are booleans. In fact, it is an instance of java.lang.Boolean.

Slide 12

Slide 12 text

q: Is null a form?

Slide 13

Slide 13 text

a: No. I think you mean nil. Java's null is Clojure's nil. It's about heritage.

Slide 14

Slide 14 text

q: Is \J a form?

Slide 15

Slide 15 text

a: Yes, because \J is a character. It is an instance of java.lang.Char. \newline, \tab, and a few others work as well. There's also Unicode character support.

Slide 16

Slide 16 text

q: Is john-brunner a form?

Slide 17

Slide 17 text

a: Yes, because john-brunner is a symbol. Clojure symbols start with a letter (or certain punctuation) and can contain most other characters.

Slide 18

Slide 18 text

q: Is :science-fiction a symbol?

Slide 19

Slide 19 text

a: No. :science-fiction is a keyword. Keywords look like symbols, but start with a colon. Symbols or keywords can contain colons, but that is rarely used.

Slide 20

Slide 20 text

q: Is clojure.core/reduce a form?

Slide 21

Slide 21 text

a: Yes, because symbols can be namespace qualified. Symbols can contain a namespace, a slash, and a simple symbol. Watch out: simple symbols can start with a slash. clojure.core// is also a symbol.

Slide 22

Slide 22 text

q: Is ::donald-hogan a keyword?

Slide 23

Slide 23 text

a: Yes, because ::donald-hogan starts with a colon. Two colons is a shortcut for qualifying the keyword into the current namespace.

Slide 24

Slide 24 text

q: Are those all of the Clojure forms?

Slide 25

Slide 25 text

a: No. There's a couple of more literal types, such as regular expressions and ratios, and some more number types. There's also collection forms.

Slide 26

Slide 26 text

collections

Slide 27

Slide 27 text

q: Is () a form?

Slide 28

Slide 28 text

a: Yes, because () is the empty list. In most cases, nil and () are treated identically but they aren't the same thing.

Slide 29

Slide 29 text

q: Is (1 2 3) a form?

Slide 30

Slide 30 text

a: Yes, because (1 2 3) is a sequence of forms surrounded by open and close parenthesis.

Slide 31

Slide 31 text

q: Is ("Howard" ("Lewis" "Ship")) a list?

Slide 32

Slide 32 text

a: Yes, because lists are forms that can be contained inside other lists.

Slide 33

Slide 33 text

q: ("Stand on Zanzibar" "John Brunner" 1968 "ISBN 0-09-919110-5" :science-fiction) Is this a list?

Slide 34

Slide 34 text

a: Yes. Lists can contain a mix of different types of forms. Forms are separated by whitespace, including blank lines. The comma character is also whitespace in Clojure.

Slide 35

Slide 35 text

q: Is [1 2 3] a list?

Slide 36

Slide 36 text

a: No. [1 2 3] is a vector. Vectors are similar to lists, but support indexed access to the elements. We'll come back to that later.

Slide 37

Slide 37 text

q: Is {:genre} a form?

Slide 38

Slide 38 text

a: No. Curly braces denote a map form, but map forms must have an matching number of keys and values.

Slide 39

Slide 39 text

q: Is {:title "Stand on Zanzibar", :genre :science-fiction} a map?

Slide 40

Slide 40 text

a: Yes, because there is a value for every key. Keys and values can be any form, even collections. Remember: commas are whitespace. Some people like to put commas after each value.

Slide 41

Slide 41 text

q: Is #{:science-fiction :romance :western :fantasy} a form?

Slide 42

Slide 42 text

a: Yes. This is a set. Like the other collections, the values inside the set can be any other form, even collections or nil. Unlike lists or vectors, the values inside a set are un-ordered.

Slide 43

Slide 43 text

q: Is ; TODO: Fix this later a form?

Slide 44

Slide 44 text

a: No. It is a comment. Comments start with a ; and continue to the end of the line. By convention, a single ; is a comment for the same line. A ;; denotes a block within a definition. A ;;; denotes a section within a source file.

Slide 45

Slide 45 text

evaluation

Slide 46

Slide 46 text

q: What does 37 evaluate to?

Slide 47

Slide 47 text

a: 37. Literal values evaluate to themselves. user=> 37 ➠ 37

Slide 48

Slide 48 text

user=> 37 ➠ 37 Namespace prompt Evaluation output Input Expression

Slide 49

Slide 49 text

q: What does :science-fiction evaluate to?

Slide 50

Slide 50 text

a: It evaluates to :science-fiction. Keywords always evaluate to themselves. user=> :science-fiction ➠ :science-fiction

Slide 51

Slide 51 text

q: What does [1 2 3] evaluate to?

Slide 52

Slide 52 text

a: It evaluates to [1 2 3]. Vectors, maps, and sets evaluate to themselves.

Slide 53

Slide 53 text

q: What does [space 1999] evaluate to?

Slide 54

Slide 54 text

a: Unknown, or an error. space is a symbol that has not been defined.

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

q: What does = evaluate to?

Slide 57

Slide 57 text

a: A function, from the clojure.core namespace. = is a symbol, and symbols evaluate to something else. Everything in the clojure.core namespace is automatically available by default.

Slide 58

Slide 58 text

user=> = ➠ #object[clojure.core$_EQ_ …] How Clojure outputs Java objects

Slide 59

Slide 59 text

q: What does () evaluate to?

Slide 60

Slide 60 text

a: () evaluates to itself, (). As we'll see, this is an exception to the general rule.

Slide 61

Slide 61 text

q: What does this evaluate to? (= :science-fiction :fantasy)

Slide 62

Slide 62 text

a: It evaluates to false. A list form evaluates as a function call. The = function compares two or more values for equality.

Slide 63

Slide 63 text

q: What does (=) evaluate to?

Slide 64

Slide 64 text

a: An error. Each function has an arity, or number of parameters it expects. It is an error to invoke a function with the wrong number of arguments. user=> (=) clojure.lang.ArityException: Wrong number of args (0) passed to: core/=

Slide 65

Slide 65 text

q: What does (= 42 42.) evaluate to?

Slide 66

Slide 66 text

a: false. The = function does not coerce its arguments, and the java.lang.Long 42 does not equal the java.lang.Double 42. You can use == instead; it only works for numbers and does some coercion.

Slide 67

Slide 67 text

q: What does (1 2 3) evaluate to?

Slide 68

Slide 68 text

a: An error. The first form inside a list must be a function. user=> (1 2 3) java.lang.ClassCastException: java.lang.Long cannot be cast to clojure.lang.IFn

Slide 69

Slide 69 text

q: What does '(flip flop) evaluate to?

Slide 70

Slide 70 text

a: It evaluates to (flip flop). The leading quote prevents evaluation of the list. '(flip flop) ➠ (flip flop) The symbols are not evaluated and do not have to be previously defined.

Slide 71

Slide 71 text

q: What does String evaluate to?

Slide 72

Slide 72 text

a: The Java Class java.lang.String. The classes in the java.lang package are automatically available. Other classes can be imported, or fully qualified (e.g., java.util.ArrayList).

Slide 73

Slide 73 text

q: What does (println "Outstanding!") evaluate to?

Slide 74

Slide 74 text

a: It evaluates to nil. More importantly, it has a side effect. user=> (println "Outstanding!") Outstanding! ➠ nil

Slide 75

Slide 75 text

q: What does this evaluate to? (def book {:title "Stand on Zanzibar" :author "John Brunner"})

Slide 76

Slide 76 text

a: It evaluates to a Var, a holder of references to other things. The Var prints itself as #'user/book. A side effect is that the current namespace, user, is modified: a new Var is created and added to the namespace.

Slide 77

Slide 77 text

user=> (def book {:title "Stand on Zanzibar" :author "John Brunner"}) ➠ #'user/book

Slide 78

Slide 78 text

clojure.core = def defn String … user book class java.lang.String {:title "Stand on Zanzibar" :author "John Brunner"} a function a function a function

Slide 79

Slide 79 text

q: What does book evaluate to?

Slide 80

Slide 80 text

a: It evaluates to: {:title "Stand on Zanzibar" :author "John Brunner"} This is the value stored in the Var identified by the book symbol.

Slide 81

Slide 81 text

functions

Slide 82

Slide 82 text

q: What does this evaluate to? (defn add-one [x] (+ 1 x))

Slide 83

Slide 83 text

a: It evaluates to the Var #'user/add-one. It also adds the symbol mapping to the user namespace.

Slide 84

Slide 84 text

(defn add-one [x] (+ 1 x)) New Symbol Function Body Parameter symbols

Slide 85

Slide 85 text

clojure.core = def defn String … user book add-one class java.lang.String {:title "Stand on Zanzibar" :author "John Brunner"} a function a function a function a function

Slide 86

Slide 86 text

q: What is the purpose of this string? (defn add-one "Returns the value of x incremented by one." [x] (+ 1 x))

Slide 87

Slide 87 text

a: It adds :doc metadata to the Var. The clojure.repl/doc function will print it out.

Slide 88

Slide 88 text

user=> (doc add-one) ------------------------- user/add-one ([x]) Returns the value of x incremented by one. ➠ nil

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

q: What does this evaluate to? (let [mystery :science-theater] mystery)

Slide 91

Slide 91 text

a: It evaluates to :science-theater. The let macro is a way to define local symbols. mystery is a local symbol, and becomes the evaluation of the let.

Slide 92

Slide 92 text

q: What does this evaluate to? (let [mystery (fn [x] (* 2 x))] (mystery 3000))

Slide 93

Slide 93 text

a: It evaluates to 6000. mystery is a local symbol for an anonymous function. Passing 3000 to mystery returns 6000.

Slide 94

Slide 94 text

q: What does this evaluate to? (let [mystery #(str "* " % " *")] (mystery "YOW"))

Slide 95

Slide 95 text

a: It evaluates to "* YOW *". str is a function that assembles a string. #() is an even more terse way to write an anonymous function. The % symbol is the parameter.

Slide 96

Slide 96 text

q: Is #(str "* " % " *") a form?

Slide 97

Slide 97 text

a: No, it is a reader macro. The reader is the part of Clojure that parses a stream of characters into forms. The #(…) sequence is converted into a (fn [x] …) form before being evaluated.

Slide 98

Slide 98 text

q: What are we doing here? (defn inc-by ([x] (inc-by x 1)) ([x delta] (+ x delta)))

Slide 99

Slide 99 text

a: We're defining a function with two arities. user=> (inc-by 10) ➠ 11 user=> (inc-by 10 5) ➠ 15

Slide 100

Slide 100 text

q: Are Clojure functions pure?

Slide 101

Slide 101 text

a: It depends. Pure - mathematically pure - functions will always return the same value for the same parameters. Generally, Clojure functions are pure.

Slide 102

Slide 102 text

• A function is pure if it only calls pure functions. • A function is pure if it performs no side-effects. • A pure function must not reference mutable state. • Input or output of any kind is impure: • Reading from the keyboard • Reading from a file, socket, etc. • System.currentTimeMillis(), etc. • Randomness is impure.

Slide 103

Slide 103 text

q: Why do we care about purity?

Slide 104

Slide 104 text

a: Pure functions are easier to reason about. They tend to be simpler. You don’t have to think about anything outside the function’s body and parameters. You can test them easily, and experiment with them using the REPL. Pure functions compose better than impure functions.

Slide 105

Slide 105 text

a: A pure function is independent from time. With multiple threads, it does not matter what threads are doing which calculations. Computing the result of a pure function can be deferred until the value is needed. The result can be cached, using the parameters as a key. This is called memoization.

Slide 106

Slide 106 text

q: Are Java methods pure?

Slide 107

Slide 107 text

a: It depends. Java objects are often mutable. They contain fields whose value can be updated at any time. Mutability works against purity. Basically, any mutable field anywhere is an additional invisible parameter of the Java method. This makes reasoning about Java much harder.

Slide 108

Slide 108 text

q: Are Clojure collections pure?

Slide 109

Slide 109 text

a: Yes, for vectors, maps, and sets. Clojure collections are persistent (aka immutable). They can act as functions. As functions, they are pure.

Slide 110

Slide 110 text

q: What does this evaluate to? ([:arthur :ford :zaphod] 1)

Slide 111

Slide 111 text

a: It evaluates to :ford. A vector can acts as a function; the parameter is the index.

Slide 112

Slide 112 text

q: What does this evaluate to? ({:author "John Brunner"} :author)

Slide 113

Slide 113 text

a: It evaluates to "John Brunner". Matching keys return their value. Non-matching keys return nil, or the second parameter.

Slide 114

Slide 114 text

q: What does this evaluate to? (#{:blinky :pinky :inky :clyde} :pinky)

Slide 115

Slide 115 text

a: It evaluates to :pinky. Sets return the matching value if found, or nil. This is idiomatic for testing for inclusion in the set.

Slide 116

Slide 116 text

higher order functions

Slide 117

Slide 117 text

q: What is truth?

Slide 118

Slide 118 text

a: I'm not sure, I think it is related to beauty. Truthy is easier. nil and false are falsey. Anything else is truthy.

Slide 119

Slide 119 text

q: Is odd? a predicate? (doc odd?) ------------------------- clojure.core/odd? ([n]) Returns true if n is odd, throws an exception if n is not an integer

Slide 120

Slide 120 text

a: Yes, because odd? is a 1-arity function that returns a truthy response. Predicates end in ? by convention.

Slide 121

Slide 121 text

q: What does this evaluate to? (iterate inc 1) inc is a function that adds one to a number

Slide 122

Slide 122 text

a: It evaluates to (1 2 3 4 5 …) iterate keeps passing a value through a function, building an infinite list from it, as if: (1 (inc 1) (inc (inc 1)) (inc (inc (inc 1))) …) Clojure handles infinite data really well via lazy evaluation.

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

q: What does this symbolize? f ?

Slide 125

Slide 125 text

a: A filter. The filter function is a predicate. Each collection value is passed, in turn, to the predicate. Using filter, only the truthy predicate results are included in the result. remove is the opposite; truthy predicate results are removed, falsey are retained.

Slide 126

Slide 126 text

q: What does this evaluate to? (take 10 (filter odd? (iterate inc 1))

Slide 127

Slide 127 text

a: It evaluates to 
 (1 3 5 7 9 11 13 15 17 19). take will return up to N values from a collection. take and filter are lazy.

Slide 128

Slide 128 text

q: Isn't that getting nested a bit deep?

Slide 129

Slide 129 text

a: Yes, so we would use the ->> ("thread last") macro. (->> (iterate inc 1) (filter odd?) (take 10)) This makes the flow of transformations easier to understand.

Slide 130

Slide 130 text

q: What does this symbolize? f

Slide 131

Slide 131 text

a: A mapping operation. The provided function can convert one cube to one pyramid. f The map function converts a collection of cubes to a collection of pyramids.

Slide 132

Slide 132 text

q: What does this evaluate to? (map inc [5 10 50])

Slide 133

Slide 133 text

a: It evaluates to (6 11 51). map does not return a vector; this is a lazy list.

Slide 134

Slide 134 text

q: What does this evaluate to? (take 10 (map * (iterate inc 1) (iterate inc 1))) * is, of course, multiplication.

Slide 135

Slide 135 text

a: It evaluates to (1 4 9 16 25 36 49 64 81 100). f The arity of the mapping function must match the number of collections passed to map.

Slide 136

Slide 136 text

q: Are lazy collections pure?

Slide 137

Slide 137 text

a: Yes. Mostly. A lazy collection’s internal state does mutate, as lazy values are computed. A lazy collection’s externally visible state is pure, as long as the mapping function (for map) or predicate (for filter and reduce) is pure. Of course, the values in the collection need to be pure as well.

Slide 138

Slide 138 text

q: What is a race condition?

Slide 139

Slide 139 text

a: A race condition occurs when two different threads read and mutate the same memory, at the same time.

Slide 140

Slide 140 text

No content

Slide 141

Slide 141 text

No content

Slide 142

Slide 142 text

q: Are race conditions the only risk?

Slide 143

Slide 143 text

a: Heck no! There are deadlocks where two or more threads just stop. Or … sometimes different threads see different values in the same mutable field. Or things can just get slow (because of locks).

Slide 144

Slide 144 text

No content

Slide 145

Slide 145 text

q: Are lazy collections thread safe?

Slide 146

Slide 146 text

a: Yes. That internal mutable state is properly protected by locks. All threads will see a consistent view of the lazy collection.

Slide 147

Slide 147 text

back to higher order functions

Slide 148

Slide 148 text

Some Coders (def coders [{:name "Rich" :language :clojure} {:name "Guido" :language :python} {:name "Bjarne" :language :c++} {:name "Charles" :language :forth} {:name "Brendan" :language :javascript}])

Slide 149

Slide 149 text

q: How can I get the names of all the coders?

Slide 150

Slide 150 text

a: (map :name coders) will evaluate to:
 ("Rich" "Guido" "Bjarne" "Charles" "Brendan") A keyword can act as a function for accessing a value from a map. Remember that map is a data structure, and map is a function for transforming collections.

Slide 151

Slide 151 text

q: How can I get the set of first letters of the coders’ names?

Slide 152

Slide 152 text

a: The output of one mapping operation can be the input to the next. (->> coders (map :name) (map #(subs % 0 1)) set) ➠ #{"G" "R" "C" "B"} subs extracts a substring from a string. set converts a collection to a set. Notice how "B" is only present once.

Slide 153

Slide 153 text

q: What does this symbolize? f

Slide 154

Slide 154 text

a: A reducing operation. f The reducing function accepts an accumulator and a collection value, and returns a new accumulator. It reduces a collection to a single accumulated value.

Slide 155

Slide 155 text

q: What does (reduce + [1 20 300 4000]) evaluate to?

Slide 156

Slide 156 text

a: It evaluates to 4321 as follows: (+ 1 20) ➠ 21 (+ 21 300) ➠ 321 (+ 321 4000) ➠ 4321 The first value in the collection, 1, is treated as the initial accumulator.

Slide 157

Slide 157 text

q: What is reduce useful for?

Slide 158

Slide 158 text

a: Lots of things, like deriving meaning from data. Example: finding the frequencies of specific values.

Slide 159

Slide 159 text

(reduce (fn [m v] (update m v (fnil inc 0))) {} [1 3 5 10 5 3]) ➠{1 1, 3 2, 5 2, 10 1}

Slide 160

Slide 160 text

1. Start with an empty map 2. For each collection value: 1. Update the map, using the value as a key 2. Increment the map value (the count) 3. When the map value is nil, use 0 as default (reduce (fn [m v] (update m v (fnil inc 0))) {} [1 3 5 10 5 3])

Slide 161

Slide 161 text

(require '[clojure.string :as str]) (def text "Any programming language can be divided into two parts: some set of fundamental operators that play the role of axioms, and the rest of the language, which could in principle be written in terms of these fundamental operators.\n") (def sep #"[\p{Space}|\p{Punct}]+") (->> (str/split text sep) (map count) frequencies) ➠ {7 2, 4 6, 6 1, 3 8, 2 8, 11 3, 9 3, 5 5, 8 2}