Successfully reported this slideshow.

Basics

350 views

Published on

Clojure basics from the MFUG Clojure workshop.

  • Be the first to comment

  • Be the first to like this

Basics

  1. 1. CLOJURE WORKSHOP
  2. 2. println ("Hello, World")
  3. 3. (println "Hello, World")
  4. 4. SCALARS
  5. 5. nil
  6. 6. true false
  7. 7. 3.14
  8. 8. H i
  9. 9. "Kittens are cute"
  10. 10. :hello
  11. 11. :foo/bar
  12. 12. 'println
  13. 13. 'clojure.core/println
  14. 14. COLLECTIONS
  15. 15. [1, 2, 3, 4]
  16. 16. [1 2 3 4]
  17. 17. [1 2 :three "four"]
  18. 18. {:foo 1, :bar 2}
  19. 19. {:foo 1 :bar 2}
  20. 20. { "fizz" true :bugs false [1 2 3] {:do "re"} }
  21. 21. #{1 2 3}
  22. 22. '(1 2 3)
  23. 23. THE SEQUENCE ABSTRACTION
  24. 24. (first [1 2 3])
  25. 25. (first [1 2 3]) ; => 1
  26. 26. (first [1 2 3]) ; => 1 (first '(1 2 3))
  27. 27. (first [1 2 3]) ; => 1 (first '(1 2 3)) ; => 1
  28. 28. (first [1 2 3]) ; => 1 (first '(1 2 3)) ; => 1 (first #{1 2 3})
  29. 29. (first [1 2 3]) ; => 1 (first '(1 2 3)) ; => 1 (first #{1 2 3}) ; => 2
  30. 30. (first [1 2 3]) ; => 1 (first '(1 2 3)) ; => 1 (first #{1 2 3}) ; => 2 (first {:one 1 :two 2})
  31. 31. (first [1 2 3]) ; => 1 (first '(1 2 3)) ; => 1 (first #{1 2 3}) ; => 2 (first {:one 1 :two 2}) ; => [:two 2]
  32. 32. (range 10)
  33. 33. (range 10) ;=> (0 1 2 3 4 5 6 7 8 9)
  34. 34. (take 5 (range))
  35. 35. (take 5 (range)) ; => (0 1 2 3 4)
  36. 36. (defn fib [a b] (cons a (lazy-seq (fib b (+ b a)))))
  37. 37. FUNCTIONS
  38. 38. println ("Hello, World")
  39. 39. (println "Hello, World")
  40. 40. (+ 40 2)
  41. 41. (+ 10 8 20 4)
  42. 42. (fn [name] (println "Hello " name))
  43. 43. (def host "Thoughtworks")
  44. 44. (def greet (fn [name] (println "Hello " name)))
  45. 45. (def greet [name] fn (println "Hello " name))
  46. 46. (map inc [1 5 9])
  47. 47. (map inc [1 5 9]) => [2 6 10]
  48. 48. (defn create-incrementor [increment-by] (fn [number] (+ number increment-by)))
  49. 49. (defn create-incrementor [increment-by] (fn [number] (+ number increment-by))) (map (create-incrementor 7) [1 5 9])
  50. 50. (defn create-incrementor [increment-by] (fn [number] (+ number increment-by))) (map (create-incrementor 7) [1 5 9]) => [8 12 16]
  51. 51. REAL WORLD
  52. 52. AUTOMATED TESTS (fact (split "a/b/c" #"/") => ["a" "b" "c"]) (tabular (fact "The rules of Conway's life" (alive? ?cell-status ?neighbor-count) => ?expected) ?cell-status :alive :alive :alive :alive ?neighbor-count 1 2 3 4 ?expected false true true false :dead :dead :dead 2 3 4 false true false)
  53. 53. SQL (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 "korma@sqlkorma.com"}))
  54. 54. JSON (ns examples.json (:use cheshire.core)) (generate-string {:foo "bar" :baz 5}) => {“foo”: “bar”, “baz”: 5} (parse-string "{"foo":"bar"}") => {"foo" "bar"}
  55. 55. XML PARSING (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”]
  56. 56. HTML (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>"
  57. 57. WEB (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>"))
  58. 58. GUI
  59. 59. GUI (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))
  60. 60. FUNCTIONAL PROGRAMMING
  61. 61. 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 + 1 end
  62. 62. 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 end end
  63. 63. 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 end end   puts numbers
  64. 64. (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))
  65. 65. RESOURCES
  66. 66. WEBSITES
  67. 67. CLOJURE.ORG
  68. 68. CLOJUREDOCS.ORG
  69. 69. CLOJARS.ORG
  70. 70. CLOJURE-DOC.ORG
  71. 71. VIDEO
  72. 72. YOUTUBE - CLOJURETV
  73. 73. INFOQ - CLOJURE TAG
  74. 74. BOOKS
  75. 75. CLOJURE PROGRAMMING
  76. 76. PROGRAMMING CLOJURE
  77. 77. CLOJURE IN ACTION
  78. 78. THE JOY OF CLOJURE
  79. 79. PRACTICAL CLOJURE
  80. 80. FP4OOP
  81. 81. SIMPLE MADE EASY
  82. 82. IMPLEMENTATIONS Clojure (JVM) ClojureScript ClojureCLR clojure-py clojure-scheme
  83. 83. FIN Questions?

×