Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I create a large vector in clojure

Tags:

clojure

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
like image 675
Arthur Ulfeldt Avatar asked Jul 20 '09 19:07

Arthur Ulfeldt


People also ask

What is a vector in Clojure?

A Vector is a collection of values indexed by contiguous integers. A vector is created by using the vector method in Clojure.

What is a collection in Clojure?

Clojure collections "collect" values into compound values. There are four key Clojure collection types: vectors, lists, sets, and maps. Of those four collection types, vectors and lists are ordered.


1 Answers

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.

like image 86
Arthur Ulfeldt Avatar answered Sep 30 '22 17:09

Arthur Ulfeldt