知识卡片
引用透明与记忆化
引用透明意味着同一输入下可以用求得的值替换一个表达式,而不改变程序的可观察结果。纯函数的确定性与无副作用为这种替换提供条件。因此,记忆化可以在首次得到结果后用参数作键缓存,遇到相同参数时直接取值。若缓存的函数还读取可变化的数据库,参数相等便不再足以判断缓存仍有效。
相关卡片 [[纯函数与外部副作用]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
- · 纯函数与副作用
原书出处 · 1 处
- From panic to result: Error handling 纯函数与副作用 · 查看原书依据
Pure functions have nice, mathematical properties like referential transparency, which means you can replace a pure function call with its result. This opens the door for optimizations like memoization, a caching of function results, which is easy when the answer is always the same as long as the parameters do not change.
参考来源
- 位置:《Write Powerful Rust Macros》第07章《From panic to result: Error handling》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-7.xhtml#p26)
- 结论依据:7.2 由纯函数的可替换性推导可按输入缓存返回值。
- 原始内容:Pure functions have nice, mathematical properties like referential transparency, which means you can replace a pure function call with its result. This opens the door for optimizations like memoization, a caching of function results, which is easy when the answer is always the same as long as the parameters do not change.