code: def map_(function, *structures, **kwargs):
    # pylint: disable=differing-param-doc,missing-param-doc
    
    # Named keyword arguments are not allowed after *args in Python 2.
    flatten = kwargs.pop('flatten', False)
    assert not kwargs, 'map() got unexpected keyword arguments.'
  
    def impl(function, *structures):
      if len(structures) == 0:  # pylint: disable=len-as-condition
        return structures
      if all(isinstance(s, (tuple, list)) for s in structures):
        if len(set(len(x) for x in structures)) > 1:
          raise ValueError('Cannot merge tuples or lists of different length.')
        args = tuple((impl(function, *x) for x in _builtin_zip(*structures)))
        if hasattr(structures[0], '_fields'):  # namedtuple
          return type(structures[0])(*args)
        else:  # tuple, list
          return type(structures[0])(args)
      if all(isinstance(s, dict) for s in structures):
        if len(set(frozenset(x.keys()) for x in structures)) > 1:
          raise ValueError('Cannot merge dicts with different keys.')
        merged = {
            k: impl(function, *(s[k] for s in structures))
            for k in structures[0]}
        return type(structures[0])(merged)
      return function(*structures)
  
    result = impl(function, *structures)
    if flatten:
      result = flatten_(result)
    return result
docs: Apply a function to every element in a nested structure.
  
    If multiple structures are provided as input, their structure must match and
    the function will be applied to corresponding groups of elements. The nested
    structure can consist of any combination of lists, tuples, and dicts.
  
    Args:
      function: The function to apply to the elements of the structure. Receives
          one argument for every structure that is provided.
      *structures: One of more nested structures.
      flatten: Whether to flatten the resulting structure into a tuple. Keys of
          dictionaries will be discarded.
  
    Returns:
      Nested structure.