Code Monkey home page Code Monkey logo

Comments (13)

louthy avatar louthy commented on June 11, 2024

What are you seeing output?

from language-ext.

olegdunkan avatar olegdunkan commented on June 11, 2024

After each second I see *, but I can't break it

from language-ext.

louthy avatar louthy commented on June 11, 2024

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.

olegdunkan avatar olegdunkan commented on June 11, 2024

Yes, I expected the same behaviour as you have demonstrated to me, but console don't want to accept any keys

from language-ext.

louthy avatar louthy commented on June 11, 2024

Sorry, just noticed this isn't exactly the same as the one in the Effects project. I'll try yours.

from language-ext.

olegdunkan avatar olegdunkan commented on June 11, 2024

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.

louthy avatar louthy commented on June 11, 2024

I don't think that version of repeat is paying attention to the cancellation-token, leave it with me.

from language-ext.

olegdunkan avatar olegdunkan commented on June 11, 2024

Ok, thank you!

from language-ext.

louthy avatar louthy commented on June 11, 2024

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.

olegdunkan avatar olegdunkan commented on June 11, 2024

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.

louthy avatar louthy commented on June 11, 2024

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.

olegdunkan avatar olegdunkan commented on June 11, 2024

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 and Aff 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.

harish-venkataramanan-cko avatar harish-venkataramanan-cko commented on June 11, 2024

Interesting problem, do you think this behavior could somehow be restricted statically rather than noticing during runtime?

from language-ext.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.