Created
January 31, 2015 21:46
-
-
Save jdivock/2660f523cdc433bfaaab to your computer and use it in GitHub Desktop.
protocols and such
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Anything you type in here will be executed | |
;; immediately with the results shown on the | |
;; right. | |
(import 'java.util.UUID) | |
(UUID/randomUUID) | |
(defn who-are-you [input] | |
(cond | |
(= java.lang.String (class input)) "String - Who are you?" | |
(= clojure.lang.Keyword (class input)) "Keyword - Who are you??" | |
(= java.lang.Long (class input)) "Number - Who are you?")) | |
(who-are-you :alice) | |
(who-are-you "alice") | |
(who-are-you 1234) | |
(who-are-you nil) | |
(defmulti who-are-you class) | |
(defmethod who-are-you java.lang.String [input] | |
(str "String - who are you? " input)) | |
(defmethod who-are-you clojure.lang.Keyword [input] | |
(str "Keyword - who are you? " input)) | |
(defmethod who-are-you java.lang.Long [input] | |
(str "Number - who are you? " input)) | |
(who-are-you :alice) | |
(who-are-you "Alice") | |
(who-are-you 1234) | |
(who-are-you true) | |
(defmethod who-are-you :default [input] | |
(str "I don't know - who are you? " input)) | |
(who-are-you true) | |
;; eat-mushroom example | |
(defmulti eat-mushroom (fn [height] | |
(if (< height 3) | |
:grow | |
:shrink))) | |
(defmethod eat-mushroom :grow [_] | |
(str "stup, I'm growing")) | |
(defmethod eat-mushroom :shrink [_] | |
(str "sup, I'm shinkring")) | |
(eat-mushroom 3) | |
;; more polymorphism | |
;; protocols | |
(defprotocol BigMushroom | |
(eat-mushroom [this])) | |
(extend-protocol BigMushroom | |
java.lang.String | |
(eat-mushroom [this] | |
(str (.toUpperCase this) " mmm tasty!")) | |
clojure.lang.Keyword | |
(eat-mushroom [this] | |
(case this | |
:grow "Eat the right side!" | |
:shrink "Eat the left side!")) | |
java.lang.Long | |
(eat-mushroom [this] | |
(if (< this 3) | |
"Eat the right side to grow" | |
"Eat the left side to shrink"))) | |
(eat-mushroom 3) | |
(eat-mushroom :shrink) | |
(eat-mushroom "burp") | |
(eat-mushroom true) | |
;; defrecord | |
(defrecord Mushroom [color height]) | |
(def regular-mushroom (Mushroom. "white and blue polda dots" "2 inches")) | |
(class regular-mushroom) | |
(.-color regular-mushroom) | |
(.-height regular-mushroom) | |
(defprotocol Edible | |
(bite-right-side [this]) | |
(bite-left-side [this])) | |
(defrecord WonderlandMushroom [color height] | |
Edible | |
(bite-right-side [this] | |
(str "The " color " bite makes ou grow bigger")) | |
(bite-left-side [this] | |
(str "The " color " bite makes you grow smaller"))) | |
(defrecord RegularMushroom [color height] | |
Edible | |
(bite-right-side [this] | |
(str "The " color " tastes bad")) | |
(bite-left-side [this] | |
(str "The " color " tastes bad too"))) | |
(def alice-mushroom (WonderlandMushroom. "blue dots" "3 inches")) | |
(def reg-mushroom (RegularMushroom. "red dots" "2 inches")) | |
(bite-right-side alice-mushroom) | |
(bite-right-side reg-mushroom) | |
;; deftype example | |
(defprotocol Edible | |
(bite-right-side [this]) | |
(bite-left-side [this])) | |
(deftype WonderlandMushroom [] | |
Edible | |
(bite-right-side [this] | |
(str "The bite makes you grow bigger")) | |
(bite-left-side [this] | |
(str "The bite makes you grow smaller"))) | |
(deftype RegularMushroom [] | |
Edible | |
(bite-right-side [this] | |
(str "Tastes crummy")) | |
(bite-left-side [this] | |
(str "Tastes crummy too"))) | |
(def won-mush (WonderlandMushroom. )) | |
(def reg-mush (RegularMushroom.)) | |
(.-bite-left-side won-mush) | |
(.-bite-right-side reg-mush) | |
;; implementing protol-esque features with fns and maps | |
(defn bite-right-side [mushroom] | |
(if (= (:type mushroom) "wonderland") | |
"The bite makes you grow bigger" | |
"The bite tastes bad")) | |
(defn bite-left-side [mushroom] | |
(if (= (:type mushroom) "wonderland") | |
"The bite makes you shrink smaller" | |
"The bite also tastes bad")) | |
(bite-right-side {:type "wonderland"}) | |
(bite-left-side {:type "regular"}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment