basics

83
CLOJURE WORKSHOP

Upload: logan-campbell

Post on 15-Nov-2014

256 views

Category:

Technology


0 download

DESCRIPTION

Clojure basics from the MFUG Clojure workshop.

TRANSCRIPT

Page 1: Basics

CLOJURE WORKSHOP

Page 2: Basics

println "Hello, World")(

Page 3: Basics

println "Hello, World")(

Page 4: Basics

SCALARS

Page 5: Basics

nil

Page 6: Basics

truefalse

Page 7: Basics

3.14

Page 8: Basics

\H\i

Page 9: Basics

"Kittens are cute"

Page 10: Basics

:hello

Page 11: Basics

:foo/bar

Page 12: Basics

'println

Page 13: Basics

'clojure.core/println

Page 14: Basics

COLLECTIONS

Page 15: Basics

[1 2 4]3, , ,

Page 16: Basics

4][1 2 3

Page 17: Basics

[1 2 :three "four"]

Page 18: Basics

:bar 2},{:foo 1

Page 19: Basics

:bar 2}{:foo 1

Page 20: Basics

{ "fizz" true :bugs false [1 2 3] {:do "re"}}

Page 21: Basics

#{1 2 3}

Page 22: Basics

'(1 2 3)

Page 23: Basics

THE SEQUENCE ABSTRACTION

Page 24: Basics

(first [1 2 3])

Page 25: Basics

(first [1 2 3]); => 1

Page 26: Basics

(first [1 2 3]); => 1

(first '(1 2 3))

Page 27: Basics

(first [1 2 3]); => 1

(first '(1 2 3)); => 1

Page 28: Basics

(first [1 2 3]); => 1

(first '(1 2 3)); => 1

(first #{1 2 3})

Page 29: Basics

(first [1 2 3]); => 1

(first '(1 2 3)); => 1

(first #{1 2 3}); => 2

Page 30: Basics

(first [1 2 3]); => 1

(first '(1 2 3)); => 1

(first #{1 2 3}); => 2

(first {:one 1 :two 2})

Page 31: Basics

(first [1 2 3]); => 1

(first '(1 2 3)); => 1

(first #{1 2 3}); => 2

(first {:one 1 :two 2}); => [:two 2]

Page 32: Basics

(range 10)

Page 33: Basics

(range 10);=> (0 1 2 3 4 5 6 7 8 9)

Page 34: Basics

(take 5 (range))

Page 35: Basics

; => (0 1 2 3 4)(take 5 (range))

Page 36: Basics

(defn fib [a b] (cons a (lazy-seq (fib b (+ b a)))))

Page 37: Basics

FUNCTIONS

Page 38: Basics

println "Hello, World")(

Page 39: Basics

println "Hello, World")(

Page 40: Basics

(+ 40 2)

Page 41: Basics

(+ 10 8 20 4)

Page 42: Basics

(fn [name] (println "Hello " name))

Page 43: Basics

(def host "Thoughtworks")

Page 44: Basics

[name] ( greet(def

) (println "Hello " name)) fn

Page 45: Basics

(println "Hello " name)) fn greet(def [name]

Page 46: Basics

(map inc [1 5 9])

Page 47: Basics

(map inc [1 5 9])=> [2 6 10]

Page 48: Basics

(defn create-incrementor [increment-by] (fn [number] (+ number increment-by)))

Page 49: Basics

(map (create-incrementor 7) [1 5 9])

(defn create-incrementor [increment-by] (fn [number] (+ number increment-by)))

Page 50: Basics

(map (create-incrementor 7) [1 5 9])

(defn create-incrementor [increment-by] (fn [number] (+ number increment-by)))

=> [8 12 16]

Page 51: Basics

REAL WORLD

Page 52: Basics

(fact (split "a/b/c" #"/") => ["a" "b" "c"])

(tabular (fact "The rules of Conway's life" (alive? ?cell-status ?neighbor-count) => ?expected)

?cell-status ?neighbor-count ?expected :alive 1 false :alive 2 true :alive 3 true :alive 4 false :dead 2 false :dead 3 true :dead 4 false)

AUTOMATED TESTS

Page 53: Basics

(defdb prod (postgres {:db "korma" :user "db" :password "dbpass"})) (defentity address)(defentity user (has-one address)) (select user (with address) (fields :firstName :lastName :address.state) (where {:email "[email protected]"}))

SQL

Page 54: Basics

(ns examples.json (:use cheshire.core))

(generate-string {:foo "bar" :baz 5})=> {“foo”: “bar”, “baz”: 5}

(parse-string "{\"foo\":\"bar\"}")=> {"foo" "bar"}

JSON

Page 55: Basics

(ns examples.xml (:use clojure.xml) (:import java.io.File))

(def xml-doc (parse (File. "calendar.xml")))

(get-in xml-doc [:content 1 :content 0 :content])=> [“Rover’s birthday”]

XML PARSING

Page 56: Basics

(ns examples.templating (:use hiccup.core) (html [:span {:class "foo"} "bar"])=> "<span class=\"foo\">bar</span>" (html [:ul (for [x (range 1 4)] [:li x])])=> "<ul><li>1</li><li>2</li><li>3</li></ul>"

HTML

Page 57: Basics

(ns hello-world (:use compojure.core) (:require [compojure.route :as route]))

(defroutes app (GET "/" [] "<h1>Hello World</h1>") (route/not-found "<h1>Page not found</h1>"))

WEB

Page 58: Basics

GUI

Page 59: Basics

(ns seesaw.test.examples.slider  (:use [seesaw core color border] seesaw.test.examples.example))

(defn make-frame []  (frame     :title "Slider Example"    :content      (horizontal-panel :items [        (vertical-panel :items [          "<html>Slide the sliders to change<br>the color to the right</html>"          (slider :id :red :min 0 :max 255)          (slider :id :green :min 0 :max 255)          (slider :id :blue :min 0 :max 255)])        (canvas :id :canvas :border (line-border) :size [200 :by 200])])))

(defn update-color [root]  (let [{:keys [red green blue]} (value root)]    (config! (select root [:#canvas]) :background (color red green blue))))

(defexample []  (let [root (make-frame)]    (listen (map #(select root [%]) [:#red :#green :#blue]) :change            (fn [e] (update-color root)))    root))

GUI

Page 60: Basics

FUNCTIONAL PROGRAMMING

Page 61: Basics

number = 1 while number < 20 if number % 3 == 0 && number % 5 == 0 puts "fizzbuzz" elsif number % 3 == 0 puts "fizz" elsif number % 5 == 0 puts "buzz" else puts number end  number = number + 1end

Page 62: Basics

for number in 1..20 if number % 3 == 0 && number % 5 == 0 puts "fizzbuzz" elsif number % 3 == 0 puts "fizz" elsif number % 5 == 0 puts "buzz" else puts number endend

Page 63: Basics

numbers = (0..20).map do |number| if number % 3 == 0 && number % 5 == 0 "fizzbuzz" elsif number % 3 == 0 "fizz" elsif number % 5 == 0 "buzz" else number endend puts numbers

Page 64: Basics

(defn divisible-by? [divisor number] (= 0 (mod number divisor))) (defn fizz-buzzify [number] (cond (divisible-by? 15 number) "fizzbuzz" (divisible-by? 3 number) "fizz" (divisible-by? 5 number) "buzz" :else number)) (def fizz-buzz (map fizz-buzzify (range))) (mapv println (take 100 fizz-buzz))

Page 65: Basics

RESOURCES

Page 66: Basics

WEBSITES

Page 67: Basics

CLOJURE.ORG

Page 68: Basics

CLOJUREDOCS.ORG

Page 69: Basics

CLOJARS.ORG

Page 70: Basics

CLOJURE-DOC.ORG

Page 71: Basics

VIDEO

Page 72: Basics

YOUTUBE - CLOJURETV

Page 73: Basics

INFOQ - CLOJURE TAG

Page 74: Basics

BOOKS

Page 75: Basics

CLOJURE PROGRAMMING

Page 76: Basics

PROGRAMMING CLOJURE

Page 77: Basics

CLOJURE IN ACTION

Page 78: Basics

THE JOY OF CLOJURE

Page 79: Basics

PRACTICAL CLOJURE

Page 80: Basics

FP4OOP

Page 81: Basics

SIMPLE MADE EASY

Page 82: Basics

IMPLEMENTATIONS

Clojure (JVM)

ClojureScript

ClojureCLR

clojure-py

clojure-scheme

Page 83: Basics

FINQuestions?