Your SlideShare is downloading. ×
0
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Upcoming SlideShare
Loading in...5
×

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.
Text the download link to your phone
Standard text messaging rates apply

Advanced

90

Published on

Clojure workshop, advanced end that shows off some higher level features.

Clojure workshop, advanced end that shows off some higher level features.

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

  • Be the first to like this

No Downloads
Views
Total Views
90
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
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. CLOJURE WORKSHOP
  • 2. RECURSION
  • 3. (defn length ([collection] (length collection 0)) ([collection accumulator] (if (empty? collection) accumulator (recur (rest collection) (inc accumulator)))))
  • 4. (loop [x 10]   (when (> x 1)     (println x)     (recur (- x 2))))
  • 5. SEQUENCE PROCESSING
  • 6. RECURSIVE (defn  balance    ([string]      (balance  0  (seq  string)))    ([count  [head  &  tail  :as  chars]]      (if  (not  (empty?  chars))          (case  head              (  (recur  (inc  count)  tail)              )  (if  (zero?  count)                        false                        (recur  (dec  count)  tail))              (recur  count  tail))          true)))
  • 7. PATTERN MATCHING (defn-­‐match  balance    ([?string]                          (balance  0  (seq  string)))    ([_            []                    ]  true)    ([?count  [(  &  ?tail]]  (balance  (inc  count)  tail))    ([0            [)  &  _]        ]  false)    ([?count  [)  &  ?tail]]  (balance  (dec  count)  tail))    ([?count  [_    &  ?tail]]  (balance  count  tail)))
  • 8. SEQUENCE PROCESSING (defn  balance  [string]    (-­‐>>  string            seq            (map  {(  inc  )  dec})            (filter  identity)            (reductions  #(%2  %1)  0)            (filter  neg?)            empty?))
  • 9. PROTOCOLS
  • 10. (defrecord  CartesianCoordinate  [x  y])
  • 11. (defrecord  PolarCoordinate  [distance  angle])
  • 12. (defprotocol  Moveable    (move-­‐north  [self  amount])    (move-­‐east    [self  amount]))
  • 13. (extend-­‐type  CartesianCoordinate    Moveable    (move-­‐north  [{x  :x  y  :y}  ammount]        (CartesianCoordinate.  (+  x  ammount)  y))    (move-­‐east  [{x  :x  y  :y}  ammount]        (CartesianCoordinate.  x  (+  y  ammount))))
  • 14. (defrecord  CenterPointRectangle  [center-­‐point  width  height])
  • 15. (defrecord  CornerPointRectangle  [top-­‐left  bottom-­‐right])
  • 16. (extend-­‐type  CenterPointRectangle      Moveable    (move-­‐north  [self  ammount]        (update-­‐in  self  [:center-­‐point]                              #(move-­‐x  %  ammount)))    (move-­‐east  [self  ammount]        (update-­‐in  self  [:center-­‐point]                              #(move-­‐y  %  ammount))))
  • 17. MACROS
  • 18. '(println "Hello, World")
  • 19. '(println "Hello, World") => (println "Hello, World")
  • 20. (first '(println "Hello, World"))
  • 21. (first '(println "Hello, World")) => println
  • 22. (def function-name (first '(println "Hello, World"))) (list function-name "Goodbye, Cruel World")
  • 23. (def function-name (first '(println "Hello, World"))) (list function-name "Goodbye, Cruel World") => (println "Goodbye, Cruel World")
  • 24. (def function-name (first '(println "Hello, World"))) (def new-code (list function-name "Goodbye, Cruel World"))   (eval new-code)
  • 25. (def function-name (first '(println "Hello, World"))) (def new-code (list function-name "Goodbye, Cruel World"))   (eval new-code) prints: “Goodbye, Cruel World”
  • 26. (defmacro emoify [original-code] (let [function-name (first '(println "Hello, World"))] (list function-name "Goodbye, Cruel World")   (emoify (println "Hello, World"))
  • 27. (defmacro emoify [original-code] (let [function-name (first '(println "Hello, World"))] (list function-name "Goodbye, Cruel World")   (emoify (println "Hello, World")) prints: “Goodbye, Cruel World”
  • 28. FIN Questions?

×