<s> """<STR_LIT>""" <EOL> import heapq <EOL> import vimap . pool <EOL> import vimap . util <EOL> import vimap . worker_process <EOL> def imap_unordered ( fcn , iterable , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> @ vimap . worker_process . worker <EOL> def worker ( inputs , ** kwargs2 ) : <EOL> for in_ in inputs : <EOL> yield fcn ( in_ , ** kwargs2 ) <EOL> pool = vimap . pool . fork_identical ( worker , ** kwargs ) <EOL> for _ , output in pool . imap ( iterable ) . zip_in_out ( ) : <EOL> yield output <EOL> def imap_ordered ( fcn , iterable , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> unordered_results = imap_unordered ( <EOL> lambda ( i , x ) : ( i , fcn ( x ) ) , <EOL> enumerate ( iterable ) , <EOL> ** kwargs <EOL> ) <EOL> next_index = <NUM_LIT:0> <EOL> heap = [ ] <EOL> for i , fx in unordered_results : <EOL> if next_index == i : <EOL> next_index += <NUM_LIT:1> <EOL> yield fx <EOL> else : <EOL> heapq . heappush ( heap , ( i , fx ) ) <EOL> while heap and ( heap [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == next_index ) : <EOL> i_next , fx_next = heapq . heappop ( heap ) <EOL> next_index += <NUM_LIT:1> <EOL> yield fx_next <EOL> assert not heap <EOL> def imap_ordered_chunked ( fcn , inputs , chunk_size = <NUM_LIT:100> , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> inputs_chunked = vimap . util . chunk ( inputs , chunk_size ) <EOL> output_chunks = imap_ordered ( <EOL> lambda chunk , ** kwargs2 : [ fcn ( x , ** kwargs2 ) for x in chunk ] , <EOL> inputs_chunked , <EOL> ** kwargs <EOL> ) <EOL> return ( x for chunk in output_chunks for x in chunk ) </s>
