icmp - should this go in itertools?

T

Tom Anderson

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?

tom
 
R

Roy Smith

Tom Anderson said:
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?

Whatever happens, please name it something other than icmp. When I read
"icmp", I think "Internet Control Message Protocol".
 
D

Diez B. Roggisch

Tom said:
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
 
J

Jorgen Grahn

Whatever happens, please name it something other than icmp. When I read
"icmp", I think "Internet Control Message Protocol".

Me too, but I see that as an added bonus ;-). OTOH, if this is part of
itertools or some other module, "itertools.cmp" would be a more logical
name. (On the third hand, as someone else said, I don't see the use case for
this.)

/Jorgen
 
T

Tom Anderson

Whatever happens, please name it something other than icmp. When I read
"icmp", I think "Internet Control Message Protocol".

Heh! That's a good point. The trouble is, icmp is clearly the Right Thing
to call it from the point of view of itertools, continuing the pattern of
imap, ifilter, izip etc. Wouldn't it be clear from context that this was
nothing to do with ICMP?

tom
 
T

Tom Anderson

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.

Good point. It would probably only be useful if you didn't need to do
anything with the iterators afterwards.

One option - which is somewhat icky - would be to encode that in the
return value; if n is the number of items read from both iterators, then
if the first argument is smaller, the return value is -n, and if the
second is smaller, it's n. The trouble is that you couldn't be sure
exactly how many items had been read from the larger iterator - it could
be n, if the values in the iterators differ, or n+1, if the values were
the same but the larger one was longer.
I'm just not sure if there is any usecase for that.

I used it in my ordered dictionary implementation; it was a way of
comparing two 'virtual' lists that are lazily generated on demand.

I'll go away and think about this more.

tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top