Associativity is a desirable property and quite common for many operations in FP. Now I was wondering if an impure function can interfere with it. The only example I found isn't really convincing, because I'm not sure if nullary functions count as proper functions (riegorously speaking) and additionally, the example looks rather contrived.
The following is written in JS but is hopefully self-explanatory:
// function composition
const comp = f => g => x => f(g(x));
const foo = () => 1;
const bar = () => Math.round(Math.random() * 100);
// Set functor (ignore the hideous implementation)
const map = f => s => {
const r = new Set();
s.forEach(x => r.add(f(x)));
return r;
};
const lhs = map(comp(bar) (foo));
const rhs = comp(map(bar)) (map(foo));
const set1 = lhs(new Set([1, 2, 3]));
const set2 = rhs(new Set([1, 2, 3]));
console.log(Array.from(set1)); // yields an array filled with up to three random integers
console.log(Array.from(set2)); // yields an array filled with a single random integer
I'm not sure whether this example can be considered as evidence. Are there more convincing examples?