clojure

2022/01/05

clojure programming language

multimethod

같은 함수 이름, 인자에 따라 다르게 동작하도록 할 때

(defmulti greeting
  (fn[x] (x "language")))

(defmethod greeting "English" [params]
 "Hello!")

(defmethod greeting "French" [params]
 "Bonjour!")

(def english-map {"id" "1", "language" "English"})
(def french-map {"id" "2", "language" "French"})

(greeting english-map)
;; => "Hello!"
(greeting french-map)
;; => "Bounjour!"