-
Notifications
You must be signed in to change notification settings - Fork 529
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
base: series/3.x
Are you sure you want to change the base?
Changes from all commits
ace83fc
afe219a
d6f9f68
9b2ba64
ed15013
a5b8670
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* Functor map, but causes a reschedule before and after `f` | ||||||||||||||||||
*/ | ||||||||||||||||||
Comment on lines
+285
to
+287
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
def intercede[A](fa: F[A]): F[A] = | ||||||||||||||||||
cede *> fa.guarantee(cede) | ||||||||||||||||||
Comment on lines
+285
to
+295
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: cats-effect/kernel/shared/src/main/scala/cats/effect/kernel/syntax/GenSpawnSyntax.scala Line 45 in 130fdcc
These should delegate directly to the typeclass instance, instead of duplicating the implementation. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -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 | ||||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually the reason that I leave out the test for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @biuld just add
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||
} | ||||
|
||||
def spawnForwarder[F[_]: Spawn] = | ||||
|
There was a problem hiding this comment.
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 inF[_]
and take the effect as an argument (instead of instance methods).cats-effect/kernel/shared/src/main/scala/cats/effect/kernel/GenSpawn.scala
Line 283 in 130fdcc