Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Efficient access to entry for singleton Map #673
Efficient Access to Entry for Singleton Map
I propose a function is added to the Map module that can directly access the MapOne value from the tree. Something like this:
Currently, to test and access single-entry maps has a lot of unnecessary overhead, e.g. we create a seq then test whether it's empty, take Seq.head, then test also whether the tail is empty.
Why would we test for single-entry maps? Well, my current use case is to maintain invariant structure of a radix tree, where I'm currently using a Map to represent the set of children. Upon removing a child, I need to know whether I have a singleton (no longer a split point in the trie). If so, I may need to recombine tree nodes. But I'm certain there are other use cases, much as we frequently test for single-entry lists.
Pros and Cons
Adding this function would improve performance for a niche use case. It won't break existing code, and the implementation is trivial. The only con I can think of is adding a little to the documentation.
Estimated cost (XS, S, M, L, XL, XXL): XS
Related suggestions: Maybe add to Set as well.
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
If #625 (target typed collection expression) were implemented, then you could use
let tryMapOne (m:Map<'K,'V>) : ('K * 'V) option = match (m.tree) with | [ r ] -> Some r | _ -> None
and the compiler would (hopefully) be smart enough to NOT construct a throwaway map just to compare it but instead directly check