From 501552a65a417b7381cc7c5c1bcd33fc40d4d524 Mon Sep 17 00:00:00 2001 From: Wirawan Purwanto Date: Tue, 17 Apr 2012 11:00:30 -0400 Subject: [PATCH] * Added zip_gen() to simulate the effect of built-in zip() function, but implemented as a generator rather than yielding the list right away. This is used mainly in loops, where we don't want to generate all the elements due to memory concerns. --- sugar.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/sugar.py b/sugar.py index 38a4010..a9c5e62 100644 --- a/sugar.py +++ b/sugar.py @@ -93,6 +93,18 @@ def list_join(*L): return r +def zip_gen(*L): + """Generator version of the built-in zip() function. + Used so that the all the loop iterations are not invoked before + the main `for' statement is invoked (e.g. if memory is a concern + when generating the list--this method does not generate any list). + Performance could be slightly slower than the original zip function, + though.""" + L_iters = [ i.__iter__() for i in L ] + while True: + yield tuple([ L1.next() for L1 in L_iters ]) + + class ranges_type: """This class is to provide dirty magic to specify piecewice slices of one-dimensional ranges.