跳转到内容

Clojure 编程/示例/API 示例/多方法

来自 Wikibooks,开放世界中的开放书籍

此页面列出了使用 Clojure API 创建多方法的示例。

(defmulti area :Shape)
(defn rect [wd ht] {:Shape :Rect :wd wd :ht ht})
(defn circle [radius] {:Shape :Circle :radius radius})
(defmethod area :Rect [r]
    (* (:wd r) (:ht r)))
(defmethod area :Circle [c]
    (* (. Math PI) (* (:radius c) (:radius c))))
(defmethod area :default [x] :oops)
(def r (rect 4 13))
(def c (circle 12))
(area r)
-> 52
(area c)
-> 452.3893421169302
(area {})
-> :oops

这是一个在不使用层次结构的情况下使用多方法的示例

(defmulti rand-str 
  (fn [] (> (rand) 0.5)))

(defmethod rand-str true
  [] "true")

(defmethod rand-str false
  [] "false")

(for [x (range 5)] (rand-str))
-> ("false" "false" "true" "true" "false")


defmethod

[编辑 | 编辑源代码]
(defmulti fib int) 
(defmethod fib 0 [_] 1) 
(defmethod fib 1 [_] 1) 
(defmethod fib :default [n] (+ (fib (- n 2)) (fib (- n 1)))) 
user=> (map fib (range 10)) 
(1 1 2 3 5 8 13 21 34 55)

另一个示例,其中defmulti 接受参数并在调用defmethod之前对其进行评估

(defmulti multi-fun
  (fn [x y] (+ x y)))
(defmethod multi-fun 3 [x y]
  (print "Specialisation 3"))
(defmethod multi-fun :default [x y]
  (print "Generic " (+ x y)))
user=> (multi-fun 1 2)
Specialisation 3
user=> (multi-fun 3 4)
Generic 7
华夏公益教科书