1
votes

This is the code I copied from an answer on another post on this website:

(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)
)

when I call (test) in REPL, it shows the following error:

CompilerException java.lang.RuntimeException: Unable to resolve symbol: update in this context

Why is it that Clojure does not know the update function?

2
Check what version of Clojure you are using. update was added in 1.7 - dpassen
If you are on <1.7, you can use update-in (e.g. (update-in row [1] replacements)) - cfrick

2 Answers

1
votes

You get this error if you call update from clojure 1.6 or earlier.

update was added in 1.7, before that you had to use update-in

try (clojure-version) and see what you're using.

(as it says in the comments by cfrick and dpassen)

0
votes

Try restarting the repl. It works when I try it:

(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]]

Another option (my favorite) is to work on such code in a temporary testing namespace (e.g. tst.demo.core), so you have the full editor available, and it is easier to make sure everything is loaded/reloaded correctly.

I also highly recommend the lein-test-refresh plugin for lein.

Another option is to make a new empty directory and start a repl there:

~/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]]

Update

Note that in Clojure the file name & directory structure must match the namespace declaration in each file. Thus a file like ./src/fred/core.clj must have a namespace like fred.core, where ./src is a subdirectory of the main project directory (where project.clj lives).