假设我写了一个函数:
(defn foo [to x] (conj to x))
我想通过说明 to
必须实现某些协议(例如结构/类型中必须支持调用 conj
)来记录它。是否有一个网站或数据库有这个信息?显然,我想将这个问题概括为“在哪里可以找到所有Clojure协议的完整参考资料?”
使用Sam Estep的建议作为清晰和具体的例子,它会看起来像:
(defn invert-many-to-one
"returns a one-to-many mapping where vals are collections of type `(constructor-fn)`,
(defaults to `hash-set`). Note that `constructor-fn` is a function of 0 args.
`insert-fn` function can be passed. if only `constructor-fn` is passed
then `insert-fn` defaults to `conj` and `(constructor-fn)` must be an instance
of `clojure.lang.IPersistentCollection`"
([m] (invert-many-to-one hash-set conj m))
([constructor-fn m] {:pre [(instance? clojure.lang.IPersistentCollection (constructor-fn))]}
(invert-many-to-one constructor-fn conj m))
([constructor-fn insert-fn m]
(persistent!
(reduce (fn [m [k v]]
(assoc! m v (insert-fn (clojure.core/get m v (constructor-fn)) k)))
(transient {}) m))))