Clojure: O (1) time to get the size of the vector

Context

(doc count) ------------------------- clojure.core/count ([coll]) Returns the number of items in the collection. (count nil) returns 0. Also works on strings, arrays, and Java Collections and Maps 

Question

Is constant time guaranteed on a vector?

If not, do you still need to get the size of the vector in constant time?

More context

I really want to get a list item from a vector, which I can do in O (1) with nth if I know the size of the vector.

EDIT:

I forgot to mention this. I need this to work with the transition vector.

+6
source share
1 answer

Yes, counting is always done on a vector. But you can just call peek to get your last item.

+8
source

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


All Articles