Merge branch 'main' into the-lab

This commit is contained in:
Emi Simpson 2023-02-12 16:58:14 -05:00
commit c71ea2616e
Signed by: Emi
GPG Key ID: A12F2C2FFDC3D847
1 changed files with 7 additions and 4 deletions

View File

@ -530,21 +530,24 @@ def bind_opt(f: Callable[[A], Option[B]], o: Option[A]) -> Option[B]:
return f(val)
case none:
return none
def note(e: B, o: Option[A]) -> 'Result[A, B]':
def note(e: Callable[[], B], o: Option[A]) -> 'Result[A, B]':
"""
Convert an `Option` to a `Result` by attaching an error to the `None` variants
>>> note('woops!', Some(1))
`e` should be a zero-argument function which produces the desired error value. It
will be called if and only if `o` is `None`.
>>> note(lambda: 'woops!', Some(1))
Ok(1)
>>> note('woops!', None)
>>> note(lambda: 'woops!', None)
Err('woops!')
"""
match o:
case Some(val):
return Ok(val)
case None:
return Err(e)
return Err(e())
def unwrap_opt(r: Option[A]) -> A:
"""