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
Proposal: add Task.flatMapLoop method #995
Comments
Btw, this helper is super obvious — is it described anywhere? In Cats maybe? |
I can take a stab at it if you want. |
👍 let's call it flatMapLoop, has a nicer ring to it. Also search for an equivalent in Cats first. |
One issue we need to solve is to get the signature right — this function looks like a def foldLeft[A, B](fa: F[A], b: B)(f: (B, A) => B): B This is consistent with the definition from Haskell: foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b In other words maybe the signature should be: def flatMapLoop[S](seed: S)(f: (S, A, S => Task[S]) => Task[S]): Task[S] On the other hand we have the problem that we already described: def onErrorRestartLoop[S, B >: A](initial: S)(f: (Throwable, S, S => Task[B]) => Task[B]): Task[B] This would be a problem. And I think we might have gotten this signature wrong. |
Note that foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b def foldRight[A, B](fa: F[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] So maybe this is more like a foldRight 😂 |
PR coming tonight (US Mountain Time). I was not able to find a similar method in cats. Currently working on a couple of test cases. Regarding the signature do you want to keep it as is or change it? |
Consistency with |
Will be delayed til tomorrow |
Anything else needed in the PR? |
Thank you for the PR @mudsam and sorry for the delay! |
I think this method would be cool to have on
Task
:Usage sample:
This saves us from describing a loop via
def
, which I find super annoying:So it's a syntactic helper that's similar with
onErrorRestartLoop
.TODO:
foldLeft
, however usually the order of params infoldLeft
in Scala is(S, A)
and not(A, S)
(like we're doing here)Coeval
The text was updated successfully, but these errors were encountered: