returns icon indicating copy to clipboard operation
returns copied to clipboard

Think about lazy loop method

Open sobolevn opened this issue 5 years ago • 4 comments

Imagine that you have a list of 3 elements. And you are going to use Fold.loop on it. The first one is Failure(1).

Currently, loop will iterate over all 3 elements. With no breakpoints. But, Failure(1) is a roadblock for any other logic. We should not iterate over other values.

We need to think about best solutions here. Criteria:

  • It should ideally work with any FailableN
  • It should be similar to loop in semantics
  • It should be possible to use loop or loop_lazily on the same functions / helpers. In other words, it should be easy to switch from one to another
  • It should be faster than loop

Ideas?

sobolevn avatar Sep 28 '20 11:09 sobolevn

This seems to be solving the same problem

  1. https://docs.rs/itertools/0.5.9/itertools/trait.Itertools.html#method.fold_results, https://docs.rs/itertools/0.9.0/itertools/trait.Itertools.html#method.fold_options -- pretty straightforward and aiming at results and options respectively
  2. https://docs.rs/itertools/0.5.9/itertools/trait.Itertools.html#method.fold_while -- more general, but requires a function to be aware of calling convention, which is somewhat inconvenient. Can be used as underlying building block when implementing for more specific cases?

makarchuk avatar Oct 27 '20 11:10 makarchuk

@makarchuk yes, that's exactly the same 🙂

We just need to think about the API and implement a single fold_failable in https://github.com/dry-python/returns/blob/master/returns/iterables.py

Do you want to work on it? 🙂

sobolevn avatar Oct 27 '20 11:10 sobolevn

I do, but currently I'm struggling to wrap my head around type signatures you're using. HKT is a concept I've known for a while, but never understood, so I'm stuck at figuring out signature of loop_failable at the moment.

I'm not sure what a signature of Callable should be. It should be something like Result[_UpdatedType, _ErrorType] but that's not flexible enough (only supports Results, which is not the objective here) and I really don't get how FailableN which I presumably need to use, works.

makarchuk avatar Oct 27 '20 13:10 makarchuk

You can start small without any signatures at all.

I really don't get how FailableN which I presumably need to use, works.

It is a common interfaces for all types that can fail, like Result or Maybe or IOResult or FutureResult. Think of it as a Container with lash method.

sobolevn avatar Oct 27 '20 15:10 sobolevn