-
Notifications
You must be signed in to change notification settings - Fork 182
Data.Set: add insertUnlessMember #161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Might be worth checking the performance gain. Lookups are usually much faster than inserts so a lookup + insert is often as fast as just an insert. |
Yes, maybe, but why then has |
Someone probably thought it was faster (perhaps it is!), but I doubt it was tested. |
We could add it for consistency, but I would at least like to know if it actually helps. Our API suffers from a combinatoric explosion unfortunately, so I'd like to avoid growing it if not necessary. |
When comparison is expensive, the combined approach obviously wins. Obviously, that's not the best scenario for |
@treeowl right, it can in theory be arbitrarily expensive. In the typical case however the runtime is dominated by the allocation done on insert. It's a typical memory bound problem (like most performance problems on modern CPUs). |
Personally I am not sure we can make the implementation faster than the suggested one, notably when comparison is cheap like in the Together with the fact that the proposed function is a simple combination of existing functions and is at most twice slower (assuming lookup complexity is dominated by insert complexity), I am not a big fan. But please do not hesitate to surprise me with benchmark results :-) |
This is very similar to #291, #166, #290 which propose: lookupEntry :: (Ord a) => a -> Set a -> Maybe a
lookupEntry :: (Ord k) => k -> Map k v -> Maybe (k, v) Would these functions address your use case? These have already (mostly*) been approved by the libraries committee and have the potential of going into the API soon. * There was general agreement, but the discussion was a while ago so I'm going to raise it again to make sure everyone is on board still. |
No, I think they go together.
…On Dec 26, 2017 1:48 PM, "Matt Renaud" ***@***.***> wrote:
This is very similar to #291
<#291>, #166
<#166>, #290
<#290> which propose:
lookupEntry :: (Ord a) => a -> Set a -> Maybe alookupEntry :: (Ord k) => k -> Map k v -> Maybe (k, v)
Would these functions address your use case? These have already (mostly*)
been approved by the libraries committee and have the potential of going
into the API soon.
* *There was general agreement, but the discussion was a while ago so I'm
going to raise it again to make sure everyone is on board still.*
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#161 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABzi_d-gty8m_pwPrCJ5aK3J27zlqkMVks5tET-EgaJpZM4Fcx-Z>
.
|
Oh yeah, duh, I read this wrong. Sorry for the noise. |
With the insertUnlessMember = alterF (\member_ -> if member_ then Nothing else Just True) I'll tentatively close this issue, but feel free to reopen, if there's more to do here. |
For
Data.Set
, I am missing something comparable toMap.lookupInsertWithKey
that combines alookup
and aninsert
. Suggestion (specification):Of course, that should have an efficient implementation that only traverses the tree once.
The text was updated successfully, but these errors were encountered: