如何解决杂质会影响操作的关联性吗?
关联性是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 举报,一经查实,本站将立刻删除。