Advanced

212 views

Published on

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
212
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Advanced

  1. 1. CLOJURE WORKSHOP
  2. 2. RECURSION
  3. 3. (defn length ([collection] (length collection 0)) ([collection accumulator] (if (empty? collection) accumulator (recur (rest collection) (inc accumulator)))))
  4. 4. (loop [x 10]   (when (> x 1)     (println x)     (recur (- x 2))))
  5. 5. SEQUENCE PROCESSING
  6. 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. 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. 8. SEQUENCE PROCESSING (defn  balance  [string]    (-­‐>>  string            seq            (map  {(  inc  )  dec})            (filter  identity)            (reductions  #(%2  %1)  0)            (filter  neg?)            empty?))
  9. 9. PROTOCOLS
  10. 10. (defrecord  CartesianCoordinate  [x  y])
  11. 11. (defrecord  PolarCoordinate  [distance  angle])
  12. 12. (defprotocol  Moveable    (move-­‐north  [self  amount])    (move-­‐east    [self  amount]))
  13. 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. 14. (defrecord  CenterPointRectangle  [center-­‐point  width  height])
  15. 15. (defrecord  CornerPointRectangle  [top-­‐left  bottom-­‐right])
  16. 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. 17. MACROS
  18. 18. '(println "Hello, World")
  19. 19. '(println "Hello, World") => (println "Hello, World")
  20. 20. (first '(println "Hello, World"))
  21. 21. (first '(println "Hello, World")) => println
  22. 22. (def function-name (first '(println "Hello, World"))) (list function-name "Goodbye, Cruel World")
  23. 23. (def function-name (first '(println "Hello, World"))) (list function-name "Goodbye, Cruel World") => (println "Goodbye, Cruel World")
  24. 24. (def function-name (first '(println "Hello, World"))) (def new-code (list function-name "Goodbye, Cruel World"))   (eval new-code)
  25. 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. 26. (defmacro emoify [original-code] (let [function-name (first '(println "Hello, World"))] (list function-name "Goodbye, Cruel World")   (emoify (println "Hello, World"))
  27. 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. 28. FIN Questions?

×