0
Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Basics

143

Published on

Clojure basics from the MFUG Clojure workshop.

Clojure basics from the MFUG Clojure workshop.

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
143
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
3
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. CLOJURE WORKSHOP
• 2. println ("Hello, World")
• 3. (println "Hello, World")
• 4. SCALARS
• 5. nil
• 6. true false
• 7. 3.14
• 8. H i
• 9. "Kittens are cute"
• 10. :hello
• 11. :foo/bar
• 12. 'println
• 13. 'clojure.core/println
• 14. COLLECTIONS
• 15. [1, 2, 3, 4]
• 16. [1 2 3 4]
• 17. [1 2 :three "four"]
• 18. {:foo 1, :bar 2}
• 19. {:foo 1 :bar 2}
• 20. { "fizz" true :bugs false [1 2 3] {:do "re"} }
• 21. #{1 2 3}
• 22. '(1 2 3)
• 23. THE SEQUENCE ABSTRACTION
• 24. (first [1 2 3])
• 25. (first [1 2 3]) ; => 1
• 26. (first [1 2 3]) ; => 1 (first '(1 2 3))
• 27. (first [1 2 3]) ; => 1 (first '(1 2 3)) ; => 1
• 28. (first [1 2 3]) ; => 1 (first '(1 2 3)) ; => 1 (first #{1 2 3})
• 29. (first [1 2 3]) ; => 1 (first '(1 2 3)) ; => 1 (first #{1 2 3}) ; => 2
• 30. (first [1 2 3]) ; => 1 (first '(1 2 3)) ; => 1 (first #{1 2 3}) ; => 2 (first {:one 1 :two 2})
• 31. (first [1 2 3]) ; => 1 (first '(1 2 3)) ; => 1 (first #{1 2 3}) ; => 2 (first {:one 1 :two 2}) ; => [:two 2]
• 32. (range 10)
• 33. (range 10) ;=> (0 1 2 3 4 5 6 7 8 9)
• 34. (take 5 (range))
• 35. (take 5 (range)) ; => (0 1 2 3 4)
• 36. (defn fib [a b] (cons a (lazy-seq (fib b (+ b a)))))
• 37. FUNCTIONS
• 38. println ("Hello, World")
• 39. (println "Hello, World")
• 40. (+ 40 2)
• 41. (+ 10 8 20 4)
• 42. (fn [name] (println "Hello " name))
• 43. (def host "Thoughtworks")
• 44. (def greet (fn [name] (println "Hello " name)))
• 45. (def greet [name] fn (println "Hello " name))
• 46. (map inc [1 5 9])
• 47. (map inc [1 5 9]) => [2 6 10]
• 48. (defn create-incrementor [increment-by] (fn [number] (+ number increment-by)))
• 49. (defn create-incrementor [increment-by] (fn [number] (+ number increment-by))) (map (create-incrementor 7) [1 5 9])
• 50. (defn create-incrementor [increment-by] (fn [number] (+ number increment-by))) (map (create-incrementor 7) [1 5 9]) => [8 12 16]
• 51. REAL WORLD
• 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. 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. JSON (ns examples.json (:use cheshire.core)) (generate-string {:foo "bar" :baz 5}) => {“foo”: “bar”, “baz”: 5} (parse-string "{"foo":"bar"}") => {"foo" "bar"}
• 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. 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. 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. GUI
• 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. FUNCTIONAL PROGRAMMING
• 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. 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. 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. (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. RESOURCES
• 66. WEBSITES
• 67. CLOJURE.ORG
• 68. CLOJUREDOCS.ORG
• 69. CLOJARS.ORG
• 70. CLOJURE-DOC.ORG
• 71. VIDEO