def lazy_lt(lseq, rseq): """Less-than comparison for two lazily generated sequences. See ``lazy_lexicographic_ordering``. """ liter = lseq() riter = rseq() for left, right in zip_longest(liter, riter, fillvalue=done): if (left is done) or (right is done): return left is done # left was shorter than right sequence = callable(left) equal = lazy_eq(left, right) if sequence else left == right if equal: continue if sequence: return lazy_lt(left, right) if left is None: return True if right is None: return False return left < right return False # if equal, return False
def lazy_eq(lseq, rseq): """Equality comparison for two lazily generated sequences. See ``lazy_lexicographic_ordering``. """ liter = lseq() # call generators riter = rseq() # zip_longest is implemented in native code, so use it for speed. # use zip_longest instead of zip because it allows us to tell # which iterator was longer. for left, right in zip_longest(liter, riter, fillvalue=done): if (left is done) or (right is done): return False # recursively enumerate any generators, otherwise compare equal = lazy_eq(left, right) if callable(left) else left == right if not equal: return False return True