-
-
Notifications
You must be signed in to change notification settings - Fork 128
Consider adding LazyIO and friends #525
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
Comments
But, when talking about composition, we need to give it a helping hand: Lazy = Callable[[], _ValueType]
class LazyIO(...):
inner_value: Lazy[IO[_ValueType]]
def __init__(self, inner_value: Lazy[IO[_ValueType]]) -> None: ...
def __call__(self) -> IO[_ValueType]: ...
def map(self, function: Callable[[_ValueType], _NewValueType]) -> LazyIO[_NewValueType]: ...
def lazy(inner_value: _ValueType) -> Lazy[_ValueType]: ...
def lazy_impure(function: Callable[..., _ValueType]) -> Callable[..., LazyIO[_ValueType]: ... It is much better than working with |
This can be useful to create something like def example(arg: str) -> Result[int, str]:
return lazy_cond(Result, arg.isnumeric(), Lazy(int), 'Is not a number')
assert example('42') == Success(42)
assert example('string') == Failure('string') Today is impossible to make something similar and simple |
Well, I was wrong. |
I know the documentation mentions Taking my personal experience as an example, I learned explicitly that the In my example I started in earnest with reading https://github.com/MostlyAdequate/mostly-adequate-guide, maybe my experience is not representative of everyone's. I am just not as sure the value of a simple |
The thing about
IO
in multiple languages / libraries is that it is lazy.Our
IO
is not lazy by design. It is done, so Python developers can use it like so:impure(print)(1) # prints "1"
But, we also need to think about other problems as well:
IO
one can retry an operation as many times as one wishes:p = impure_lazy(print)(1); p(); p() # prints "1" twice
Future
, currently it is not similar to regularIO
, becauseFuture
s are lazy: they don't run until they are executed properlyThe text was updated successfully, but these errors were encountered: