Bartosz Milewski, in his book "Category Theory for Programmers" says that:
Composition is associative. If you have three morphisms, f, g, and h, that can be composed (that is, their objects match end-to-end), you don’t need parentheses to compose them. In math notation this is expressed as:
h∘(g∘f) = (h∘g)∘f = h∘g∘f
While trying to understand how to write composable code I want to be able to recognize when my code is NOT composable. Should be simple, right - my functions should comply with the equation h∘(g∘f) = (h∘g)∘f
But when I'm trying to translate it into code I have problem finding functions that are not associative.
Even functions that mutate state seems to have this property.
Example:
Let say I have following functions:
static int a;
void ZeroInc()
{
a = 0;
a++;
}
void Inc()
{
a++;
}
void Zero()
{
a = 0;
}
void IncInc()
{
a++;
a++;
}
I've intentionally written here functions that mutate global state to show that that global state can also be treated as (implicit) function input.
It doesn't matter if I'll call them like this:
ZeroInc(); // a = 1
Inc(); // a = 2
or like this
Zero(); //a = 0
IncInc(); //a = 2
In both cases as a result I have a = 2;
Which means that I can also write just
a = 0;
a++;
a++;
and have the same result.
Can someone show me example of programming function/code that is not associative (and therefore not composable)?
Please show me how it doesn't obey h∘(g∘f) = (h∘g)∘f equation.