Comments (13)
What are you seeing output?
from language-ext.
After each second I see *, but I can't break it
from language-ext.
Works for me, so I'll need much more info. This is what I see output.
Forks a process that runs 10 times, summing a value each time.
If you press enter before the 10 iterations then the forked process will be cancelled
*
*
done
Returning to menu in 5
*
total: 3
Returning to menu in 4
Returning to menu in 3
Returning to menu in 2
Returning to menu in 1
from language-ext.
Yes, I expected the same behaviour as you have demonstrated to me, but console don't want to accept any keys
from language-ext.
Sorry, just noticed this isn't exactly the same as the one in the Effects project. I'll try yours.
from language-ext.
Yes, yours example works, but my doesn't
public class ForkCancelExample<RT>
where RT: struct,
HasCancel<RT>,
HasConsole<RT>,
HasTime<RT>
{
public static Aff<RT, Unit> main =>
from cancel in fork(inner)
from key in Console<RT>.readKey
from _1 in cancel
from _2 in Console<RT>.writeLine("done")
select unit;
static Aff<RT, Unit> inner =>
from x in sum
from _ in Console<RT>.writeLine($"total: {x}")
select unit;
static Aff<RT, int> sum =>
digit.Fold(Schedule.Recurs(10) | Schedule.Spaced(1*second), 0, (s, x) => s + x);
static Aff<RT, int> digit =>
from one in SuccessAff<RT, int>(1)
from _ in Console<RT>.writeLine("*")
select one;
}
from language-ext.
I don't think that version of repeat
is paying attention to the cancellation-token, leave it with me.
from language-ext.
Ok, thank you!
from language-ext.
Ah, no, it's correct. The body of your repeat
is an Eff
. As you can see from the Eff
version of repeat
, there is no HasCancel<RT>
trait unlike the Aff
version of repeat
Which means the whole thing is synchronous and therefore can't access any CancellationToken
in the runtime. You need to convert the body to an Aff
for it to work.
static Aff<RT, Unit> inner =>
repeat(Schedule.spaced(1 * sec),
Console<RT>.writeLine("*").ToAff());
from language-ext.
Now I see, thanks.
But it is not evidently that I can fork an Aff
which has body of Eff
which in turn isn't cancelable. I thought that implicit conversion from Eff
to Aff
makes Eff
full fledged citizen of affect world. But the truth is I need to lift Eff
inside body of repeat
explicit. For me turning (from _ in Console<RT>.writeLine("*") select unit).ToAff()
looks like workaround.
public class ForkCancelExample<RT>
where RT : struct,
HasCancel<RT>,
HasConsole<RT>,
HasTime<RT>
{
public static Aff<RT, Unit> main =>
from cancel in fork(inner)
from key in Console<RT>.readKey
from _1 in cancel
from _2 in Console<RT>.writeLine("done")
select unit;
static Aff<RT, Unit> inner =>
repeat(Schedule.spaced(1 * sec), innerInner);
static Aff<RT, Unit> innerInner =>
from _ in Console<RT>.writeLine("*")
select unit;
}
In the example above, all is fine, but innerInner property looks like artificial step
public class ForkCancelExample<RT>
where RT : struct,
HasCancel<RT>,
HasConsole<RT>,
HasTime<RT>
{
public static Aff<RT, Unit> main =>
from cancel in fork(inner.ToAff())
from key in Console<RT>.readKey
from _1 in cancel
from _2 in Console<RT>.writeLine("done")
select unit;
static Eff<RT, Unit> inner =>
repeat(Schedule.spaced(1 * sec),
from _ in Console<RT>.writeLine("*")
select unit);
}
in the example above, is not
If some team work on the library and develop an sync effect to expose to other team via declaration as Eff
, other team can treat it like cancelable by turn it to Aff
using ToAff
and doesn't know anything about internals of Eff
and can lead to this situation.
The nature of functional, composable world enforces us to look at implementation details or maybe is it possible to treat all inside Eff
like Aff
recomposing them in the Aff
root of composition?
from language-ext.
I thought that implicit conversion from Eff to Aff makes Eff full fledged citizen of affect world.
I realise it may not be obvious at first, but when you think that HasCancel<RT>
doesn't exist in the Eff
space then it should be clear. Any conversion from Eff
to Aff
can only ever wrap the inner Eff
in an outer Aff
, it can't fundamentally go into the body of the Eff
and start converting your code to asynchronous code.
This is all moot however, because the cancellation system will be different as part of of the transducers overhaul. The transducers all carry a TState
through the reducer computation. The TState
contains a cancellation-token (and a collection of used resources for auto-disposal).
That means everything built with transducers will have access to a cancellation-token, synchronous or not. The plan is to replace the inner workings of most of the monadic types, including Eff
and Aff
with transducers.
Right now I'm just working on some prototypes to remove the need for Transducer
and TransducerAsync
('green threads' for transducers basically), to minimise the surface of the API and to make the whole thing much more elegant (and hopefully performant).
I'll should start the process of implementing in language-ext very soon.
from language-ext.
can't fundamentally go into the body of the
Eff
and start converting your code to asynchronous code.
that is answer to my question
The plan is to replace the inner workings of most of the monadic types, including
Eff
andAff
with transducers.
I want to say using following words
Iām excited about transducers and the power they bring, and I hope you are too!
Rich Hickey
from language-ext.
Interesting problem, do you think this behavior could somehow be restricted statically rather than noticing during runtime?
from language-ext.
Related Issues (20)
- Creating a custom Either type with a custom fixed Left type. HOT 1
- Conflict between LangugeExt Extension methods and LINQ Extension methods HOT 2
- AffRxExtensions.Consume blocks the waiting thread on IObservable's completion
- How to make LanguageExt.CodeGen work on Unity? HOT 2
- Seq<EitherAsync<A, B>>.SequenceParallel does not preserve order HOT 14
- FileIO bug for Test when opening new file for writing
- Directory IO bug for Test when enumerating files
- Simple Kickstart Project available? CodeGen tool is outdated? dotnet8 HOT 1
- Implicit conversion of int? to Option<int> HOT 2
- SequenceParallel performance degradation in 4.4.8 HOT 1
- Refit.ApiException: 'An error occured deserializing the response.'
- Constructor not found for LanguageExt.LongRange HOT 1
- Alternative to inherit from Fin and other monads
- EitherAsync is missing HOT 4
- Add `DoAsync` to Either types (and maybe others) HOT 2
- Release build fails for a big application HOT 5
- v5 resource tracking issue? HOT 6
- Latest v5 alpha IEnumerable<T> ToSeq() extension is missing HOT 4
- v5 await more than a single forked IO, similar to Task.WhenAll HOT 2
- Higher-Kinded Types in Algebraic Free Monads with language-ext?
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 language-ext.