summaryrefslogtreecommitdiff
path: root/OptionalUtils.py
diff options
context:
space:
mode:
Diffstat (limited to 'OptionalUtils.py')
-rw-r--r--OptionalUtils.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/OptionalUtils.py b/OptionalUtils.py
new file mode 100644
index 0000000..51828bb
--- /dev/null
+++ b/OptionalUtils.py
@@ -0,0 +1,19 @@
+
+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)