#NEWLINE# def matchby_comparison( iter1 , iter2 , matchfun1 , matchfun2 = None ) : #NEWLINE# #INDENT# 'return pairs of values from two streams that are matched by match \n    function.\n\n    The two input streams need to be sorted by increasing key value.\n    ' #NEWLINE# if ( matchfun2 is None ) : #NEWLINE# #INDENT# matchfun2 = matchfun1 #NEWLINE# #UNINDENT# groups1 = itertools . groupby ( iter1 , matchfun1 ) #NEWLINE# groups2 = itertools . groupby ( iter2 , matchfun2 ) #NEWLINE# ( key2 , value2 ) = groups2 . next ( ) #NEWLINE# ( last_key1 , last_key2 ) = ( key1 , key2 ) #NEWLINE# for ( key1 , value1 ) in groups1 : #NEWLINE# #INDENT# assert ( last_key1 < key1 ) , 'input needs to be sorted by keys in ascending order.' #NEWLINE# last_key1 = key1 #NEWLINE# while ( key1 > key2 ) : #NEWLINE# #INDENT# last_key2 = key2 #NEWLINE# ( key2 , value2 ) = groups2 . next ( ) #NEWLINE# assert ( last_key2 < key2 ) , 'input needs to be sorted by keys in ascending order.' #NEWLINE# #UNINDENT# if ( key1 < key2 ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# ( yield ( value1 , value2 ) )