-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow ability to extend a sweep with a prior default value #5
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from __future__ import annotations | ||
from typing import Callable, Generic, TypeVar | ||
|
||
|
||
T = TypeVar('T') | ||
U = TypeVar('U') | ||
|
||
class Maybe(Generic[T]): | ||
def __init__(self, v: T | None): | ||
self._v: T | None = v | ||
|
||
|
||
def map(self, f: Callable[[T], U | None]) -> Maybe[U]: | ||
if self._v is None: | ||
return Maybe[U](None) | ||
|
||
u = f(self._v) | ||
return Maybe(u) | ||
|
||
|
||
def flat_map(self, f: Callable[[T], Maybe[U]]) -> Maybe[U]: | ||
if self._v is None: | ||
return Maybe[U](None) | ||
|
||
return f(self._v) | ||
|
||
|
||
def flat_otherwise(self, f: Callable[[], Maybe[T]]) -> Maybe[T]: | ||
if self._v is None: | ||
return f() | ||
|
||
return self | ||
|
||
|
||
def or_else(self, t: T) -> T: | ||
if self._v is None: | ||
return t | ||
|
||
return self._v | ||
|
||
|
||
def expect(self, msg: str = '') -> T: | ||
if self._v is None: | ||
raise Exception(msg) | ||
|
||
return self._v | ||
|
||
|
||
def is_none(self): | ||
return self._v is None | ||
|
||
|
||
def is_some(self): | ||
return self._v is not None |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ version_files = ["pyproject.toml"] | |
|
||
[tool.ruff.lint] | ||
select = ['F', 'E', 'W', 'B'] | ||
ignore = ['E501', 'E701'] | ||
ignore = ['E501', 'E701', 'B023'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new warning is for unbound variable captures in a for loop. Python really isn't a great language for functional programming. Something like this: obj = {}
for i in range(10):
call_later(lambda: obj['a'] = i) depending on when that With the |
||
|
||
[tool.pyright] | ||
include = ['ml_experiment'] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a
Maybe
monad. It's an algebraic type, meaning that transformations (e.g. viamap
andotherwise
) are type-preserving --- they also return aMaybe
. The transformation may alter the interior type, i.e.Maybe[str]
might become aMaybe[int]
in the following:Maybe
monads are quite helpful when you have complex "nullable" logic where you might go down one of many code paths depending on if any of those code paths return aNone
.Ideally, we would refactor a lot of the internal code to use
Maybe[T]
instead ofT | None
for consistency and to reduce severalif x is None: ...
guards.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I will work on adding
maybe
monad's to other parts