What is Algebraic Effects?

南小北
2 min readAug 15, 2019

--

Below is a purely personal understanding, if wrong, welcome to scold me.

1. What is algebraic expression?

Recalling the advanced elementary school mathematics, it should be like this:

2x + 3y + 4z

We can name this algebra: getNum.

2. Change to a function

function x() {}
function y() {}
function z() {}
function getNum() {
let a = 2 * x()
let b = 3 * y()
let c = 4 * z()
return a + b + c
}

The three functions x, y and z in the code above are three variables in the “algebraic expression” getNum.

3. What’s the problem?

getNum depends on three functions: x, y and z.

Classified by functions: what to do → getNum(), how to do → x() y() z().

“What to do” and “how to do” are coupled.

4. How to decouple?

function getNum() {
let a = 2 * perform `x`
let b = 3 * perform `y`
let c = 4 * perform `z`
return a + b + c
}

Well, first define “what to do” to ensure that the mainstream process is smooth.

As for “how to do”, it can be written anywhere, such as in another file:

try {
getNum();
} handle (effect) {
if (effect === 'x') {
// ...
} else if (effect === 'y') {
// ...
} else if (effect === 'z') {
// ...
}
}

WARNING: The above is pseudocode, and the above syntax is not implemented in JS at present.

5. What is algebraic effects?

“Effects” are the three brothers of x y z. These three functions have some effects.

“Algebraic”, notice, is an adjective.

What is “Algebraic Effects”?

It is the effect that is inserted into a function like a variable in an algebraic expression.

So the full name should be “the effects like variables in algebraic expressions”.

That’s what I understand. That’s it.

If you think what I’m talking about is a piece of shit,

Take a quick look at this: overreacted.io/algebraic-e…

--

--

No responses yet