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

Conversation

biuld
Copy link

@biuld biuld commented Jan 9, 2023

This pull request resolves #3318.

Comment on lines 1319 to 1329
/**
* Functor map, but causes a reschedule before and after `f`
*/
def cedeMap[A, B](fa: IO[A])(f: A => B): IO[B] =
(fa <* cede).map(a => f(a)).guarantee(cede)

/**
* causes a reschedule before and after `fa`
*/
def intercede[A](fa: IO[A]): IO[A] =
cede *> fa.guarantee(cede)
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 opening the PR! What would you think about moving these methods onto the IO instance, so that instead of

IO.cedeMap(ioa)(a => foo(a))
IO.intercede(ioa)

users can write

ioa.cedeMap(a => foo(a))
ioa.intercede

However, the methods as written here would be an excellent addition to GenSpawn in the kernel module.

Let me know if you have any questions!

Copy link
Author

Choose a reason for hiding this comment

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

True! cedeMap and intercede as instance methods would be less boilerplate.

Comment on lines +500 to +512
/**
* 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)
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).

Comment on lines 288 to 294
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.

Comment on lines +285 to +295
/**
* Functor map, but causes a reschedule before and after `f`
*/
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`
*/
def intercede[A](fa: F[A]): F[A] =
cede *> fa.guarantee(cede)
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!

Copy link
Member

@armanbilge armanbilge left a comment

Choose a reason for hiding this comment

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

Looking great! Just a couple docs tweaks.

Comment on lines +285 to +287
/**
* Functor map, but causes a reschedule before and after `f`
*/
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
*/

Comment on lines +291 to +293
/**
* Causes a reschedule before and after `fa`
*/
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
*/

Comment on lines 282 to 283
*/
def cede: F[Unit]
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]

Comment on lines +143 to +146
{
val result = target.intercede
result: F[A]
}
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.

Copy link
Member

@armanbilge armanbilge left a comment

Choose a reason for hiding this comment

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

👍 thanks for all your good work on this! But someone else should bikeshed the names because I just made them up :)

@armanbilge armanbilge added this to the v3.5.0 milestone Feb 4, 2023
@djspiewak
Copy link
Member

So I genuinely love the idea of these combinators. I'm not 100% sold on the API, but I'm thinking about it.

@djspiewak
Copy link
Member

Going to punt these from 3.5 just to give us a bit more time to bikeshed. Like I said, I absolutely love the concept I just want to make sure we get the API right (not just the names, but also the ergonomics). Outstanding work, again!

@djspiewak djspiewak modified the milestones: v3.5.0, v3.6.0 Feb 14, 2023
Copy link
Contributor

@BalmungSan BalmungSan left a comment

Choose a reason for hiding this comment

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

Leaving my two cents:

  1. Personally I would prefer a more intuitive name like computeMap over cedeMap. Of course, the Scaladoc should mention and link cede.
  2. In a similar vein, I would like to see two other static combinators like:
object IO {
  def compute[A](thunk: => A): IO[A] =
      IO.cede >> IO.pure(thunk) >> IO.cede // Not sure if delay + guarantee would be better.

  def attemptCompute[A](thunk: => Either[Throwable, A]): IO[A] =
    IO.cede >> IO.fromEither(thunk) >> IO.cede // ditto delay + guarantee.
}

These may be in another PR tho.
Especially since those should also be added to Async

@djspiewak djspiewak removed this from the v3.6.0 milestone Feb 19, 2024
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.

"cedeMap" and "intercede" combinators
4 participants