from itertools import zip_longest
def map_longest(func, *iterables, fillvalue=None):
for t in zip_longest(*iterables, fillvalue=fillvalue):
yield func(*t)
def map_strict(func, *iterables, exception=None):
sentinel = object()
for t in zip_longest(*iterables, fillvalue=sentinel):
if sentinel in t:
if exception is None:
exception = ValueError('too few items in iterable')
raise exception
yield func(*t)
Diff to Previous Revision
--- revision 1 2011-05-06 09:29:52
+++ revision 2 2011-05-06 17:35:17
@@ -8,9 +8,7 @@
sentinel = object()
for t in zip_longest(*iterables, fillvalue=sentinel):
if sentinel in t:
- if exception: raise exception
- else:
- n = len(t)
- c = t.count(sentinel)
- raise ValueError('too few items in iterable')
+ if exception is None:
+ exception = ValueError('too few items in iterable')
+ raise exception
yield func(*t)