Give Flatten-iterator HasLength() in some cases#16680
Closed
mschauer wants to merge 1 commit intoJuliaLang:masterfrom
Closed
Give Flatten-iterator HasLength() in some cases#16680mschauer wants to merge 1 commit intoJuliaLang:masterfrom
mschauer wants to merge 1 commit intoJuliaLang:masterfrom
Conversation
Author
|
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") |
Contributor
There was a problem hiding this comment.
should this actually throw the ArgumentError instead of returning it? and test that branch?
Author
|
Replaced by #22691 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
vcatfor iterators becomes simplyThis PR only gives the HasLength trait to Base.Flatten. The function
itervcatjust illustrates the usefulness.The check for
isleaftypegets optimized away: