这是我在本网站上另一篇文章的答案中复制的代码:
(def data [[1 1 1 3] [2 2 2 3] [3 2 1 1] [4 3 3 4]])
(def replacements {1 "joe" 2 "fred" 3 "martha"})
(defn test
[]
(mapv (fn [row] (update row 1 replacements)) data)
)
当我在REPL中调用(test)时,它显示以下错误:
CompilerException java.lang.RuntimeException:无法在此上下文中解析符号:update
为什么Clojure不知道更新功能?
答案 0 :(得分:0)
尝试重新启动repl。它在我尝试时起作用:
(def data [[1 1 1 3] [2 2 2 3] [3 2 1 1] [4 3 3 4]])
(def replacements {1 "joe" 2 "fred" 3 "martha"})
(defn test []
(mapv (fn [row] (update row 1 replacements)) data) )
(test) => [[1 "joe" 1 3] [2 "fred" 2 3] [3 "fred" 1 1] [4 "martha" 3 4]]
另一个选项(我最喜欢的)是在临时测试命名空间(例如tst.demo.core
)中处理此类代码,因此您可以使用完整的编辑器,并且更容易确保正确加载/重新加载所有内容。
我还强烈推荐lein-test-refresh
plugin for lein.
另一个选择是创建一个新的空目录并在那里开始一个repl:
~/expr > mkdir sally
~/expr > cd sally
~/expr/sally > lein repl
user=> (def data [[1 1 1 3] [2 2 2 3] [3 2 1 1] [4 3 3 4]])
#'user/data
user=> (def replacements {1 "joe" 2 "fred" 3 "martha"})
#'user/replacements
user=> (defn test []
#_=> (mapv (fn [row] (update row 1 replacements)) data) )
WARNING: test already refers to: #'clojure.core/test in namespace: user, being replaced by: #'user/test
#'user/test
user=> (test)
[[1 "joe" 1 3] [2 "fred" 2 3] [3 "fred" 1 1] [4 "martha" 3 4]]
<强> 更新 强>
请注意,在Clojure中,文件名称为&amp;目录结构 必须 匹配每个文件中的名称空间声明。因此,./src/fred/core.clj
等文件必须 具有fred.core
这样的命名空间,其中./src
是主项目目录的子目录(其中) project.clj
生命)。
答案 1 :(得分:0)
如果从clojure 1.6或更早版本调用update
,则会收到此错误。
update
是在1.7中添加的,在此之前您必须使用update-in
尝试(clojure-version)
,看看您正在使用什么。
(如cfrick和dpassen在评论中所说)