Clojure 如何将优先级映射扩展为可还原的
我注意到没有实现reduce(AbstractMethodError,如果您尝试的话)-我该如何扩展它以便它能够实现?这对我来说很有用:Clojure 如何将优先级映射扩展为可还原的,clojure,extend,reduce,Clojure,Extend,Reduce,我注意到没有实现reduce(AbstractMethodError,如果您尝试的话)-我该如何扩展它以便它能够实现?这对我来说很有用: (ns reducing (:use clojure.data.priority-map) (:import (clojure.data.priority_map PersistentPriorityMap))) (extend-type PersistentPriorityMap clojure.core.protocols/CollRe
(ns reducing
(:use clojure.data.priority-map)
(:import (clojure.data.priority_map PersistentPriorityMap)))
(extend-type PersistentPriorityMap
clojure.core.protocols/CollReduce
(coll-reduce
([this f] (reduce f (seq this)))
([this f val] (reduce f (seq this) val))))
(def p (priority-map :a 2 :b 1 :c 3 :d 5 :e 4 :f 3))
(reduce conj [] p)
这是由clojure.core对集合实现的一些隐含假设造成的,据我所知,这些假设在任何地方都没有被编码。具体地说,它假设所有java集合接口都已实现,但实际上并未扩展这些接口。所以,有可能忘记实现其中的一些,然后让事情正常工作,直到得到一些假设它们已经实现的代码
在这种情况下,丢失的接口(或至少其中一个接口)是Iterable:reduce可以处理任何Iterable,而不需要了解更多信息。我将了解如何应用补丁程序使优先级映射实现可移植。
(reduce fn(seq(priority map…)
?这就是如何使用它进行reduce,但我想扩展优先级映射,以便它可以在现有reduce表达式中使用,而不需要他们知道它是一个优先级映射将其标记为可接受的答案,正如我所想的,但amalloy的补丁同样有用