DEV Community

Discussion on: Algebraic Effects in JavaScript part 4 - Implementing Algebraic Effects and Handlers

Collapse
 
iquardt profile image
Iven Marquardt • Edited

I'm not going to discuss the motivation behind throwing effects vs executing them right away inside a function. The question just translates to why we should separate pure and impure computations, which is a whole subject ont its own.

I think in Javascript it should be the goal to be explicit about which portion of the code is impure and to separate it from the "pure portion" on the temporal rather then the spacial axis (separate the code).

We can achieve this temporal separation by deferring the evaluation of impure code through thunks. However, we don't want thunks flying around throughout our code, so we wrap them in an object type and define some operators to be able to lift normal functions into this type and to chain operations on this type.

Now we can compose eventually-impure with pure code and unleash the effects later explicitly by calling e.g.

computation.runEffects();
Collapse
 
voronar profile image
Kirill Alexander Khalitov

We can achieve this temporal separation by deferring the evaluation of impure code through thunks. However, we don't want thunks flying around throughout our code, so we wrap them in an object type and define some operators to be able to lift normal functions into this type and to chain operations on this type.

Sounds like the main idea behind redux-saga library.