from typing import Sequence
from typing import Callable
from typing import Any
from functools import reduce


def compose(functions: Sequence[Callable]) -> Callable:
    """
    Compose a sequence of functions
    :param functions: sequence of functions
    :return: combined functions, e.g. [f(x), g(x)] -> g(f(x))
    """

    def func(f: Callable, g: Callable) -> Callable:
        def func2(*x) -> Any:
            res = g(*x)
            if type(res) == bool:
                return f(*x)
            else:
                return f(*res)

        return func2

    return reduce(func, reversed(functions), lambda *x: x)