Is (vec #{1 2 3}) guaranteed to always return [1 3 2] or could the order be different?
I am not so much interested in the implementation details behind this but going from unordered to ordered in general in order to keep my functions pure and easily testable.
As mentioned, standard
#{}sets (bothPersistentArrayMapandPersistentHashMap; depending on the size) are considered unordered.Regarding purity with respect to calling
seqon a set though, the current implementation does seem to return a well-defined, consistent order; just not an easily predictable one:So yes, it seems that within a single run of a program, the order of
(seq #{1 2 3})can be relied upon, and can be considered pure. The language gives no guarantees though, and this property may not always exist, so really, I wouldn't rely on it. It's an implementation detail.If you require a consistent ordering, it may be beneficial to have a vector along with the set to define the order. You could do something like:
Reference the set when you want to do a membership test, and the vector when you need order. Of course, this requires twice as much memory as it otherwise would though, so this may not always be practical. Additions to both sets and vectors are essentially constant however, so additions will still be quick.