Julia convert EnumerableGroupBy to array

Viewed 58

I have the following code that does a groupby on an array of numbers and returns an array of tuples with the numbers and respective counts:

using Query
a = [1, 2, 1, 2, 3, 4, 6, 1, 5, 5, 5, 5]
key_counts = a |> @groupby(_) |> @map g -> (key(g), length(values(g)))
collect(key_counts)

Is there a way to complete the last step in the pipeline to convert the key_counts of type QueryOperators.EnumerableMap{Tuple{Int64, Int64}, QueryOperators.EnumerableIterable{Grouping{Int64, Int64}, QueryOperators.EnumerableGroupBy{Grouping{Int64, Int64}, Int64, Int64, QueryOperators.EnumerableIterable{Int64, Vector{Int64}}, var"#12#17", var"#13#18"}}, var"#15#20"} to Vector{Tuple{Int, Int}} directly by integrating the collect operation to the pipeline as one liner?

1 Answers

The question has been clarified. My answer is no longer intended as a solution but provides additional information.

Using key_counts |> collect instead of collect(key_counts) works on the second line, but |> collect at the end of the pipe line does not, which feels like unwanted behavior.

Below response no longer relevant

When I run your code I actually do receive a Vector{Tuple{Int, Int}} as output. I'm using Julia v1.6.0 with Query v1.0.0.

using Query
a = [1, 2, 1, 2, 3, 4, 6, 1, 5, 5, 5, 5]
key_counts = a |> @groupby(_) |> @map g -> (key(g), length(values(g)))
output = collect(key_counts)
typeof(output) # Vector{Tuple{Int64, Int64}} (alias for Array{Tuple{Int64, Int64}, 1}) 
Related