-
Notifications
You must be signed in to change notification settings - Fork 182
Consider adding applicative/monadic versions of adjust/update/alter functions #278
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
I've added |
I just realized we can include |
Thanks for alterF, that's exactly what I needed 👍 |
Neat, I think The main motivator was deleting an element and looking it up at the same time: deleteLookup :: Ord k => k -> Map k a -> (Maybe a, Map k a)
deleteLookup = focus (, Remove)
deleteLookup :: Ord k => k -> Map k a -> (Maybe a, Map k a)
deleteLookup = alterF (, Nothing) However, I wouldn't exactly say this is obvious, especially to a newer Haskeller. Perhaps the documentation could be improved around this function. |
I'm always open to documentation suggestions and pull requests, and I think it does make sense to add an explanation of how to use |
I don't think a
Deleting a value and looking it up at the same time: deleteLookup :: Ord k => k -> Map k a -> (Maybe a, Map k a)
deleteLookup = alterF (, Nothing) Deleting a value and returning whether or not it was deleted: deleteSuccess :: Ord k => k -> Map k a -> (Bool, Map k a)
deleteSuccess = alterF (\x -> (isJust x, Nothing)) Inserting a value and return what was overwritten: overwrite :: Ord k => k -> a -> Map k a -> (Maybe a, Map k a)
overwrite k a = alterF (, Just a) ... yeah these are all the |
These three functions are pure, which is really great. But I believe purity gets it the way in the following use case: add key-value pair if key is not present or throw error in a monad if key is already present.
Sharing traversal for both presence check and update will avoid redundant work.
The text was updated successfully, but these errors were encountered: