Skip to content

Instantly share code, notes, and snippets.

@tolitius
Created January 29, 2012 07:04
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tolitius/1697620 to your computer and use it in GitHub Desktop.
Save tolitius/1697620 to your computer and use it in GitHub Desktop.
clojure bootcamp: "defmulti" example
;; 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"))
(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