diff --git a/emis_funky_funktions.py b/emis_funky_funktions.py index 4e5f0ae..54a1706 100644 --- a/emis_funky_funktions.py +++ b/emis_funky_funktions.py @@ -24,6 +24,7 @@ def c(f2: Callable[[B], C], f1: Callable[P, B]) -> Callable[P, C]: >>> double = lambda x: x + x >>> succ = lambda x: x + 1 + >>> c(double, succ)(1) 4 @@ -44,6 +45,7 @@ def flip(f: Callable[P1, Callable[P2, C]]) -> Callable[P2, Callable[P1, C]]: `flip` if the arguments you want to flip are not curried. >>> pair = lambda x: lambda y: (x, y) + >>> pair(1)(2) (1, 2) @@ -82,7 +84,6 @@ def replace(replace_with: A) -> Callable[..., A]: >>> always_seven = replace(7) >>> always_seven(2) 7 - >>> always_seven('hello', 'world!') 7 @@ -545,6 +546,28 @@ def note(e: B, o: Option[A]) -> 'Result[A, B]': case None: return Err(e) +def unwrap_opt(r: Option[A]) -> A: + """ + Assert that an `Option` is `Some` and return it's value. + + Throws: + `AssertionError` - The result was NOT okay. The `AssertionError` will have two + arguments: The first is a string to make it more obvious what happened. The + second is the error that was stored in the `Err`. + + >>> unwrap_opt(Some('hai!')) + 'hai!' + + >>> unwrap_opt(None) #doctest: +IGNORE_EXCEPTION_DETAIL + Traceback (most recent call last): + AssertionError: ('Tried to unwrap a None value') + """ + match r: + case Some(val): + return val + case None: + raise AssertionError('Tried to unwrap a None value') + # Results! @dataclass(frozen=True) class Ok(Generic[A]):