我知道库https://github.com/clojure/algo.generic提供了实现泛型算术运算符的方法,+ * / -但在那里我找不到如何创建它们的简单示例,以及如何将其用作库.
说如果我想实现向量加法等:
> (+ [1 2 3 4 5] 5)
;; => [6 7 8 9 10]
Run Code Online (Sandbox Code Playgroud)
我该怎么做:
+用algo.generic 定义运算符+之前在另一个项目中定义的运算符?(ns your.custom.operators
(:import
clojure.lang.IPersistentVector)
(:require
[clojure.algo.generic.arithmetic :as generic]))
(defmethod generic/+
[IPersistentVector Long]
[v x]
(mapv + v (repeat x)))
(ns your.consumer.project
(:refer-clojure :exclude (+))
(:use
[clojure.algo.generic.arithmetic :only (+)])
(:require
your.custom.operators))
(defn add-five
[v]
(+ v 5))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
488 次 |
| 最近记录: |