from typing import Callable
from typing import Any
from typing import Sequence


def foldl(fun: Callable[[Any, Any], Any], acc: Any, seq: Sequence[Any]) -> Any:
    """Implementation of foldl in Python3.

    This is an implementation of the left-handed
    fold function from functional programming.

    If the list is empty, we return the accumulator
    value. Otherwise, we recurse with a new accumulator
    value which is a result of the function application
    on the old accumulator value and the head of the list.

    Below are the implementations of the len
    and sum functions using foldl to
    demonstrate how foldl function works.

    >>> foldl((lambda x, _: x + 1), 0, [0, 1, 2, 3, 4])
    5
    >>> foldl((lambda x, y: x + y), 0, [0, 1, 2, 3, 4])
    10

    foldl takes the second argument and the
    first item of the list and applies the function
    to them, then feeds the function with this result
    and the second argument and so on.
    """
    return acc if not seq else foldl(fun, fun(acc, seq[0]), seq[1:])