On Sun, Apr 26, 2020 at 11:56 PM Christopher Barker <pythonchb@gmail.com> wrote:
If I have two or more "sequences" there are basically two cases of that.
so you need to write different code, depending on which case? that seems not very "there's only one way to do it" to me.
This difference is built into the problem itself. There CANNOT be only one way to do these fundamentally different things. With iterators, there is at heart a difference between "sequences that one can (reasonably) concretize" and "sequences that must be lazy." And that difference means that for some versions of a seemingly similar problem it is possible to ask len() before looping through them while for others that is not possible (and hence we may have done some work that we want to "roll-back" in some sense). Exactly what not-reasonable means might vary by context. Infinite sequences are always a no-go. But slow iterators could go either way perhaps. I.e. I'm waiting on a slow wire for data, but when it arrives it will be moderate sized. Should I wait? I dunno, it depends. Or maybe it is fast, but there are a billion items. Do I want to use the memory? Maybe Whatever decision I make has to decide whether bounds can be checked in advance. However, the mismatched length feels like such a small concern in what can go wrong. For example, I have some time series data I was working with yesterday. The same timestamps are meant to match up with several different measurements. However, *sometimes* a measurement is missing. I might therefore wind up with two or more sequences of the same length, but with "teeth" of the zipper that don't actually match up always. Neither checking len() nor zip_strict() nor a zip_longest() sentinel are going to catch this problem.
Or alternately, we have a new function/mode that instead formulates this as:
try: for pair in zip_strict(stuff1, stuff2): process(pair) except ZipLengthError: raise UnequalLengthError("uh oh")
The hypothetical new style is fine. To me it looks slightly less good, but the difference is minimal.
To me it looks better than both of the other options -- and much better (particularly for beginners) than the _sentinal approach.
Sure. That's fine. I'm +0 or even higher on adding itertools.zip_strict(). My taste prefers the other style I showed, but as I say, this version is perfectly fine. De gustibus non disputandum est. -- Keeping medicines from the bloodstreams of the sick; food from the bellies of the hungry; books from the hands of the uneducated; technology from the underdeveloped; and putting advocates of freedom in prisons. Intellectual property is to the 21st century what the slave trade was to the 16th.