clojure-koans/src/koans/09_runtime_polymorphism.clj

43 lines
1.2 KiB
Clojure
Raw Normal View History

(ns koan-engine.runner)
(ns koans.09-runtime-polymorphism (:use koan-engine.core))
2010-02-14 00:19:13 +00:00
(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] __)
(defmethod diet :carnivore [a] __)
(defmethod diet :default [a] __)
(meditations
"Some functions can be used in different ways - with no arguments"
(= __ (hello))
"With one argument"
(= __ (hello "world"))
"Or with many arguments"
(= __
(hello "Peter" "Paul" "Mary"))
"Multimethods allow more complex dispatching"
(= "Bambi eats veggies."
(diet {:species "deer" :name "Bambi" :age 1 :eater :herbivore}))
"Animals have different names"
(= "Thumper eats veggies."
(diet {:species "rabbit" :name "Thumper" :age 1 :eater :herbivore}))
2014-01-21 13:14:00 +00:00
"Different methods are used depending on the dispatch function result"
(= "Simba eats animals."
(diet {:species "lion" :name "Simba" :age 1 :eater :carnivore}))
2010-02-14 00:19:13 +00:00
"You may use a default method when no others match"
(= "I don't know what Rich Hickey eats."
(diet {:name "Rich Hickey"})))