[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Iterators, iterables and special objects

On Fri, Jul 24, 2020, at 14:42, Chris Angelico wrote:
> And then someone will ask why you can't subscript a map object if the
> underlying object could be subscripted, etc, etc, etc. It's not meant
> to be a transparent layer over the object; it's just an iterator -
> basically equivalent to:
> def map(func, *iters):
>     try:
>         while True:
>             yield func(*(next(i) for i in iters))
>     except StopIteration:
>         pass
> If you want a "MappedList" class, or a "MappedDict" class, or
> whatever, then build it - it isn't hard.

Asking to be able to restart the iteration is hardly the same thing as asking to pass through subscripts etc... C#'s Linq functions do fine with restartable iterables and hardly ever expose Iterators [well, IEnumerators, as they're called] to the user at all. If the original IEnumerable was restartable, so are all the intervening steps if you put an arbitrarily long chain of Select, Where, etc, on them, since each one restarts the one under it.

Obviously no-one should reasonably expect to be able to pass list subscripts through, say, a filter object, but restarting is a perfectly reasonable thing to do to *any* transformation of an iterable, whether or not it groups, splits, filters, interjects items or anything else you might imagine doing.