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

Add two new combinators: cedeMap and intercede #3353

Open
wants to merge 6 commits into
base: series/3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions core/shared/src/main/scala/cats/effect/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,20 @@ sealed abstract class IO[+A] private () extends IOPlatform[A] {
*/
def map[B](f: A => B): IO[B] = IO.Map(this, f, Tracing.calculateTracingEvent(f))

/**
* Functor map, but causes a reschedule before and after `f`. For more information, checkout
* `cede` in the companion object.
*/
def cedeMap[B](f: A => B): IO[B] =
(this <* IO.cede).map(a => f(a)).guarantee(IO.cede)

/**
* Causes a reschedule before and after `fa`. For more information, checkout `cede` in the
* companion object.
*/
def intercede: IO[A] =
IO.cede *> this.guarantee(IO.cede)
Comment on lines +500 to +512
Copy link
Member

Choose a reason for hiding this comment

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

Nice!

Would you mind adding versions of these methods to GenSpawn in the kernel module? These would be more similar to your original definitions, which are in F[_] and take the effect as an argument (instead of instance methods).


/**
* Applies rate limiting to this `IO` based on provided backpressure semantics.
*
Expand Down
12 changes: 12 additions & 0 deletions kernel/shared/src/main/scala/cats/effect/kernel/GenSpawn.scala
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,18 @@ trait GenSpawn[F[_], E] extends MonadCancel[F, E] with Unique[F] {
*/
def cede: F[Unit]
Comment on lines 282 to 283
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
*/
def cede: F[Unit]
*
* @see [[cedeMap]], [[intercede]]
*/
def cede: F[Unit]


/**
* Functor map, but causes a reschedule before and after `f`
*/
Comment on lines +285 to +287
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/**
* Functor map, but causes a reschedule before and after `f`
*/
/**
* Like functor [[map]], but inserts a [[cede]] before and after `f`. Use this when `f` is a long-running, compute-bound operation.
*
* @see [[cede]] for more details
*/

def cedeMap[A, B](fa: F[A])(f: A => B): F[B] =
(fa <* cede).map(a => f(a)).guarantee(cede)

/**
* Causes a reschedule before and after `fa`
*/
Comment on lines +291 to +293
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/**
* Causes a reschedule before and after `fa`
*/
/**
* Insert a [[cede]] before and after `fa`. Use this when `fa` involves a long-running, compute-bound operation outside of the monadic context.
*
* @see [[cede]] for more details
*/

def intercede[A](fa: F[A]): F[A] =
cede *> fa.guarantee(cede)
Comment on lines +285 to +295
Copy link
Member

Choose a reason for hiding this comment

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

Excellent! :)

If you don't mind, there is one more place we need to add these methods, which is as syntax here:

final class GenSpawnOps[F[_], A, E] private[syntax] (private val wrapped: F[A]) extends AnyVal {

These should delegate directly to the typeclass instance, instead of duplicating the implementation.

Copy link
Author

Choose a reason for hiding this comment

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

No problem!


/**
* A low-level primitive for racing the evaluation of two fibers that returns the [[Outcome]]
* of the winner and the [[Fiber]] of the loser. The winner of the race is considered to be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,10 @@ final class GenSpawnOps[F[_], A, E] private[syntax] (private val wrapped: F[A])
implicit F: GenSpawn[F, E]
): F[(Outcome[F, E, A], Outcome[F, E, B])] =
F.bothOutcome(wrapped, another)

def cedeMap[B](f: A => B)(implicit F: GenSpawn[F, E]): F[B] =
F.cedeMap(wrapped)(f)

def intercede(implicit F: GenSpawn[F, E]): F[A] =
F.intercede(wrapped)
}
13 changes: 12 additions & 1 deletion kernel/shared/src/test/scala/cats/effect/kernel/SyntaxSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ class SyntaxSpec extends Specification {
}
}

def genSpawnSyntax[F[_], A, B, E](target: F[A], another: F[B])(implicit F: GenSpawn[F, E]) = {
def genSpawnSyntax[F[_], A, B, E](target: F[A], another: F[B], f: A => B)(
implicit F: GenSpawn[F, E]) = {
import syntax.spawn._

GenSpawn[F]: F.type
Expand Down Expand Up @@ -139,6 +140,16 @@ class SyntaxSpec extends Specification {
val result = target.bothOutcome(another)
result: F[(Outcome[F, E, A], Outcome[F, E, B])]
}

{
val result = target.cedeMap(f)
result: F[B]
}

{
val result = target.intercede
result: F[A]
}
Comment on lines +149 to +152
Copy link
Member

Choose a reason for hiding this comment

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

Excellent, thanks for adding this test! Would you mind adding a test for cedeMap as well?

Copy link
Author

@biuld biuld Jan 13, 2023

Choose a reason for hiding this comment

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

Actually the reason that I leave out the test for cedeMap is I don't know how to obtain f: A => B in test. I might need some time on getting familiar with how specs2 work. 😂

Copy link
Member

Choose a reason for hiding this comment

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

@biuld just add f: A => B to the function arguments here:

def genSpawnSyntax[F[_], A, B, E](target: F[A], another: F[B])(implicit F: GenSpawn[F, E]) = {

Copy link
Author

Choose a reason for hiding this comment

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

Done.

}

def spawnForwarder[F[_]: Spawn] =
Expand Down