Skip to content

HasLength for even more Flatten iterators #23431

@tkoolen

Description

@tkoolen

I saw these recent changes: #22691, but maybe we can do better.

Even after these changes, the following example (from Discourse) still results in a SizeUnknown Flatten iterator:

using Base.Iterators: flatten
dr = [1:2, 4:7]
Base.iteratorsize(flatten(dr)) # SizeUnknown()

It would be possible to simply define

Base.length(fl::Flatten) = mapreduce(length, +, 0, fl.it)

and to add UnitRange to this list, but maybe it would be nice to do this more generically? Base.iteratorsize could return HasLength() for fl::Flatten if all iterators in fl.it are HasLength or HasSize, and SizeUnknown otherwise. That would mean that Base.iteratorsize(::Flatten) could no longer be optimized into a constant however; is that a big issue?

Metadata

Metadata

Assignees

No one assigned

    Labels

    iterationInvolves iteration or the iteration protocol

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions