Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
36 lines (28 sloc) 1.2 KB
(defn hello
([] "Hello World!")
([a] (str "Hello, you silly " a "."))
([a & more] (str "Hello to this group: "
(apply str
(interpose ", " (concat (list a) more)))
"!")))
(defmulti diet (fn [x] (:eater x)))
(defmethod diet :herbivore [a] (str (:name a) " eats veggies."))
(defmethod diet :carnivore [a] (str (:name a) " eats animals."))
(defmethod diet :default [a] (str "I don't know what " (:name a) " eats."))
(meditations
"Some functions can be used in different ways - with no arguments"
(= "Hello World!" (hello))
"With one argument"
(= "Hello, you silly world." (hello "world"))
"Or with many arguments"
(= "Hello to this group: Peter, Paul, Mary!"
(hello "Peter" "Paul" "Mary"))
"Multimethods allow more complex dispatching"
(= "Bambi eats veggies."
(diet {:species "deer" :name "Bambi" :age 1 :eater :herbivore}))
"Different methods are used depending on the dispatch function result"
(= "Simba eats animals."
(diet {:species "lion" :name "Simba" :age 1 :eater :carnivore}))
"You may use a default method when no others match"
(= "I don't know what Rich Hickey eats."
(diet {:name "Rich Hickey"})))