def heap_merged(items_lists, combiner):
    heap = []
    def pushback(it):
        try:
            k,v = it.next()
            # put i before value, so do not compare the value
            heapq.heappush(heap, (k, i, v))
        except __HOLE__:
            pass
    for i, it in enumerate(items_lists):
        if isinstance(it, list):
            items_lists[i] = it = (k for k in it)
        pushback(it)
    if not heap: return

    last_key, i, last_value = heapq.heappop(heap)
    pushback(items_lists[i])

    while heap:
        k, i, v = heapq.heappop(heap)
        if k != last_key:
            yield last_key, last_value
            last_key, last_value = k, v
        else:
            last_value = combiner(last_value, v)
        pushback(items_lists[i])

    yield last_key, last_value
dataset/ETHPy150Open douban/dpark/dpark/shuffle.py/heap_merged