Closed
Description
I propose a function/decorator (maybe attempt
would be a good name?) similar to safe
with the twist of returning Failure(bad_input)
from safe(f)(bad_input)
instead of Failure(SomeException)
. An example where this would be useful:
(
attempt(json.loads)('<hello world>')
.alt(lambda s: ValueError(f'"{s}" is not a valid json'))
)
# Failure(ValueError('"<hello world>" is not a valid json'))