4 ms·
The value of monads is that they fold side-effects into the return values. dirty languages: input -> function_a -> output/input -> function_b -> output
by Double_Cast 7y ago
The value of monads is that they fold side-effects into the return values.
dirty languages:
input -> function_a -> output/input -> function_b -> output
^ ^
| |
side_effect_a side_effect_b
| |
v v
lovecraftian_primordial_soup_of_global_state
pure languages:
input -> function_a -> output/input -> function_b -> output
^ ^
| |
side_effect_a side_effect_b ------>
|
+-------------------------------------------->
If C++ were pure, the type-signatures would look like
(output, side_effect_a) function_a(input);
(output, side_effect_b) function_b(input);
The drawback is that the type-signature of function_b(function_a()) becomes complex. Now, function_b needs to accept and pass-on the upstream side-effects. To compose function_a and function_b, we need to convert the type-signature of function_b to
(output, side_effect_b, side_effect_a) function_b(input, side_effect_a);
Fortunately, ">>=" converts function_b under the hood. Which allows us to write
function_a() >>= function_b() >>= function_c >>= function_d
and pretend that each ">>=" is just a ";" without wrestling with compound inputs and compound returns.