(first '(Clojure.))

498 views
427 views

Published on

An introduction to Clojure I held at the Valtech Tech Day 2012.

Published in: Technology, Design
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
498
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

(first '(Clojure.))

  1. 1. (first (Clojure.))
  2. 2. Clojure isA dynamic,LISP-basedprogramming languagerunning on the JVM
  3. 3. Origin2007, Rich Hickey.. 1958, John McCarthy
  4. 4. FeaturesFunctionalHomoiconicImmutability
  5. 5. PlusConcurrencyPragmatismJava Interop
  6. 6. Fun!
  7. 7. Syntax
  8. 8. Homoiconic(+ 1 1)● Uniform Prefix Notation● List Processing (LISP)● Code is Data=> Predictable!
  9. 9. Scalarsc ; characters"Hello" ; strings42 ; numbers3/2 ; ratiostrue ; booleansnil
  10. 10. Names; symbols ; keywordsi :nameprintln :when+empty?next-state.toUpperCase
  11. 11. Collections
  12. 12. Lists(println "Hello")(first (println "Hello"))
  13. 13. Vectors["bag" "of" 4 "chips"]
  14. 14. Hash-maps{:name "Intro" :date "2012-05-29"}
  15. 15. Sets#{"unique" "snowflakes"}
  16. 16. Special Forms
  17. 17. Define(def i 1)
  18. 18. If(if true (/ 1 1) (/ 1 0))
  19. 19. Functions(fn [name] (str "Hello " name "!"))
  20. 20. Named Functions(def greet (fn [name] (str "Hello " name)))
  21. 21. The defn macro(defn greet [name] (str "Hello " name))
  22. 22. Let the local names in(defn greet [name] (let [greeting "Hello" out (str greeting " " name)] out))
  23. 23. Sugared Lambdas#(str %1 %2); same as(fn [a1 a2] (str a1 a2))
  24. 24. The rest is just eval/apply
  25. 25. Basic Usage => represents results
  26. 26. Vectors(def items [1 2 3])(first items)=> 1(rest items)=> (2 3)(items 0)=> 1
  27. 27. Adding to collections(cons items [3 4])=> [1 2 3 4](conj items 3)=> [1 2 3]
  28. 28. Map/Reduce(map #(+ % 1) [1 2 3])=> (2 3 4)(reduce #(- %1 %2) [1 2 3])=> -4
  29. 29. For Comprehension(for [i (range 1 7) :when (even? i)] (str "item-" i)))=> ("item-2" "item-4" "item-6")
  30. 30. Composing Data
  31. 31. Constructs(def persons [{:id 1 :name "Some Body"} {:id 2 :name "Some Other"}])(let [person (persons 0)] (person :name))=> "Some Body"
  32. 32. The key is getting the value(:name (first persons))=> "Some Body"(map :name persons)=> ("Some Body" "Some Other")
  33. 33. Records(defrecord Person [id name])(let [person (Person. 1 "Some Body")] (:name person))=> "Some Body"
  34. 34. Destructuring(let [{id :id name :name} person] [id name])=> [1 "Some Body"]
  35. 35. Compose Data, Compose FunctionsData firstCollectionsFlowAbstractions
  36. 36. Concurrency
  37. 37. States over TimeValue as StateReference to IdentityIdentity over Time
  38. 38. Atoms(def item-counter (atom 0))(defn next-item [] (str "item-" (swap! item-counter inc)))(next-item)=> "item-1"(next-item)=> "item-2"...
  39. 39. Refs(def item1 (ref {:a "foo"}))(def item2 (ref {:a "bar"}))(let [v1 (:a @item1) v2 (:a @item2)] (dosync (alter item1 assoc :a v2) (alter item2 assoc :a v1)))
  40. 40. Agents(def info (agent {}))(send info assoc :status :open); eventually,; (assoc info :status :open); is called
  41. 41. Java Interop
  42. 42. Working with MonstersCan be shackled by parentheses,even fed immutability,but their nature is wild
  43. 43. Parallel HTTP Fetches(ns parallel-fetch (:import (java.io InputStream InputStreamReader BufferedReader) (java.net URL HttpURLConnection)))
  44. 44. (defn get-url [url] (let [conn (doto (.openConnection (URL. url)) (.setRequestMethod "GET") (.connect))] (with-open [stream (BufferedReader. (InputStreamReader. (.getInputStream conn)))] (.toString (reduce #(.append %1 %2) (StringBuffer.) (line-seq stream))))))
  45. 45. (defn get-urls [urls] (let [agents (doall (map #(agent %) urls))] (doseq [agent agents] (send-off agent get-url)) (apply await-for 5000 agents) (doall (map #(deref %) agents))))(prn (get-urls ["http://erlang.org" "http://clojure.org/"]))
  46. 46. Interface On Your Own Terms
  47. 47. Idiom(get-name root)(get-attr link "href")(get-child-elements div)
  48. 48. Protocol(defprotocol DomAccess (get-name [this]) (get-attr [this attr-name]) (get-child-elements [this]))
  49. 49. Realization(extend-type org.w3c.dom.Node DomAccess (get-name [this] (.getNodeName this)) (get-attr [this attr-name] (if (.hasAttribute this attr-name) (.getAttribute this attr-name))) (get-child-elements [this] (filter #(= (.getNodeType %1) Node/ELEMENT_NODE) (node-list (.getChildNodes this)))))
  50. 50. Working with Clojure
  51. 51. Leiningen$ lein deps$ lein compile$ lein repl$ lein uberjar$ lein ring server-headless
  52. 52. EditorsVim: VimClojureEmacs: SwankIDEA: La Clojure
  53. 53. More Info<http://clojure.org/><http://clojure.org/cheatsheet><http://clojuredocs.org/><http://dev.clojure.org/display/community/Clojure+Success+Stories><http://www.infoq.com/presentations/Are-We-There-Yet-Rich-Hickey><http://www.infoq.com/presentations/Simple-Made-Easy>
  54. 54. Have Fun!
  55. 55. (thanks!)
  56. 56. @niklasl@valtechSweden
  57. 57. Attribution● Clojure Logo © Rich Hickey● Playing Ball● "Where the Wild Things Are" © Maurice Sendak● Parallel HTTP Fetches by Will Larson

×