code: def pack_sequence_as(structure, flat_sequence, sequence_fn=None):
      """Implements sequence packing, i.e. nest.pack_sequence_as()."""
      is_nested_fn = tree.is_nested
      sequence_fn = sequence_fn or tree._sequence_like
  
      def truncate(value, length):
          value_str = str(value)
          return value_str[:length] + (value_str[length:] and "...")
  
      if not is_nested_fn(flat_sequence):
          raise TypeError(
              "Attempted to pack value:\n  {}\ninto a structure, but found "
              "incompatible type `{}` instead.".format(
                  truncate(flat_sequence, 100), type(flat_sequence)
              )
          )
  
      if not is_nested_fn(structure):
          if len(flat_sequence) != 1:
              raise ValueError(
                  "The target structure is of type `{}`\n  {}\nHowever the input "
                  "is a sequence ({}) of length {}.\n  {}\nnest cannot "
                  "guarantee that it is safe to map one to the other.".format(
                      type(structure),
                      truncate(structure, 100),
                      type(flat_sequence),
                      len(flat_sequence),
                      truncate(flat_sequence, 100),
                  )
              )
          return flat_sequence[0]
  
      try:
          final_index, packed = packed_nest_with_indices(
              structure, flat_sequence, 0, is_nested_fn, sequence_fn
          )
          if final_index < len(flat_sequence):
              raise IndexError
      except IndexError:
          flat_structure = tree.flatten(structure)
          if len(flat_structure) != len(flat_sequence):
              # pylint: disable=raise-missing-from
              raise ValueError(
                  "Could not pack sequence. "
                  f"Structure had {len(flat_structure)} atoms, but "
                  f"flat_sequence had {len(flat_sequence)} items. "
                  f"Structure: {structure}, flat_sequence: {flat_sequence}."
              )
      return sequence_fn(structure, packed)
  
imports: import tree
label_desc: Use image node_id 1 for calling a global function with example usage: pack_sequence_as(structure, flat_sequence, sequence_fn) and returns: sequence_fn, unknown, flat_sequence