Skip to content

Instantly share code, notes, and snippets.

@draftcode
Created January 8, 2014 12:10
Show Gist options
  • Save draftcode/8315934 to your computer and use it in GitHub Desktop.
Save draftcode/8315934 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from __future__ import division, absolute_import, print_function, unicode_literals
def indicate_last(gen):
saved_e = None
prev_value = next(gen)
value = None
while True:
if saved_e is not None:
raise saved_e
try:
value = next(gen)
except StopIteration as e:
saved_e = e
yield prev_value, (saved_e is not None)
prev_value = value
value = None
for v, b in indicate_last(i for i in range(5)):
print(v, b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment