作为多方法的一部分修改参数

Modifying parameters as part of a multimethod

编写一个在调度过程中修改传入参数的多方法的惯用方法是什么?

在这种情况下,我想删除其中一个参数:

(defmulti do-update ???)

(defmethod do-update :set-cursor [state x y]
  (assoc state :cursor [x y]))

(defn post [action & args]
  (swap! some-state do-update action args))

(post :set-cursor 0 0)

此处的 dispatch-fn 将负责读取 action 关键字 转发 (cons state args) 作为方法的参数。

此方法的替代方法是创建调度图。

(defn set-cursor [state x y])

(def handlers
  {:set-cursor set-cursor})

(defn dispatch [action & args]
  (let [handler (action handlers)]
    (apply swap! some-state handler args)))

但是如果没有地图,自动注册这些处理程序以针对其相应的操作会很好。

你不能直接询问方法是哪个调度函数的输入导致它选择调用这个方法,这没关系,因为调度方法只是函数!

您可以从派发到的方法中再次调用派发函数。只要你的调度函数是一个纯函数,这就是可靠的。如果不是...

因此,如果您这样定义 yoru dipatch 函数 ???(是的 ??? 是一个有效的函数名称)

user=> (defn ??? [x] (inc x))
#'user/???

你的多重方法是这样的:

user=> (defmulti foo ???)

然后在方法中再次调用dispatch函数:

 (defmethod foo 1 [x] 
    (str "I was dispatched from " x 
         " and i will use " (??? x) 
         " to do my good work"))

您可以重构调度函数的动作,而无需维护一个独立的 function/map/thing。

user=> (foo 0)
"I was dispatched from 0 and i will use 1 to do my good work"

虽然核心语言不支持这一点,但您可以使用一些 "rewriting" 宏来分层您想要的语义:

(defmacro defupdatemulti [name] 
  `(defmulti ~name (fn [state# action# & args#] action#)))

(defmacro defupdatemethod [name action [state & args] & body] 
  `(defmethod ~name ~action 
    [~state _# [~@args]] 
    ~@body))

并使用这个:

(defupdatemulti do-update)

(defupdatemethod do-update :set-cursor [state x y] 
  (assoc state :cursor [x y]))

有了这个

(def some-state (atom {}))

(defn post [action & args]
  (swap! some-state do-update action args))

(post :set-cursor 0 0)

产生以下结果:

{:cursor [0 0]}

注意:虽然上述有效,但肯定不是惯用的。

多重方法设计的一部分是方法接收与分派函数相同的参数。如果方法对仅用于分派的某些参数不感兴趣,那么在方法实现中忽略它们是完全没问题的——一点也不奇怪:

(def some-state (atom {:cursor [-1 -1]}))

(defmulti do-update
  (fn [state action x y]
    action))

;; ignoring the action argument here:
(defmethod do-update :set-cursor [state _ x y]
  (assoc state :cursor [x y]))

;; added apply here to avoid ArityException in the call to do-update:
(defn post [action & args]
  (apply swap! some-state do-update action args))

(post :set-cursor 0 0)

@some-state
;= {:cursor [0 0]}

请注意,dispatch 参数在这里排在第二位,以便于将 do-updateswap! 一起使用。