import types
import asyncio


def loop_apply_coroutine(loop, func: types.FunctionType, *args, **kwargs) -> object:
    """
    Call a function with the supplied arguments.
    If the result is a coroutine, use the supplied loop to run it.
    """
    if asyncio.iscoroutinefunction(func):
        future = asyncio.ensure_future(
            func(*args, **kwargs), loop=loop)

        loop.run_until_complete(future)
        return future.result()
    else:
        return func(*args, **kwargs)