The idea is a shorthand for reduce. Here, _next_ meant the next item
You mean like one of these:
def lookahead(iterator):
i = iter(iterator)
x = i.next()
for item in i:
yield x, item
x = item
def lookahead2(iterator, **kwarg):
i = iter(iterator)
if 'initial' in kwarg:
x = kwarg['initial']
else:
x = i.next()
for item in i:
yield x, item
x = item
if 'last' in kwarg:
yield x, kwarg['last']
print 'lookahead()'
for this, next in lookahead([1,2,3,4,5]):
print this, next
print 'lookahead2()'
for this, next in lookahead2([1,2,3,4,5]):
print this, next
print 'lookahead2(initial=42)'
for this, next in lookahead2([1,2,3,4,5], initial=42):
print this, next
print 'lookahead2(last=42)'
for this, next in lookahead2([1,2,3,4,5], last=42):
print this, next
print 'lookahead2(initial=3.14159, last=42)'
for this, next in lookahead2([1,2,3,4,5],
initial=3.14159, last=42):
print this, next
There are some alternate behaviors that can happen at the end
points, so depending on which behavior you want, the lookahead()
is cleanest, but doesn't allow you to handle edge cases. The
lookahead2() is a little more complex, but allows you to specify
a first item for pairing (so "next" touches every item in your
list) or a trailing element (so "this" touches every item).
-tkc