Skip to content
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

insertLookup function ? #245

Open
jfaure opened this issue Jan 5, 2020 · 4 comments
Open

insertLookup function ? #245

jfaure opened this issue Jan 5, 2020 · 4 comments

Comments

@jfaure
Copy link

jfaure commented Jan 5, 2020

Data.HashMap seems to lack one, although such a function is important for performance reasons.

@ollef
Copy link

ollef commented Mar 2, 2020

It's not entirely obvious, but this can be implemented using alterF which was added in 0.2.9, by letting the functor f = (,) (Maybe v). See http://hackage.haskell.org/package/unordered-containers-0.2.10.0/docs/Data-HashMap-Lazy.html#v:alterF.

It would still be nice to have a special-purpose function for this though.

@sjakobi
Copy link
Member

sjakobi commented May 31, 2020

What does the implementation in terms of alterF look like?

I suspect that it might not cross the Fairbairn threshold – in that case I'd probably prefer to add it only as an example in the alterF docs, instead of adding a new export.

@ollef
Copy link

ollef commented Jun 2, 2020

I believe it would be

insertLookup :: (Eq k, Hashable k) => k -> v -> HashMap k v -> (Maybe v, HashMap k v)
insertLookup k v = alterF (\old -> (old, Just v)) k

Data.Map has a withKey version of this function, which might be reason enough to add at least that.

@sjakobi
Copy link
Member

sjakobi commented Jun 2, 2020

Hm, consistency with containers is also nice. containers is huge though, and that also comes with costs with regard to discoverability and maintainability IMHO.

If there's a choice between making it easier to use alterF and adding more functions that can be implemented with alterF, I'd currently prefer doing the former.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants