1
votes

I've got a map from keywords to compass direction strings:

(def dirnames {:n "North", :s "South", :e "East", :w "West"})

I can look up names using the map as a function:

(dirnames :n)
;# = "North"

It seems to me that

(map dirnames [:n :s])

ought to return the vector

["North" "South"]

but it returns

[:n :s]

instead. I've tried this half a dozen ways, supplying different functions in place of "dirnames" in the (map) call, and I always get the vector of keywords back.

Clearly I'm missing something basic. What is it?

2
Which Clojure version(s)? I've only tried in 1.4.0 and 1.5.1, and both of those do what you want, i.e. (map dirnames [:n :s]) evaluates to ("North" "South"). Or replace map with mapv and you get ["North" "South"]. - Omri Bernstein
I've been working this in Light Table; and I'm seeing the results above in Light Table's "InstaREPL". I'm seeing the correct behavior if I run the REPL as "java -cp clojure.jar clojure.main". - Will
Seems to be a Light Table problem. - Will
Ah, well I can't help you there :/ I don't use Light Table. - Omri Bernstein
Asking Light Table to evaluate the entire module from scratch appears to have solved the problem. - Will

2 Answers

2
votes

Works for me, am i misinterpreting the question:

user> (def dirnames {:n "North", :s "South", :e "East", :w "West"})\
#'user/dirnames

user> (map dirnames [:n :s])
("North" "South")

also:

user> (map #(dirnames %) [:n :s])
("North" "South")
user> (mapv #(dirnames %) [:n :s])
["North" "South"]
2
votes

I bet you forgot some parens. Consider this function definition:

(defn foo [dirnames]
  map dirnames [:n :s])

It looks almost right, but it evaluates map for side effects, then dirnames for side effects (both of those do nothing), and then finally returns [:n :s]. That's the only reasonable explanation I can think of for behavior like what you're describing.