1

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?

Omri Bernstein
  • 1,893
  • 1
  • 13
  • 17
Will
  • 53
  • 6
  • 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 May 20 '14 at 22:31
  • 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 May 20 '14 at 22:31
  • Seems to be a Light Table problem. – Will May 20 '14 at 22:32
  • Ah, well I can't help you there :/ I don't use Light Table. – Omri Bernstein May 20 '14 at 22:34
  • Asking Light Table to evaluate the entire module from scratch appears to have solved the problem. – Will May 20 '14 at 23:04

2 Answers2

2

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"]
Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
  • Wrong issue, though I'm glad to know about (mapv). The problem isn't whether I'm getting a seq or a vector, it's that I wasn't getting the strings back, but the keywords. – Will May 20 '14 at 22:30
2

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.

amalloy
  • 89,153
  • 8
  • 140
  • 205