Top Banner
CLOJURE WORKSHOP
28

Advanced

Jun 24, 2015

Download

Technology

Logan Campbell

Clojure workshop, advanced end that shows off some higher level features.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Advanced

CLOJURE WORKSHOP

Page 2: Advanced

RECURSION

Page 3: Advanced

(defn length ([collection] (length collection 0)) ([collection accumulator] (if (empty? collection) accumulator (recur (rest collection) (inc accumulator)))))

Page 4: Advanced

(loop [x 10]  (when (> x 1)    (println x)    (recur (- x 2))))

Page 5: Advanced

SEQUENCE PROCESSING

Page 6: Advanced

(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)))

RECURSIVE

Page 7: Advanced

(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)))

PATTERN MATCHING

Page 8: Advanced

(defn  balance  [string]    (-‐>>  string            seq            (map  {\(  inc  \)  dec})            (filter  identity)            (reductions  #(%2  %1)  0)            (filter  neg?)            empty?))

SEQUENCE PROCESSING

Page 9: Advanced

PROTOCOLS

Page 10: Advanced

(defrecord  CartesianCoordinate  [x  y])

Page 11: Advanced

(defrecord  PolarCoordinate  [distance  angle])

Page 12: Advanced

(defprotocol  Moveable    (move-‐north  [self  amount])    (move-‐east    [self  amount]))

Page 13: Advanced

(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))))

Page 14: Advanced

(defrecord  CenterPointRectangle  [center-‐point  width  height])

Page 15: Advanced

(defrecord  CornerPointRectangle  [top-‐left  bottom-‐right])

Page 16: Advanced

(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))))

Page 17: Advanced

MACROS

Page 18: Advanced

'(println "Hello, World")

Page 19: Advanced

=> (println "Hello, World")'(println "Hello, World")

Page 20: Advanced

(first )'(println "Hello, World")

Page 21: Advanced

(first )'(println "Hello, World")=> println

Page 22: Advanced

function-name(def )'(println "Hello, World")(first )

(list function-name "Goodbye, Cruel World")

Page 23: Advanced

function-name(def )'(println "Hello, World")(first )

(list function-name "Goodbye, Cruel World")=> (println "Goodbye, Cruel World")

Page 24: Advanced

function-name(def )

(def new-code ) (eval new-code)

(first )'(println "Hello, World")

(list function-name "Goodbye, Cruel World")

Page 25: Advanced

function-name(def )

(def new-code ) (eval new-code)

(first )'(println "Hello, World")

(list function-name "Goodbye, Cruel World")

prints: “Goodbye, Cruel World”

Page 26: Advanced

(defmacro emoify [original-code] (let [ ]  (emoify (println "Hello, World"))

function-name (first )'(println "Hello, World")(list function-name "Goodbye, Cruel World")

Page 27: Advanced

(defmacro emoify [original-code] (let [ ]  (emoify (println "Hello, World"))

function-name (first )'(println "Hello, World")(list function-name "Goodbye, Cruel World")

prints: “Goodbye, Cruel World”

Page 28: Advanced

FINQuestions?