在我对Clojure For Comprehension 示例的回答中,我想删除一些重复项:
(def all-letters (map char (range 65 90)))
(defn kw [& args] (keyword (apply str args)))
(concat
(for [l all-letters] (kw l))
(for [l all-letters l2 all-letters] (kw l l2))
(for [l all-letters l2 all-letters l3 all-letters] (kw l l2 l3)))
我想删除“for”重复。我写了以下宏:
(defmacro combine [times]
(let [symbols (repeatedly times gensym)
for-params (vec (interleave symbols (repeat 'all-letters)))]
`(for ~for-params (kw ~@symbols))))
适用于:
(concat (combine 1) (combine 2) (combine 3))
但如果我尝试:
(for [i (range 1 4)] (combine i))
我得到:
CompilerException java.lang.ClassCastException: clojure.lang.Symbol cannot be cast to java.lang.Number, compiling:(NO_SOURCE_PATH:177)
到底是怎么回事?有没有更好的方法来删除重复?