# Copyright (c) 2020 CloudZero - ALL RIGHTS RESERVED - PROPRIETARY AND CONFIDENTIAL
# Unauthorized copying of this file and/or project, via any medium is strictly prohibited.
# Direct all questions to legal@cloudzero.com

import asyncio
import functools
from typing import Callable, Iterable


def async_map(function: Callable, arguments: Iterable[Iterable]) -> Iterable:
    """Asyncronously map the synchronous `function` over a list of
    `arguments` using asyncio."""

    async def main():
        loop = asyncio.get_running_loop()
        tasks = [loop.run_in_executor(None, functools.partial(function, *x))
                 for x in arguments]

        return await asyncio.gather(*tasks)

    return asyncio.run(main())
