Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a clojure array-map to maintain insertion order after assoc?

I have an array-map which I am associng some values into it. After a certain size the returned value is a PersistentHashMap rather than the original PersistentArrayMap. I've read about this behavior on a few web sites. Is there any way to force the insertion order even after assoc?

I do have a separate function which will take a ash-map and a vector of keys, and return a "fresh" array-map with keys in this order, but it means that for each assoc, I have to extract the keys first, cons/conj the new key to the vector, then create a new array-map. Seems kludgey, even if written in a separate function.

Is there a more direct language-supported way of keeping insertion order even on large-ish (>10, but < 50) keys array-map?

In case it's relevant, I'm using a list of array-maps as data into an incanter dataset and then outputting to excel. The save-xls function keeps the order of the keys/columns.

Thanks

like image 284
Sonicsmooth Avatar asked Aug 20 '12 08:08

Sonicsmooth


1 Answers

You can use an ordered map: https://github.com/flatland/ordered

like image 78
amalloy Avatar answered Oct 17 '22 18:10

amalloy