icmp - should this go in itertools?

Diez B. Roggisch deets at nospam.web.de
Sat Nov 26 07:14:11 EST 2005


Tom Anderson wrote:
> Hi all,
> 
> This is a little function to compare two iterators:
> 
> 
> 
> def icmp(a, b):
>     for xa in a:
>         try:
>             xb = b.next()
>             d = cmp(xa, xb)
>             if (d != 0):
>                 return d
>         except StopIteration:
>             return 1
>     try:
>         b.next()
>         return -1
>     except StopIteration:
>         return 0
> 
> 
> 
> It's modelled after the way cmp treats lists - if a and b are lists, 
> icmp(iter(a), iter(b)) should always be the same as cmp(a, b).
> 
> Is this any good? Would it be any use? Should this be added to itertools?

Whilst not a total itertools-expert myself, I have one little objection 
with this: the comparison won't let me know how many items have been 
consumed. And I end up with two streams that lack some common prefix 
plus one field. I'm just not sure if there is any usecase for that.

However, _if_ there is one, I'm all for adding it to itertools - it 
seems to be in the appropriate spirit.

Regards,

Diez



More information about the Python-list mailing list