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

Return Array instead of Dict in function groupby #91

Open
sgalal opened this issue Aug 23, 2018 · 0 comments
Open

Return Array instead of Dict in function groupby #91

sgalal opened this issue Aug 23, 2018 · 0 comments

Comments

@sgalal
Copy link

sgalal commented Aug 23, 2018

Hi! I am using the groupby function and I notice that the return type is Dict instead of Array.

import Base.@get!
function groupby(f, xs)
result = d()
for x in xs
push!(@get!(result, f(x), []), x)
end
return result
end

julia> groupby(x -> x[1], [(1,2,3), (2,2,3), (1,2,4), (2,3,4)])
Dict{Any,Any} with 2 entries:
  2 => Any[(2, 2, 3), (2, 3, 4)]
  1 => Any[(1, 2, 3), (1, 2, 4)]

Would it be better if the function returns an Array so the original order could be preserved? Like this:

julia> groupby(x -> x[1], [(1,2,3), (2,2,3), (1,2,4), (2,3,4)])
2-element Array{Any,1}:
 Any[(1, 2, 3), (1, 2, 4)]
 Any[(2, 2, 3), (2, 3, 4)]

Besides, in Haskell, the type signature of groupBy looks like:

groupBy :: (a -> a -> Bool) -> [a] -> [[a]]

which returns an Array.

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

No branches or pull requests

1 participant