-
Notifications
You must be signed in to change notification settings - Fork 39
Implement mergewith() for OrderedDict and LittleDict #119
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
Conversation
src/little_dict.jl
Outdated
| return dc | ||
| end | ||
|
|
||
| mergewith(combine::Function, d::LittleDict, others::AbstractDict...) = merge(combine, d, others...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using Function is very restrictive, it excludes a bunch of useful applications, e.g. where combine is a type (so expressing coercion). However just making this Any can lead to ambiguities in the merge method. Avoiding that is AFAIK precisely the reason why mergewith was introduced in the first place.
So instead of implementing mergewith in terms of merge it is better to do it the other way around.
Thus I suggest that instead of adding this here, you instead rename the preceding merge method starting at line 169 to mergewith and drop the ::Function from its first argument.
Then here you can do
| mergewith(combine::Function, d::LittleDict, others::AbstractDict...) = merge(combine, d, others...) | |
| merge(combine::Function, d::LittleDict, others::AbstractDict...) = mergewith(combine, d, others...) |
or even better (matching what the mergewith docstring says):
| mergewith(combine::Function, d::LittleDict, others::AbstractDict...) = merge(combine, d, others...) | |
| merge(combine::Union{Function,Type}, d::LittleDict, others::AbstractDict...) = mergewith(combine, d, others...) |
src/ordered_dict.jl
Outdated
| merge!(combine, OrderedDict{K,V}(), d, others...) | ||
| end | ||
|
|
||
| mergewith(combine::Function, d::OrderedDict, others::AbstractDict...) = merge(combine, d, others...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above I suggest to generalize this so that combine can be anything. This should work:
| mergewith(combine::Function, d::OrderedDict, others::AbstractDict...) = merge(combine, d, others...) | |
| function mergewith(combine, d::OrderedDict, others::AbstractDict...) | |
| K,V = _merge_kvtypes(d, others...) | |
| mergewith!(combine, OrderedDict{K,V}(), d, others...) | |
| end |
Optionally the preceding merge method could then be turned into a one-liner calling mergewith.
|
Just to be clear: I am not a maintainer of this project, feel free to ignore me. Also, regardless of anything else: thank you for making the effort and contributing to this package, much appreciated. (I should have started with that, sorry) |
|
Thanks @fingolfin, makes sense! I had no idea why |
As suggested by @fingolfin, implementing `merge()` with a `combine::Function` argument in terms of `mergewith()` allows the signature of `mergewith()` to not restrict `combine` to be a `Function` specifically, which can be useful in some situations.
fingolfin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, thank you :-)
Fixes #77.