(首先[“a”“b”“c”]) - > “C”
我期望的地方: (第一个[“a”“b”“c”]) - > “一”
我想我一定在这里误解了一些东西,感谢任何帮助!
最诚挚的问候。
(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”。
答案 0 :(得分: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
序列将正确扩展。
答案 1 :(得分: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"