Replacement for Zip/Lockstep

bearophile bearophileHUGS at lycos.com
Sun Mar 16 19:19:56 PDT 2014


jerro:

> Code that intentionally
> passes finite ranges with different lengths to zip() is probably
> pretty rare (code that does it unintentionally may be more 
> common).

In my code it's sufficiently common.

I have just "grepped" only two usages of 
StoppingPolicy.requireSameLength in my code. And one of them is 
an artificial need.

One use case for zipping unequal length sequences is to take 
adjacent pairs of a sequence, you zip the sequence with itself 
minus the first (Python2.6 code):

>>> a = [10, 20, 30, 40]
>>> zip(a, a[1:])
[(10, 20), (20, 30), (30, 40)]
>>> from itertools import izip
>>> list(izip(a, a[1:]))
[(10, 20), (20, 30), (30, 40)]
>>> zip(a, a[1:], a[2:])
[(10, 20, 30), (20, 30, 40)]

This is useful for local averages, or to compute simple local 
operations on a sequence.


> And even when such code is correct, being explicit about it 
> would make it clearer.

One problem is that "StoppingPolicy.requireSameLength" is very 
long, so putting it in UFCS chains kills too much horizontal 
space.

Bye,
bearophile


More information about the Digitalmars-d mailing list