Make note use lazy evaluation

This commit is contained in:
Emi Simpson 2023-02-12 16:57:23 -05:00
parent 2665f8ff98
commit d7dab8e4b0
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847

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:
"""