1

(第一个 ["a" "b" "c"])-> "c"

我期望的地方:(first ["a" "b" "c"]) -> "a"

我想我一定在这里误解了一些东西,任何帮助表示赞赏!

此致。


(defn binnd-to-name [name-string to-bind]
  (bind-to-name name-string to-bind))

(defmacro bind-to-name [name-string stuff-to-bind]
  `(def ~(symbol name-string) ~stuff-to-bind))

(defn bind-services [list-of-services]
  (if (empty? list-of-services)
    nil
    (do
      (binnd-to-name (first (first list-of-services)) (last (first list-of-services)))
        (bind-services (rest list-of-services)))))

(bind-services [["*my-service*" se.foo.bar.service.ExampleService]])

ExampleService 是类路径上的一个 Java 类,我想将它绑定到符号my-service。这个想法是遍历名称-值对列表并将每个名称绑定到值。它没有按预期工作。

所以不知何故,在这段代码中,某些东西显然被评估为“def first last”。

4

2 回答 2

3

问题是你的宏没有像你期望的那样扩展

(defmacro bind-to-name [name-string stuff-to-bind]
  `(def ~(symbol name-string) ~stuff-to-bind))

(defmacro bind-services [services]
  `(do
     ~@(for [s services]
         `(bind-to-name ~(first s) ~(second s)))))

(bind-services [["*my-service*" se.foo.bar.service.ExampleService]])

如果您尝试这种方法,您的def symbol序列将正确扩展。

于 2013-10-20T19:38:23.873 回答
2

没门!

user=> (doc first)
-------------------------
clojure.core/first
([coll])
  Returns the first item in the collection. Calls seq on its
      argument. If coll is nil, returns nil.

user=> (first ["a" "b" "c"])
"a"
于 2013-10-20T18:22:26.843 回答