nodes: 0, module; 1, function_definition; 2, function_name:merge; 3, parameters; 4, block; 5, identifier:iterables; 6, default_parameter; 7, default_parameter; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, if_statement; 12, if_statement; 13, for_statement; 14, expression_statement; 15, while_statement; 16, if_statement; 17, identifier:key; 18, None; 19, identifier:reverse; 20, False; 21, string; 22, assignment; 23, assignment; 24, identifier:reverse; 25, block; 26, else_clause; 27, comparison_operator:key is None; 28, block; 29, pattern_list; 30, call; 31, block; 32, call; 33, comparison_operator:len(h) > 1; 34, block; 35, identifier:h; 36, block; 37, string_content:Merge multiple sorted inputs into a single sorted output.
  
      Similar to sorted(itertools.chain(*iterables)) but returns a generator,
      does not pull the data into memory all at once, and assumes that each of
      the input streams is already sorted (smallest to largest).
  
      >>> list(merge([1,3,5,7], [0,2,4,8], [5,10,15,20], [], [25]))
      [0, 1, 2, 3, 4, 5, 5, 7, 8, 10, 15, 20, 25]
  
      If *key* is not None, applies a key function to each element to determine
      its sort order.
  
      >>> list(merge(['dog', 'horse'], ['cat', 'fish', 'kangaroo'], key=len))
      ['dog', 'cat', 'fish', 'horse', 'kangaroo']; 38, identifier:h; 39, list; 40, identifier:h_append; 41, attribute; 42, expression_statement; 43, expression_statement; 44, expression_statement; 45, expression_statement; 46, block; 47, identifier:key; 48, None; 49, for_statement; 50, expression_statement; 51, while_statement; 52, if_statement; 53, return_statement; 54, identifier:order; 55, identifier:it; 56, identifier:enumerate; 57, argument_list; 58, try_statement; 59, identifier:_heapify; 60, argument_list; 61, call; 62, integer:1; 63, try_statement; 64, expression_statement; 65, expression_statement; 66, for_statement; 67, identifier:h; 68, identifier:append; 69, assignment; 70, assignment; 71, assignment; 72, assignment; 73, expression_statement; 74, expression_statement; 75, expression_statement; 76, expression_statement; 77, pattern_list; 78, call; 79, block; 80, call; 81, comparison_operator:len(h) > 1; 82, block; 83, identifier:h; 84, comment:# fast case when only a single iterator remains; 85, block; 86, call; 87, block; 88, except_clause; 89, identifier:h; 90, identifier:len; 91, argument_list; 92, block; 93, except_clause; 94, assignment; 95, yield; 96, identifier:value; 97, identifier:it; 98, block; 99, identifier:_heapify; 100, identifier:_heapify_max; 101, identifier:_heappop; 102, identifier:_heappop_max; 103, identifier:_heapreplace; 104, identifier:_heapreplace_max; 105, identifier:direction; 106, unary_operator; 107, assignment; 108, assignment; 109, assignment; 110, assignment; 111, identifier:order; 112, identifier:it; 113, identifier:enumerate; 114, argument_list; 115, try_statement; 116, identifier:_heapify; 117, argument_list; 118, call; 119, integer:1; 120, try_statement; 121, expression_statement; 122, expression_statement; 123, for_statement; 124, identifier:map; 125, argument_list; 126, expression_statement; 127, expression_statement; 128, identifier:StopIteration; 129, block; 130, identifier:h; 131, while_statement; 132, identifier:StopIteration; 133, block; 134, pattern_list; 135, subscript; 136, identifier:value; 137, expression_statement; 138, integer:1; 139, identifier:_heapify; 140, identifier:heapify; 141, identifier:_heappop; 142, identifier:heappop; 143, identifier:_heapreplace; 144, identifier:heapreplace; 145, identifier:direction; 146, integer:1; 147, call; 148, block; 149, except_clause; 150, identifier:h; 151, identifier:len; 152, argument_list; 153, block; 154, except_clause; 155, assignment; 156, yield; 157, identifier:value; 158, identifier:it; 159, block; 160, identifier:iter; 161, identifier:iterables; 162, assignment; 163, call; 164, pass_statement; 165, True; 166, block; 167, expression_statement; 168, identifier:key_value; 169, identifier:order; 170, identifier:value; 171, identifier:it; 172, identifier:h; 173, integer:0; 174, yield; 175, identifier:map; 176, argument_list; 177, expression_statement; 178, identifier:StopIteration; 179, block; 180, identifier:h; 181, while_statement; 182, identifier:StopIteration; 183, block; 184, pattern_list; 185, subscript; 186, identifier:value; 187, expression_statement; 188, identifier:value; 189, call; 190, identifier:h_append; 191, argument_list; 192, expression_statement; 193, expression_statement; 194, expression_statement; 195, expression_statement; 196, expression_statement; 197, expression_statement; 198, call; 199, identifier:value; 200, identifier:iter; 201, identifier:iterables; 202, call; 203, pass_statement; 204, True; 205, block; 206, expression_statement; 207, comment:# remove empty iterator; 208, identifier:value; 209, identifier:order; 210, identifier:it; 211, identifier:h; 212, integer:0; 213, yield; 214, identifier:next; 215, argument_list; 216, list; 217, assignment; 218, yield; 219, assignment; 220, assignment; 221, assignment; 222, call; 223, identifier:_heappop; 224, argument_list; 225, identifier:h_append; 226, argument_list; 227, expression_statement; 228, expression_statement; 229, expression_statement; 230, comment:# raises StopIteration when exhausted; 231, expression_statement; 232, comment:# restore heap condition; 233, call; 234, identifier:value; 235, identifier:it; 236, call; 237, binary_operator:order * direction; 238, identifier:value; 239, identifier:it; 240, pattern_list; 241, assignment; 242, identifier:value; 243, identifier:value; 244, call; 245, subscript; 246, call; 247, subscript; 248, identifier:value; 249, identifier:_heapreplace; 250, argument_list; 251, identifier:h; 252, list; 253, assignment; 254, yield; 255, assignment; 256, call; 257, identifier:_heappop; 258, argument_list; 259, identifier:key; 260, argument_list; 261, identifier:order; 262, identifier:direction; 263, identifier:key_value; 264, identifier:order; 265, identifier:value; 266, identifier:it; 267, identifier:s; 268, subscript; 269, identifier:next; 270, argument_list; 271, identifier:s; 272, integer:0; 273, identifier:key; 274, argument_list; 275, identifier:s; 276, integer:2; 277, identifier:h; 278, identifier:s; 279, call; 280, binary_operator:order * direction; 281, identifier:it; 282, pattern_list; 283, assignment; 284, identifier:value; 285, subscript; 286, call; 287, identifier:_heapreplace; 288, argument_list; 289, identifier:h; 290, identifier:value; 291, identifier:h; 292, integer:0; 293, identifier:it; 294, identifier:value; 295, identifier:next; 296, argument_list; 297, identifier:order; 298, identifier:direction; 299, identifier:value; 300, identifier:order; 301, identifier:it; 302, identifier:s; 303, subscript; 304, identifier:s; 305, integer:0; 306, identifier:next; 307, argument_list; 308, identifier:h; 309, identifier:s; 310, identifier:it; 311, identifier:h; 312, integer:0; 313, identifier:it
code: def merge(iterables, key=None, reverse=False):
      '''Merge multiple sorted inputs into a single sorted output.
  
      Similar to sorted(itertools.chain(*iterables)) but returns a generator,
      does not pull the data into memory all at once, and assumes that each of
      the input streams is already sorted (smallest to largest).
  
      >>> list(merge([1,3,5,7], [0,2,4,8], [5,10,15,20], [], [25]))
      [0, 1, 2, 3, 4, 5, 5, 7, 8, 10, 15, 20, 25]
  
      If *key* is not None, applies a key function to each element to determine
      its sort order.
  
      >>> list(merge(['dog', 'horse'], ['cat', 'fish', 'kangaroo'], key=len))
      ['dog', 'cat', 'fish', 'horse', 'kangaroo']
  
      '''
  
      h = []
      h_append = h.append
  
      if reverse:
          _heapify = _heapify_max
          _heappop = _heappop_max
          _heapreplace = _heapreplace_max
          direction = -1
      else:
          _heapify = heapify
          _heappop = heappop
          _heapreplace = heapreplace
          direction = 1
  
      if key is None:
          for order, it in enumerate(map(iter, iterables)):
              try:
                  h_append([next(it), order * direction, it])
              except StopIteration:
                  pass
          _heapify(h)
          while len(h) > 1:
              try:
                  while True:
                      value, order, it = s = h[0]
                      yield value
                      s[0] = next(it)           # raises StopIteration when exhausted
                      _heapreplace(h, s)      # restore heap condition
              except StopIteration:
                  _heappop(h)                 # remove empty iterator
          if h:
              # fast case when only a single iterator remains
              value, order, it = h[0]
              yield value
              for value in it:
                  yield value
          return
  
      for order, it in enumerate(map(iter, iterables)):
          try:
              value = next(it)
              h_append([key(value), order * direction, value, it])
          except StopIteration:
              pass
      _heapify(h)
      while len(h) > 1:
          try:
              while True:
                  key_value, order, value, it = s = h[0]
                  yield value
                  value = next(it)
                  s[0] = key(value)
                  s[2] = value
                  _heapreplace(h, s)
          except StopIteration:
              _heappop(h)
      if h:
          key_value, order, value, it = h[0]
          yield value
          for value in it:
              yield value