Created
January 29, 2012 07:04
-
-
Save tolitius/1697620 to your computer and use it in GitHub Desktop.
clojure bootcamp: "defmulti" example
This file contains hidden or 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
;; inspired by suggestion in THE doc: http://java.ociweb.com/mark/clojure/article.html | |
(ns bootcamp.multi-method) | |
(defn measure-it [size] | |
(cond | |
(< size 3) :small | |
(< size 6) :medium | |
(< size 12) :large | |
:else :hard-to-measure )) | |
(defmulti price-it measure-it) | |
(defmethod price-it :small [size] (str "we charge $1 for the size " size)) | |
(defmethod price-it :medium [size] (str "we charge $2 for the size " size)) | |
(defmethod price-it :large [size] (str "we charge $3 for the size " size)) | |
(defmethod price-it :hard-to-measure [size] (str "size '" size "' is priceless")) |
This file contains hidden or 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
(ns bootcamp.test.multi-method | |
(:use [bootcamp.multi-method]) | |
(:use [clojure.test])) | |
(deftest price-sizes-of-all-ranges | |
(is (= (map price-it (range 0 13 4)) | |
'("we charge $1 for the size 0" | |
"we charge $2 for the size 4" | |
"we charge $3 for the size 8" | |
"size '12' is priceless")))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment