How to create a big vector in clojure

How to select a vector with 1042 empty indices?
Will the storage be lazily allocated for it?
like this

(def a (array-creation-function 1042)) (def b (assoc a 1041 42)) (b 1041) --> 42 
+6
clojure
source share
4 answers

It seems that the vectors are not sparse, so you should specify a value for each index when creating the vector. The easiest way is to invoke the (vec) sequence.

 (vec (repeat 1042 nil)) 

These values ​​are not created lazily.

+14
source share

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

 (vec (make-array Object 1024)) 

Note. assoc does not change the vector, it returns a new vector with the changed value. Vectors are immutable. Your code will never work as published.

+5
source share

If your data is sparse, then consider using a blank card instead of a vector .... then you will get an unlimited number of lazily allocated free indexes for free!

 (def a {}) (def b (assoc a 1041 42)) (b 1041) --> 42 
+5
source share
 (apply vector (take 1024 (repeat nil))) 

... lazy

+4
source share

All Articles