(first '(Clojure.))

  • 419 views
Uploaded on

An introduction to Clojure, held by Niklas Lindström at Valtech Tech Day 2012

An introduction to Clojure, held by Niklas Lindström at Valtech Tech Day 2012

More in: Technology , Design
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
419
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
3
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. (first (Clojure.))
  • 2. Clojure isA dynamic,LISP-basedprogramming languagerunning on the JVM
  • 3. Origin2007, Rich Hickey.. 1958, John McCarthy
  • 4. FeaturesFunctionalHomoiconicImmutability
  • 5. PlusConcurrencyPragmatismJava Interop
  • 6. Fun!
  • 7. Syntax
  • 8. Homoiconic(+ 1 1)● Uniform Prefix Notation● List Processing (LISP)● Code is Data=> Predictable!
  • 9. Scalarsc ; characters"Hello" ; strings42 ; numbers3/2 ; ratiostrue ; booleansnil
  • 10. Names; symbols ; keywordsi :nameprintln :when+empty?next-state.toUpperCase
  • 11. Collections
  • 12. Lists(println "Hello")(first (println "Hello"))
  • 13. Vectors["bag" "of" 4 "chips"]
  • 14. Hash-maps{:name "Intro" :date "2012-05-29"}
  • 15. Sets#{"unique" "snowflakes"}
  • 16. Special Forms
  • 17. Define(def i 1)
  • 18. If(if true (/ 1 1) (/ 1 0))
  • 19. Functions(fn [name] (str "Hello " name "!"))
  • 20. Named Functions(def greet (fn [name] (str "Hello " name)))
  • 21. The defn macro(defn greet [name] (str "Hello " name))
  • 22. Let the local names in(defn greet [name] (let [greeting "Hello" out (str greeting " " name)] out))
  • 23. Sugared Lambdas#(str %1 %2); same as(fn [a1 a2] (str a1 a2))
  • 24. The rest is just eval/apply
  • 25. Basic Usage => represents results
  • 26. Vectors(def items [1 2 3])(first items)=> 1(rest items)=> (2 3)(items 0)=> 1
  • 27. Adding to collections(cons items [3 4])=> [1 2 3 4](conj items 3)=> [1 2 3]
  • 28. Map/Reduce(map #(+ % 1) [1 2 3])=> (2 3 4)(reduce #(- %1 %2) [1 2 3])=> -4
  • 29. For Comprehension(for [i (range 1 7) :when (even? i)] (str "item-" i)))=> ("item-2" "item-4" "item-6")
  • 30. Composing Data
  • 31. Constructs(def persons [{:id 1 :name "Some Body"} {:id 2 :name "Some Other"}])(let [person (persons 0)] (person :name))=> "Some Body"
  • 32. The key is getting the value(:name (first persons))=> "Some Body"(map :name persons)=> ("Some Body" "Some Other")
  • 33. Records(defrecord Person [id name])(let [person (Person. 1 "Some Body")] (:name person))=> "Some Body"
  • 34. Destructuring(let [{id :id name :name} person] [id name])=> [1 "Some Body"]
  • 35. Compose Data, Compose FunctionsData firstCollectionsFlowAbstractions
  • 36. Concurrency
  • 37. States over TimeValue as StateReference to IdentityIdentity over Time
  • 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. 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. Agents(def info (agent {}))(send info assoc :status :open); eventually,; (assoc info :status :open); is called
  • 41. Java Interop
  • 42. Working with MonstersCan be shackled by parentheses,even fed immutability,but their nature is wild
  • 43. Parallel HTTP Fetches(ns parallel-fetch (:import (java.io InputStream InputStreamReader BufferedReader) (java.net URL HttpURLConnection)))
  • 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. (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. Interface On Your Own Terms
  • 47. Idiom(get-name root)(get-attr link "href")(get-child-elements div)
  • 48. Protocol(defprotocol DomAccess (get-name [this]) (get-attr [this attr-name]) (get-child-elements [this]))
  • 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. Working with Clojure
  • 51. Leiningen$ lein deps$ lein compile$ lein repl$ lein uberjar$ lein ring server-headless
  • 52. EditorsVim: VimClojureEmacs: SwankIDEA: La Clojure
  • 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. Have Fun!
  • 55. (thanks!)
  • 56. @niklasl@valtechSweden
  • 57. Attribution● Clojure Logo © Rich Hickey● Playing Ball● "Where the Wild Things Are" © Maurice Sendak● Parallel HTTP Fetches by Will Larson