Skip to content

Add support for sorted collections #413

Open
@helins

Description

@helins

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:

  1. 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...)

  2. Malli strives to be as serializable as possible. This can be problematic when using sorted-map-by and sorted-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, default sorted-map and sorted-set are often sufficient in most cases and those sorted default collections can be readily serializable.

  3. Using custom sorted collections (eg. sorted-map-by) is akin to opening Malli to supporting any custom collection. A gateway to chaos?

  4. Spec kind of eschews serialization but it almost solves this very simply by having an :into option in s/every and friends. Something like:

[:map-of
 {:into (sorted-map)}
 :int
 :string]
  1. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions