Comments (14)
@pchlupacek how about a signature that returns IO
?
def runSyncMaybe[A](f: F[A]): IO[Either[F[A], A]]
It's consistent with Effect#runAsync
and the good thing about it is that we can guarantee, by law, that we can do unsafeRunSync
on it, which would be useful for optimizations that involve eliminating unnecessary async boundaries.
from cats-effect.
@rossabaker I can attempt to do this. I'll let you know how it goes.
from cats-effect.
If it were on Async
, it would need to be def syncView(f: F[A]): F[Either[F[A], A]]
. I'm pretty strongly 👎 on putting unsafe functions into typeclasses, which is exactly what your original type signature would be. I'm not sure whether or not the safe version has the same optimization potential.
The other problem with this is it's actually remarkably close to a foldMap
into something like Free[F, A]
. We don't have foldMap
defined, but we do have to
on IO
(which is literally foldMap
, but for Async
) and it isn't hard to define Async[Free[F, ?]]
given Async[F]
. So in other words, if your F
is an IO
, you can already achieve this pretty directly. We don't have to
on Effect
, but it wouldn't be hard to do so.
from cats-effect.
@djspiewak I am ok with F[...]
signature. However rather than on Async, I would like to put that on Sync, if possible. I think is completely ok for Sync F
to alway return F[Right[A]
whereas for F
that do have Async
available to return on left.
I think implementation of this shall not be complicated, is just more to decide on signature and where to put it.
I am not sure if I am following your reference to foldMap.
from cats-effect.
@djspiewak could we agree on something like this on Sync:
/** runs `fa` up to synchronous boundary, either returning result or asynchronous continuation of `f` **/
def runSync[A](fa: F[A]):IO[Either[F[A], A]] = ???
Generally no problem if IO will be replaced by F
, but was sort of inspired by runAsync
on Effect
from cats-effect.
more thinking about it rather F
instead of IO
....
from cats-effect.
I generally like the function, and I think specializing it to IO
(like runAsync
) makes some sense, parametrically. Looping back to the foldMap
reference though, which is what makes me reticent to specialize on something like this when a more general form may exist…
IO
is two free monads glued together: one over a trivial algebra, the other over its CPS duel (which is non-algebraic). Any algebraic monad can define a foldMap
given a natural transformation which maps its algebra into some other monad, which it then reduces (with flatMap
); this is basically just category theory. IO
cannot define this directly, since half of its algebra is… non-algebraic. However, it can define it for the subset of its algebra which is algebraic, provided it can encode a stop case. Something like this, for example:
type Alg[A] = Either[Throwable, () => A]
def foldMap[M[_]: Monad, A](ioa: IO[A])(nt: Alg ~> M): EitherT[M, IO[A], A]
As you can see, this looks remarkably like your function. :-) So you see why I'm hesitating and thinking through ways of generalizing it.
from cats-effect.
Bump -- FS2 team would like to get this in to cats-effect 1.0 in order to implement typelevel/fs2#1058
from cats-effect.
Guys, this isn't an easy issue to solve. So I'm seeing two cases: One would be:
def syncView(f: F[A]): Either[F[A], A]
def syncView(f: F[A]): IO[Either[F[A], A]]
This can't be on less than Effect
. I also believe this cannot be on Sync
:
def syncView(f: F[A]): F[Either[F[A], A]]
It's not obvious, but Sync
or Async
data types do not have the requirement for streaming an A
exactly once. The delay
and async
builders are putting values in the F
context, this is the opposite, taking values out of the F
context. Which is the purpose of Effect
.
Also, I'm not sure if this particular implementation helps you. For example the returned F[Either[F[A], A]]
might not have synchronous execution on evaluation. It's only IO
that can provide that guarantee, due to its current implementation.
So why Sync
?
from cats-effect.
@alexandru thanks a lot to looking into this. I think naming and idea came up before recent changes in IO and type class hierarchy. So lets sort of update it :
What we need is for the streams, that are subject to asynchronous nondeterministic interruption to implement that interruption on almost every frame of F
evaluation of running stream. Almost in this context means we don't want to orchestrate interruption for F
that is known to be not cancellable.
Essentially we need to somehow inspect F[A]
and if that is not cancellable (like now
, suspend
...) we want just await its execution, before moving forward. if F[A]
is interruptible, we want to race with cancelation signal and possibly cancel the F[A]
if the interruption was signalled.
Idea of Sync
was just to have it on lowest possible type class where I thought it is feasible to implement. In interruption currently we require Effect
so if Effect
is required, no problem with that.
I think with new implementation we may require Concurrent
or ConcurrentEffect
instead of Effect
so that functionality may be provided again on lowest possible type denominator where this may be implemented.
from cats-effect.
@pchlupacek so the implementation for IO
already exists and used internally. I felt the need for it as well.
But I'm not sure if we can provide this on anything lower than Effect
. Will look into it.
from cats-effect.
@alexandru thanks a lot. Effect
is fine.
from cats-effect.
Any volunteers to finish this? It seems like there is a general consensus on the approach. It would be great to get this done and keep the 1.0 release on track.
from cats-effect.
I've sent a PR: #195.
from cats-effect.
Related Issues (20)
- `IO#asyncCheckAttempt` is inconsistent with `Async#asyncCheckAttempt`
- Improve contributor documentation HOT 3
- Add (best-effort) stealing API to polling system
- Add API to polling system to attempt to get current poller without shifting HOT 6
- More efficient monitoring of fibers on virtual threads HOT 1
- Published tutorial older than tutorial.md HOT 1
- OutOfMemoryError when IO.uncancelable is used in recursive function HOT 3
- unsafeToFutureCancelable's cancel future completes before setting the result future's cancellation status HOT 7
- unsafeToFuture fails to raise a second InterruptedException if the exception is handled and retried HOT 6
- Support for V8 JavaScript Engine? HOT 2
- IO.onError is inconsistent with ApplicativeError HOT 5
- `AsyncSpec` failure (ciJS)
- When `IORuntime.global` shuts down, it doesn't shut down the WSTP HOT 1
- Queued but unstarted work on `Dispatcher.sequential` should be cancelable HOT 6
- Release for Scala Native 0.5.x HOT 1
- Consolidate duplicate "currently executing fiber" pointers
- Potential inconsistency between joinWithNever and docs HOT 1
- Add `mdoc` guards to Test Runtime documentation
- TestControl + Dispatcher == deadlock HOT 13
- doesn't work with scala 3 + scala-native HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cats-effect.