The past week or so I started helping out a bit with the implementation
of clojure.core.Vec, a vector implementation that works on primitives.
Although I am not using the code in any project of mine, it has occurred
to me that there may be ways of making it easier to use. Before opening
bugs on Assembla, I hope to get some feedback from others as to what
ideas are good or bad.
In particular, I have two different ideas, one for making it easier to
get data into a Vec, and another for getting data back out of it.
Enhancing (vector-of …)
=======================
The current implementation only allows creating an empty Vec using
(vector-of :type). For example, to create a new int Vec, you use (vector-of
:int). You can easily get information into a Vec using (into), but it
strikes me that it would be easier if (vector-of) could work like (vec)
and (vector). For example, all of the following forms would create
equivalent int Vecs:
(vector-of :int '(1 2 3))
(vector-of :int (range 1 3))
(vector-of :int 1 2 3)
(vector-of :int (int-array 1 2 3))
I think this last form may be particularly useful since I am guessing a
potential use case for Vecs would likely include working with a source
of an array of primitives.
Getting an array out of a Vec
=============================
On the other side, there is the question of how to get an array back
out. The most direct way is to use the method 'arrayFor', which
requires an index, as in (.arrayFor myvec 0). The main downside to this
is that it is a bit 'hosty'.
There are other methods, such as using (into-array Integer/TYPE myvec)
or (int-array myvec). However, I think it would be nice if (into-array)
automatically returned an array of primitives. I think this could be
achieved by doing an (instance? clojure.cor.Vec) and using (.arrayFor).
In any case, I welcome any feedback as to whether these are good or
desired ideas, or if there is something I have missed or have not
considered. I can then open tickets and submit the patches to Assembla.
Sincerely,
Daniel Solano Gómez