summaryrefslogtreecommitdiff
path: root/OptionalUtils.py
blob: 51828bb6623f39e4cdab766784555979af6d3c1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

from typing import Optional, TypeVar, Callable



T = TypeVar('T')
def exists(opt: Optional[T], cond: Callable[[T], bool]) -> bool:
    match opt:
        case None:
            return False
        case _:
            return cond(opt)

def foreach(opt: Optional[T], call: Callable[[T], None]) -> None:
    match opt:
        case None:
            return None
        case _:
            return call(opt)