简体   繁体   English

FP 中引用透明的好处

[英]The benefit of referential transparency in FP

I was learning functional programming and came across the term referential transparency.我在学习函数式编程时遇到了引用透明这个术语。

After some research on that, I found out that RT is useful经过一些研究,我发现 RT 很有用

  1. When we want to make our code easier to reason about and read since our function is predictable AND当我们想让我们的代码更容易推理和阅读时,因为我们的函数是可预测的 AND

  2. When our function is predictable, it will be of great help to the JIT compiler allowing it to replace the function with its return value(Does it replace the function with its value as long as the function is hot?).当我们的函数是可预测的时,它将对 JIT 编译器有很大帮助,允许它用它的返回值替换函数(只要函数很热,它就用它的值替换函数吗?)。

Are both the above statements true?以上两种说法都是真的吗?

Referential Transparency means that a function with certain parameters will always return the same result as long as the input parameters are the same, in other words it does not have side effects.引用透明意味着只要输入参数相同,具有某些参数的函数将始终返回相同的结果,换句话说,它没有副作用。 Of course one of the benefits of these is that the code is easier to reason about because the same execution will return the same values so you can change the call to the function by the result it returns.当然,这些的好处之一是代码更容易推理,因为相同的执行将返回相同的值,因此您可以根据返回的结果更改对函数的调用。

I suppose this feature is used by many compilers to speed up the execution by making the change, but this depends on the language and the compiler used to translate to byte code, but It little has to do with Functional programming per se.我想这个特性被许多编译器用来通过进行更改来加速执行,但这取决于用于转换为字节码的语言和编译器,但它与函数式编程本身几乎没有关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM