JuliaData / SplitApplyCombine.jl

Split-apply-combine strategies for Julia
Other
149 stars 15 forks source link

Returned Dictionaries.Dictonary type does not adhere to Julia Dict interface #57

Open kmacdough opened 2 years ago

kmacdough commented 2 years ago

I was expecting to be able to iterate through these grouped dictionaries, but I'm noticing they do not seem to follow the AbstractDict interface. Is this an intentional design decision? If so, is there a benefit to this counterintuitive behavior?

julia> group(iseven, 1:5)
2-element Dictionaries.Dictionary{Bool, Vector{Int64}}
 false │ [1, 3, 5]
  true │ [2, 4]

julia> Dict(false => [1, 3, 5], true => [2, 4])
Dict{Bool, Vector{Int64}} with 2 entries:
  0 => [1, 3, 5]
  1 => [2, 4]

julia> collect(group(iseven, 1:5))
2-element Vector{Vector{Int64}}:
 [1, 3, 5]
 [2, 4]

julia> collect(Dict(false => [1, 3, 5], true => [2, 4]))
2-element Vector{Pair{Bool, Vector{Int64}}}:
 0 => [1, 3, 5]
 1 => [2, 4]
andyferris commented 2 years ago

Yes. Take a look at e.g. https://discourse.julialang.org/t/splitapplycombine-jl-group-enhancements-reaches-version-1-0-0/32675

In your example we can do something like length.(group(iseven, 1:5)). You can convert the result to a Dict straightforwardly if that works better for you.

With Dict we can’t use things like map and broadcast. Dictionaries.jl is an attempt to bring the niceties of working with arrays in Julia to dictionaries and sets, too.

Does that make sense?

kmacdough commented 2 years ago

@andyferris Ah yes, this definitely makes sense! Messing around with the package more I can definitely see the power in that decision.

I see now there's a note in the README, but it's pretty easy to overlook tacked at the end of the quick start, and I wouldn't have thought much of it had I noticed it. Might it be worth putting it somewhere more prominent & noting the consequences/utility of the decision? It does create a significant behavior divergence from the core libraries, and I expect I'm not the first naive user of the library thrown off by this.

If you're interested, I'd be happy to throw up a PR to that effect when I find some time.

andyferris commented 2 years ago

Yes the documentation could definitely be improved. In fact, a lot of the usage documentation lives elsewhere like in the TypedTables docs. Contributions are welcome :)