tags:

views:

158

answers:

3

How do i allocate a vector with 1042 empty indexes?
Will the storage for it be lazily allocated?
like this

(def a (array-creation-function 1042))
(def b (assoc a 1041 42))
(b 1041) 
--> 42
+4  A: 
(apply vector (take 1024 (repeat nil)))

... is lazy

Andrew
+1  A: 

If you want something that's not lazy but which avoids some overhead, you can do:

(vec (make-array Object 1024))

Note, assoc does not alter a vector, it returns a new vector with one of the values changed. Vectors are immutable. Your code will never work as posted.

Brian Carper
thanks for pointing that out, i edited the question to store the array returned by assoc
Arthur Ulfeldt
`make-array` returns a Java array, but wrapping it in `vec` makes it into a Clojure vector.
Brian Carper
A: 

It seems that vectors are not sparse so you must specify a value for each index when you create the vector. The easiest way seems to be to call (vec ) on a sequence.

(vec (repeat 1042 nil))

These values are not created lazily it seems.

Arthur Ulfeldt
this requires recent version of clojure to provide the 2 argument repeat
Arthur Ulfeldt