-
-
Notifications
You must be signed in to change notification settings - Fork 125
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
Add safe_call
, Expected
, rename of
to call
#691
Comments
Actually, maybe a better definition so you can continue the pipeline with data Left[E <: Exception](error: E):
def __fmap__(self, _) = self
data Right[T](result: T)
type Either[T] = Left | Right[T]
def safe_call(_f, *args, **kwargs):
try:
return Right(_f(*args, **kwargs))
except Exception as err:
return Left(err) |
maybe even better, to mimic the original tuple definition if you want to use it that way too: data _safe_call_result[T](result: T?, error: Exception? = None):
def __fmap__(self, func) = self if self.error is not None else Result(func(self.result))
def __bool__(self) = self.error is None
def safe_call(_f, *args, **kwargs):
try:
return _safe_call_result(_f(*args, **kwargs))
except Exception as err:
return _safe_call_result(None, err) |
Maybe also provide |
evhub
changed the title
Rename
Add Dec 6, 2022
of
to call
, add safe_call
safe_call
, Expected
, rename of
to call
Final definition of data Expected[T](result: T?, error: Exception?):
def __new__(cls, result: T?=None, error: Exception?=None) -> Expected[T]:
if result is not None and error is not None:
raise ValueError("Expected cannot have both a result and an error")
return makedata(cls, result, error)
def __bool__(self) -> bool:
return self.error is None
def __fmap__[U](self, func: T -> U) -> Expected[U]:
return self.__class__(func(self.result)) if self else self |
evhub
added a commit
that referenced
this issue
Dec 7, 2022
evhub
added a commit
that referenced
this issue
Dec 25, 2022
evhub
added a commit
that referenced
this issue
Dec 25, 2022
evhub
added a commit
that referenced
this issue
Dec 25, 2022
Merged
evhub
added a commit
that referenced
this issue
Dec 30, 2022
evhub
added a commit
that referenced
this issue
Dec 30, 2022
evhub
added a commit
that referenced
this issue
Dec 30, 2022
evhub
added a commit
that referenced
this issue
Dec 30, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should keep
of
as a deprecated alias forcall
that's removed in--strict
compilation.Definition of
safe_call
:safe_call
is useful for making long pipelines / point-free programming with exceptions.The text was updated successfully, but these errors were encountered: