Description
Currently, it seems there is no support for sorted collections. Understandably, it was not a top priority since there are not that common.
They are not common but when needed, they become invaluable. Furthermore, since they are part of Clojure/script, it makes sense opening a discussion. I did not find anything, any issue on the matter. If you did consider supporting them and retracted for some reason, it would be useful providing a very brief summary here to at least have one issue documenting the process.
A few points:
-
It is usually disastrous when algorithms meant to operate on sorted collections blindly operate on unsorted ones (eg. a mistake, a bug, something escaped validation...)
-
Malli strives to be as serializable as possible. This can be problematic when using
sorted-map-by
andsorted-set-by
which accept a custom sorting function. Using SCI in any way is not an option for performance reasons. However being serializable is not mandatory hence this is a minor problem. Furthermore, from experience, defaultsorted-map
andsorted-set
are often sufficient in most cases and those sorted default collections can be readily serializable. -
Using custom sorted collections (eg.
sorted-map-by
) is akin to opening Malli to supporting any custom collection. A gateway to chaos? -
Spec kind of eschews serialization but it almost solves this very simply by having an
:into
option ins/every
and friends. Something like:
[:map-of
{:into (sorted-map)}
:int
:string]
- Example in Point 3 works for generation but not validation. Surprisingly, validation is tricky. You can check if a collection is sorted using
sorted?
but you never now if it is sorted as intended, returning back to Point 1. I don't think the sorting function can be reliably retrieved in both Clojure and CLJS. Users will have to resort to an alternative scheme such as adding a type in metadata if validation of custom sorted collections is needed.