杂质会影响操作的关联性吗?

问题描述

关联性是FP中许多操作的理想属性。现在我想知道不纯函数是否会干扰它。我发现的唯一示例并没有真正令人信服,因为我不确定是否将空函数视为适当的函数(严格地说),此外,该示例看起来也很虚构。

以下是用JS编写的,但希望可以不言自明:

// 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,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

我不确定此示例是否可以视为证据。还有更多令人信服的例子吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)