Slide 1

Slide 1 text

Introduction to Clojure Friday, October 11, 13

Slide 2

Slide 2 text

Clojure, the language Friday, October 11, 13

Slide 3

Slide 3 text

• Extremely expressive • First-class functions • Awesome Java Interoperability • Power of lisp (macros, function composition) • FUNctional Friday, October 11, 13

Slide 4

Slide 4 text

What are we looking for? Friday, October 11, 13

Slide 5

Slide 5 text

• sane development environment, integrated with a language • ability to develop and evaluate things on the fly • ease maintenance production • scalability • integration with well-known / existing tools • good, stable runtime • runtime debugging toolkit • performance Friday, October 11, 13

Slide 6

Slide 6 text

• Leiningen, Clojure build tool https://github.com/technomancy/leiningen • repl just run `lein repl` in your console • emacs (it’s written in lisp, you know?) excellent nrepl and (older thing) clojure-swank, paredit-mode, formatting • nrepl get to the guts of runtime • VisualVm and the rest of Java goodness like jmap, jstack, jstat etc etc etc Friday, October 11, 13

Slide 7

Slide 7 text

• JMX java management extensions • nrepl • JVM configuration/tuning • No shared mutable state • No locks • Pure functions -> same input -> same output • Easy composition and pipelining Friday, October 11, 13

Slide 8

Slide 8 text

• Good baseline performance • Utilization of all processor resources anyone can eat out all the memory, right? • Painless parallel execution • Memory efficiency as far as it is possible with lisps, of course. in my mind that was mostly about structural sharing & persistent DS • JIT / inlining Friday, October 11, 13

Slide 9

Slide 9 text

• Very, very good Java interop options • Most of libraries are either ported to Clojure, or use Java library underneath • Your favorite Java application container / web server • Jar packaging / public / private Maven repositories • Not to replace, but to complement your existing JVM toolkit Friday, October 11, 13

Slide 10

Slide 10 text

Primer Friday, October 11, 13

Slide 11

Slide 11 text

Functions Friday, October 11, 13

Slide 12

Slide 12 text

function () { return 5; } JS Function Friday, October 11, 13

Slide 13

Slide 13 text

(function () return 5) Add parentheses Friday, October 11, 13

Slide 14

Slide 14 text

(fn [] return 5) function -> fn Friday, October 11, 13

Slide 15

Slide 15 text

(fn [] 5) Ditch return Friday, October 11, 13

Slide 16

Slide 16 text

Prefix notation, function calls Friday, October 11, 13

Slide 17

Slide 17 text

(+ 1 1) Function Arguments Friday, October 11, 13

Slide 18

Slide 18 text

Everything is an expression Friday, October 11, 13

Slide 19

Slide 19 text

5 ; 㱺 5 Friday, October 11, 13

Slide 20

Slide 20 text

(+ 1 1) ; 㱺 2 Friday, October 11, 13

Slide 21

Slide 21 text

(if true "yes" "no") ; 㱺 “yes” Friday, October 11, 13

Slide 22

Slide 22 text

Language Primitives Friday, October 11, 13

Slide 23

Slide 23 text

"this is a string" String Friday, October 11, 13

Slide 24

Slide 24 text

:key Keyword Friday, October 11, 13

Slide 25

Slide 25 text

'key Symbol Friday, October 11, 13

Slide 26

Slide 26 text

[1 2 3] [:key 1 "value"] Vector Friday, October 11, 13

Slide 27

Slide 27 text

{:key "value" :key2 "value2"} Hash Friday, October 11, 13

Slide 28

Slide 28 text

Syntactic Sugar Friday, October 11, 13

Slide 29

Slide 29 text

#() => (fn []) Function Friday, October 11, 13

Slide 30

Slide 30 text

#{} => (set ..) Set Friday, October 11, 13

Slide 31

Slide 31 text

#inst "2013-10-04T13:32:41.008-00:00" Date Friday, October 11, 13

Slide 32

Slide 32 text

Definitions Friday, October 11, 13

Slide 33

Slide 33 text

(def a 1) Friday, October 11, 13

Slide 34

Slide 34 text

(def a (+ 1 (* 2 2))) Friday, October 11, 13

Slide 35

Slide 35 text

Functions Friday, October 11, 13

Slide 36

Slide 36 text

(fn [number] (- number 1)) Function Definition Argument (s) Function Body Friday, October 11, 13

Slide 37

Slide 37 text

(defn square "Returns a square of a number" [x] (* x x)) Function Definition Argument (s) Function Body Docstring Friday, October 11, 13

Slide 38

Slide 38 text

Destructuring basics Friday, October 11, 13

Slide 39

Slide 39 text

[1 2 3 ] [first second third] Friday, October 11, 13

Slide 40

Slide 40 text

Passing functions as arguments to functions Friday, October 11, 13

Slide 41

Slide 41 text

(map inc [1 2 3]) Function call Passed function Friday, October 11, 13

Slide 42

Slide 42 text

(map (fn [i] (+ i 1)) [1 2 3]) Function call Passed (anonymous) function Friday, October 11, 13

Slide 43

Slide 43 text

Getting help Friday, October 11, 13

Slide 44

Slide 44 text

(clojure.repl/doc defn) Friday, October 11, 13

Slide 45

Slide 45 text

(clojure.repl/source defn) Friday, October 11, 13

Slide 46

Slide 46 text

Let Friday, October 11, 13

Slide 47

Slide 47 text

function (a, b, c) { var sum = a + b + c; var count = 3; return sum / count; } Friday, October 11, 13

Slide 48

Slide 48 text

(fn [a b c] (let [sum (+ a b c) count 3] (/ sum count)) Friday, October 11, 13

Slide 49

Slide 49 text

Control structures Friday, October 11, 13

Slide 50

Slide 50 text

Conditionals Friday, October 11, 13

Slide 51

Slide 51 text

• Sometimes referred as “control structures” • Expressions, often implemented as macros • Used to delay evaluation of certain code blocks Friday, October 11, 13

Slide 52

Slide 52 text

if Friday, October 11, 13

Slide 53

Slide 53 text

Imagine “if” were a function for a minute: (if-fn (even? 2) (println "even!") (println "odd!")) Both “even!” and “odd!” are printed. Friday, October 11, 13

Slide 54

Slide 54 text

`if` structure/usage: (if (predicate) (truthy-expression) (falsy-expression)) Friday, October 11, 13

Slide 55

Slide 55 text

Multi-expression & do blocks (if (predicate) (do (truthy-expression) (another-truthy-expression)) (falsy-expression)) Friday, October 11, 13

Slide 56

Slide 56 text

when Friday, October 11, 13

Slide 57

Slide 57 text

(when (predicate) (expression-1) (expression-2)) Friday, October 11, 13

Slide 58

Slide 58 text

• Useful when you don’t have “false” part • You can write more than one expression within `where` block Friday, October 11, 13

Slide 59

Slide 59 text

if-let Friday, October 11, 13

Slide 60

Slide 60 text

(def user {:name "Alex"}) (if-let [name (:name user)] (println "Hello " name) (println "Name is empty")) Friday, October 11, 13

Slide 61

Slide 61 text

• Useful when you need to check an expression and reuse the expression result in “truth” block • As the name itself states, if-let behaves as let wrapped over if Friday, October 11, 13

Slide 62

Slide 62 text

cond Friday, October 11, 13

Slide 63

Slide 63 text

(cond (< n 0) "negative" (> n 0) "positive" :else "zero") Friday, October 11, 13

Slide 64

Slide 64 text

• Useful when you want to avoid multiple nested ifs • `:else` here is arbitrary, you can use any expression that evaluates to something truthful Friday, October 11, 13

Slide 65

Slide 65 text

Composition Friday, October 11, 13

Slide 66

Slide 66 text

(fn [c] (first (rest c))) (comp first rest) Friday, October 11, 13

Slide 67

Slide 67 text

Application Friday, October 11, 13

Slide 68

Slide 68 text

(apply max [1 2 3]) (max 1 2 3) Friday, October 11, 13

Slide 69

Slide 69 text

Namespaces Friday, October 11, 13

Slide 70

Slide 70 text

(ns superlib.core) Friday, October 11, 13

Slide 71

Slide 71 text

(:require ...) Friday, October 11, 13

Slide 72

Slide 72 text

(:import ...) Friday, October 11, 13

Slide 73

Slide 73 text

(:use ...) Friday, October 11, 13

Slide 74

Slide 74 text

(:gen-class ...) Friday, October 11, 13

Slide 75

Slide 75 text

Practical excercises Friday, October 11, 13

Slide 76

Slide 76 text

• arrange into groups of 2-3 people • you’ll be given a function name and parameters vector • tests are located under `test/workshop_tasks/*-test.clj • write a function, run the tests • submit results to exercise server Friday, October 11, 13