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 3 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
21 changes: 21 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,27 @@ 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] =
for {
a <- fa
_ <- cede
b = f(a)
_ <- cede
} yield b
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for adding these! Is there a particular reason you wrote them as for ... yield? Unfortunately it's not quite correct, because it's missing the guarantee(...). Also, in my personal opinion, it's a bit harder to read :)

Copy link
Author

@biuld biuld Jan 11, 2023

Choose a reason for hiding this comment

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

Is there a particular reason you wrote them as for ... yield?

No, it's I just can't find *> and <* combinators here. 🤦

Copy link
Member

Choose a reason for hiding this comment

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

Huh, were they not compiling? They come from here:

Copy link
Author

@biuld biuld Jan 11, 2023

Choose a reason for hiding this comment

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

Oh, I missed the fact that MonadCancel is cats Monad! Will change that in a minute.


/**
* 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] =
for {
_ <- cede
a <- fa
_ <- cede
} yield a

/**
* 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