From d7dab8e4b0474f393fa75bc0fe3b3c5c6458389e Mon Sep 17 00:00:00 2001 From: Emi Simpson Date: Sun, 12 Feb 2023 16:57:23 -0500 Subject: [PATCH] Make note use lazy evaluation --- emis_funky_funktions.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/emis_funky_funktions.py b/emis_funky_funktions.py index 1fec916..802b522 100644 --- a/emis_funky_funktions.py +++ b/emis_funky_funktions.py @@ -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: """