Skip to content

Give Flatten-iterator HasLength() in some cases#16680

Closed
mschauer wants to merge 1 commit intoJuliaLang:masterfrom
mschauer:flatten
Closed

Give Flatten-iterator HasLength() in some cases#16680
mschauer wants to merge 1 commit intoJuliaLang:masterfrom
mschauer:flatten

Conversation

@mschauer
Copy link
Copy Markdown

It is worthwhile to give some flattened iterators with elements of known type the trait HasLength.
This PR does this for iterators with elements of type tuple.

The length can be easily computed from the length of the iterator and the nfields of the tuple-type. This makes it for example possible to use a flattened zip as input of IteratorND and a naive vcat for iterators becomes simply

function itervcat(a...)
    z = zip(a...)
    Base.IteratorND(Base.flatten(z), (length(a), size(z)...))
end

This PR only gives the HasLength trait to Base.Flatten. The function itervcat just illustrates the usefulness.

The check for isleaftype gets optimized away:

@code_typed Base.iteratorsize(Base.flatten(zip(1:3)))
1-element Array{Any,1}:
 LambdaInfo for iteratorsize
:(begin  # generator.jl, line 34:
        # meta: location iterator.jl iteratorsize # iterator.jl, line 488:
        $(QuoteNode(true))
        # meta: pop location
        return $(Expr(:new, :(Base.HasLength)))
    end::Base.HasLength)

@mschauer
Copy link
Copy Markdown
Author

mschauer commented Jun 6, 2016

Personal assessment: this change is helpful, but the scope is limited as there are few objects beyond tuples which have static length (or size) attribute, and there are little means in julia to talk about length and size of a type. On the other hand fixed size arrays and a hypothetical buffer type can also covered in a similar way in the future.

base/iterator.jl Outdated

flatten_iteratorsize{T<:Tuple}(::Union{HasShape, HasLength}, b::Type{T}) = HasLength()
flatten_iteratorsize(a, b) = SizeUnknown()
flatten_length{T<:Tuple}(f, ::Type{T}) = isleaftype(T) ? nfields(T)*length(f.it) : ArgumentError("Cannot compute length of a tuple-type which is not a leaf-type")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this actually throw the ArgumentError instead of returning it? and test that branch?

@mschauer mschauer changed the title Given Flatten-iterator HasLength() in some cases Give Flatten-iterator HasLength() in some cases Jun 8, 2016
@mschauer
Copy link
Copy Markdown
Author

mschauer commented Jul 6, 2017

Replaced by #22691

@mschauer mschauer closed this Jul 6, 2017
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

Successfully merging this pull request may close these issues.

2 participants