summaryrefslogtreecommitdiff
path: root/OptionalUtils.py
blob: 654120833281b2ceffab67fdf0431ada79ac150c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

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)

def get(opt: Optional[T]) -> T:
    match opt:
        case None: raise ValueError("get on None")
        case _:
            return opt