:...">

In Clojure, is a vector a specific hashmap?

On "Clojure programming" is an example of using the get function on a vector:

(get [:a :b :c] 1) -> :b 

I called (doc get), and it looks like the get function accepts hashmap as an argument, not a vector, so I wander if the vector is a kind of hashmap. I remember that hashmap can take an index integer and return a value corresponding to that index, so I did this to see if the vector can do the same:

 ([1 2 3 4] 1) -> 2 

He returned the value 2, which is at index 1 in [1 2 3 4].

Does this mean that a vector is a hashmap whose key-value pair is an index value pair?

+6
source share
1 answer

No, the main implementation is different.

Saying this, since vectors logically map indices into elements, are they associative structures in Clojure and can be used with get , contains? and assoc (although for assoc only indexes from 0 to 1 at the end of the vector can be used). They cannot be used with dissoc , although this is a "real map" operation.

In addition, vectors act differently on cards when used as functions: calling a map as a function is equivalent to using it with get , and calling a vector is equivalent to using nth . The difference is that nth throws an exception in indices outside the bounds (as well as arguments that cannot be indices, such as negative numbers or non-numbers), while get returns nil .

+8
source

Source: https://habr.com/ru/post/946333/


All Articles