clojure-koans/10_runtime_polymorphism.clj

27 lines
808 B
Clojure
Raw Normal View History

; "Some functions can be used in different ways - with no arguments"
2010-02-14 00:19:13 +00:00
(= __ (hello))
; "With one argument"
2010-02-14 00:19:13 +00:00
(= __ (hello "world"))
; "Or with many arguments"
2010-02-14 00:19:13 +00:00
(= __
(hello "Peter" "Paul" "Mary"))
; "Multimethods allow more complex dispatching"
2010-02-14 00:19:13 +00:00
(= "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}))
; "Different methods are used depending on the dispatch function result"
2014-01-21 13:14:00 +00:00
(= "Simba eats animals."
(diet {:species "lion" :name "Simba" :age 1 :eater :carnivore}))
; "You may use a default method when no others match"
2010-02-14 00:19:13 +00:00
(= "I don't know what Rich Hickey eats."
(diet {:name "Rich Hickey"}))