less than 1 minute read

Code

;; complex_multi_method_processing.clj

(defn arg-type [arg]
  (cond
    (string? arg) :string
    (number? arg) :number
    (fn? arg) :function
    (instance? java.time.LocalDate arg) :date
    :else :other))

(defn dispatcher [args]
  (map arg-type args))

(defmulti process-args (fn [& args] (dispatcher args)))

(defmethod process-args '(:number) [& args]
  '(:number))

(defmethod process-args '(:string :string) [& args]
  '(:string :string))

(defmethod process-args '(:number :number) [& args]
  '(:number :number))

(defmethod process-args '(:number :function :function) [& args]
  '(:number :function :function))

(defmethod process-args '(:number :number :function :function) [& args]
  '(:number :number :function :function))

(defmethod process-args '(:string :function :function) [& args]
  '(:string :function :function))

(defmethod process-args '(:string :string :function :function) [& args]
  '(:string :string :function :function))

(defmethod process-args '(:date :function :function) [& args]
  '(:date :function :function))

(defmethod process-args '(:date :date :function :function) [& args]
  '(:date :date :function :function))

(defmethod process-args '(:other :function :function) [& args]
  '(:other :function :function))

(defmethod process-args '(:other :other :function :function) [& args]
  '(:other :other :function :function))

(defmethod process-args :default [& args]
  (str "Many arguments (" (count args) "): " (clojure.string/join ", " args)))

;; (defmethod process-args '(:default :function :function) [& args] ;; I thought this would work like magic
;;   "Anything and function argument provided")

Notes

Updated: