In clojure you can use both maps and keys as look up functions hence
({:a 1 :b 2} :a)
and (:a {:a 1 :b 2})
are both viable lookup functions.
Why then can you use a map as a lookup function for a compound-key but not the other way around?
This means ({[:compound :mebaby] 1} [:compound :mebaby]})
will return 1
, but ([:compound :mebaby] {[:compound :mebaby] 1})
will throw an error.
Keywords implement
IFn
as one of their features to make them convenient to use as keys. The fn they implement is a lookup for themselves in an associative structure argument. This is not the case for collections like your vector because they implementIFn
to do lookup in themselves at the argument key.So
({[:compound :mebaby] 1} [:compound :mebaby]})
asks the map what the value is for the key[:compound :mebaby]
, which exists. But([:compound :mebaby] {[:compound :mebaby] 1})
asks the vector what the value at the index{[:compound :mebaby] 1}
is. That's not an integral number, so it can't be an index/key in a vector and throws an error.