how can the clojure.algo.generic library be used?
我知道该库https://github.com/clojure/algo.generic提供了实现泛型算术运算符
说明我是否要实现向量加法,等等:
1 2 | > (+ [1 2 3 4 5] 5) ;; => [6 7 8 9 10] |
我该怎么做:
-
用algo.generic定义
+ 运算符 -
使用先前在另一个项目中定义的
+ 运算符?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | (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)) |
编辑2,
1 2 3 4 | user=> (defn + [coll i] (map #(clojure.core/+ % i) coll)) #'user/+ user=> (+ [1 2 3 4 5] 5) (6 7 8 9 10) |
编辑,您也可以执行
1 2 | (in-ns 'algo.generic) (defn + [& args]) |
-编辑-
您应该使用(在此处需要[lib:as namespace])并调用(在此处/ namespace)。下面是出现的问题的代码。
1 2 | user=> (map #(+ % 5) [1 2 3 4 5]) (6 7 8 9 10) |
另外,请签出(以ns为单位)。