Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clojure: O(1) time to get size of vector

Tags:

clojure

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 count guaranteed to be constant time on vector?

If not, is there anyway to get the size of a vector in constant time?

More Context

I actually want to get the list element of a vector, which I can do in O(1) with nth, assuming I know the vector size.

EDIT:

I forgot to mention this. I need this to work on a transient vector.

like image 638
user1383359 Avatar asked Jun 09 '12 08:06

user1383359


1 Answers

Yes, count is always fast on a vector. But you can just call peek to get its last element.

like image 167
amalloy Avatar answered Oct 05 '22 00:10

amalloy