import itertools


def flatten_metas(meta_iterables):
    """
    Take a collection of metas, and compose/flatten/project into a single list.

    For example:

        A: pkg1, pkg2a
        B: pkg2b, pkg3

        Flattened([A, B]) => [pkg1, pkg2a, pkg3]
        Flattened([B, A]) => [pkg1, pkg2b, pkg3]

    The resulting list of metas will not be ordered in any particular way.

    """
    visited = {}
    for metas in meta_iterables:
        visited_this_depth = {}
        for meta in metas:
            if meta.name() not in visited:
                visited_this_depth.setdefault(meta.name(), []).append(meta)
        for name, metas in visited_this_depth.items():
            visited.setdefault(name, []).extend(metas)
    return itertools.chain.from_iterable(visited.values())