JuliaCollections / IterTools.jl

Common functional iterator patterns
Other
153 stars 29 forks source link

Collect subsets of tuple #68

Open JobJob opened 4 years ago

JobJob commented 4 years ago

Is this a bug in IterTools?

julia> using IterTools
julia> collect(subsets((0,2,6), 2))

ERROR: MethodError: Cannot `convert` an object of type Tuple{Int64,Int64} to an object of type Array{Int64,1}
...
Stacktrace:
 [1] setindex!(::Array{Array{Int64,1},1}, ::Tuple{Int64,Int64}, ::Int64) at ./array.jl:767
 [2] copyto!(::Array{Array{Int64,1},1}, ::IterTools.Binomial{Tuple{Int64,Int64,Int64}}) at ./abstractarray.jl:671
 [3] _collect at ./array.jl:550 [inlined]
 [4] collect(::IterTools.Binomial{Tuple{Int64,Int64,Int64}}) at ./array.jl:544
 [5] top-level scope at none:0

This works though:

julia> collect(x for x in subsets((0,2,6), 2))
3-element Array{Tuple{Int64,Int64},1}:
 (0, 2)
 (0, 6)
 (2, 6)

Julia 1.1 IterTools 1.3

JobJob commented 4 years ago

Ok I looked, the problem is that eltype(typeof(subsets((0,2,6), 2))) is Vector{Int64}

code here: https://github.com/JuliaCollections/IterTools.jl/blob/dc0b0a2eabf433ebc8f09afe57db6d7c1433d637/src/IterTools.jl#L538

iamed2 commented 4 years ago

Yes that's a bug. I think the best fix would be to have subsets(::Tuple, ::Any) use StaticSizeBinomial.