Skip to content
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

Introduce parReplicateA #4007

Merged
merged 4 commits into from
Oct 26, 2021
Merged

Conversation

RafalSumislawski
Copy link
Contributor

This PR introduces parReplicateA as a parallel version of Applicative.replicateA.

Comment on lines 110 to 116
/**
* Like [[Applicative.replicateA]], but uses the apply instance
* corresponding to the Parallel instance instead.
*/
def parReplicateA[A](n: Int, ma: M[A]): M[List[A]] =
if (n <= 0) monad.pure(List.empty[A])
else Parallel.parSequence(List.fill(n)(ma))(UnorderedFoldable.catsTraverseForList, this)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me it seems that trait Parallel is designed to just represent a relationship between a Monad and Applicative with parallel composition. But most of complimentary methods are placed into object Parallel (including parAp per se). I'm not absolutely confident, but shouldn't parReplicateA be also placed in the object rather than the trait?

Comment on lines 250 to 251
if (n <= 0) P.monad.pure(List.empty[A])
else Parallel.parSequence(List.fill(n)(ma))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess these two lines can be replaced with just P.parReplicateA(...) if you decide to leave Parallel#parReplicateA in the trait Parallel or with Parallel.parReplicateA(...) if you move it into the object. Just to avoid code duplication.

@@ -184,6 +184,7 @@ object SyntaxSuite {
val mb4: M[B] = ma.parProductR(mb)
val mab2: M[(A, B)] = ma.parProduct(mb)
val mb5: M[B] = mab.parAp(ma)
val mla: M[List[A]] = ma.parReplicateA(3)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mock[Int] instead of just 3?

@RafalSumislawski
Copy link
Contributor Author

Hello @satorg I agree with you comments and so I've pushed corresponding changes.

satorg
satorg previously approved these changes Oct 6, 2021
Copy link
Contributor

@satorg satorg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks.

Although I think it will be also appreciated if you add a corresponding test case to ParallelSuite.

@RafalSumislawski
Copy link
Contributor Author

Hello @satorg I added some tests as you've requested.

LukaJCB
LukaJCB previously approved these changes Oct 14, 2021
Copy link
Member

@LukaJCB LukaJCB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thanks! :)

satorg
satorg previously approved these changes Oct 14, 2021
Copy link
Contributor

@satorg satorg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thx!

* Like `Applicative[F].replicateA`, but uses the apply instance
* corresponding to the Parallel instance instead.
*/
def parReplicateA[M[_], A](n: Int, ma: M[A])(implicit P: Parallel[M]): M[List[A]] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not P.applicative.replicateA(n, ma)?

Since in principle, that implementation could contain some optimization/specialization of some kind.

I think that is a better than having a second implementation here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has to be a bit more complicated than that.

P.sequential(P.applicative.replicateA(n, P.parallel(ma)))

But yes, I agree, it's better to reuse it.

@RafalSumislawski RafalSumislawski dismissed stale reviews from satorg and LukaJCB via 856e2b4 October 22, 2021 19:04
@LukaJCB LukaJCB merged commit 76c069d into typelevel:main Oct 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants