Exercise 9 - runtime polymorphism
This commit is contained in:
parent
3d6d81c63a
commit
bd5f7eadd7
1 changed files with 6 additions and 6 deletions
|
|
@ -7,19 +7,19 @@
|
||||||
"!")))
|
"!")))
|
||||||
|
|
||||||
(defmulti diet (fn [x] (:eater x)))
|
(defmulti diet (fn [x] (:eater x)))
|
||||||
(defmethod diet :herbivore [a] __)
|
(defmethod diet :herbivore [a] (str (:name a) " eats veggies."))
|
||||||
(defmethod diet :carnivore [a] __)
|
(defmethod diet :carnivore [a] (str (a :name) " eats animals."))
|
||||||
(defmethod diet :default [a] __)
|
(defmethod diet :default [a] (str "I don't know what " (:name a) " eats."))
|
||||||
|
|
||||||
(meditations
|
(meditations
|
||||||
"Some functions can be used in different ways - with no arguments"
|
"Some functions can be used in different ways - with no arguments"
|
||||||
(= __ (hello))
|
(= "Hello World!" (hello))
|
||||||
|
|
||||||
"With one argument"
|
"With one argument"
|
||||||
(= __ (hello "world"))
|
(= "Hello, you silly world." (hello "world"))
|
||||||
|
|
||||||
"Or with many arguments"
|
"Or with many arguments"
|
||||||
(= __
|
(= "Hello to this group: Peter, Paul, Mary!"
|
||||||
(hello "Peter" "Paul" "Mary"))
|
(hello "Peter" "Paul" "Mary"))
|
||||||
|
|
||||||
"Multimethods allow more complex dispatching"
|
"Multimethods allow more complex dispatching"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue