from typing import Optional
from typing import Callable
from typing import Any
from typing import List


def as_list(ans: str, sep: str = ',', f: Optional[Callable[[str], Any]] = None) -> List[Any]:
    """
    Parse list elements and optionally apply a transformer to each element.
    This allows you to parse things directly as the type they should be, e.g. integers:
    as_list("3, 4", f=int) == [3, 4]
    """
    items = ans.strip().split(sep)
    items = map(str.strip, items)
    if f is not None:
        items = map(f, items)
    return list(items)