gpt4 book ai didi

functional-programming - 纯函数可以在同一个类中使用私有(private)常量变量吗?

转载 作者:行者123 更新时间:2023-12-01 13:41:47 25 4
gpt4 key购买 nike

纯函数可以在同一个类中使用私有(private)常量吗?

例如:

class TimesThousand {
const CONSTANT = 1000;

function calculate(number) {
return number * CONSTANT;
}
}

可以将 calculate() 视为一个纯函数吗?

最佳答案

纯函数是纯函数,当返回值仅由其输入值决定时,没有任何可观察到的副作用。

所以你的函数是纯粹的。由于 CONSTANT 的值(顾名思义)是常量,因此输出完全由输入决定。

来自 Wikipedia :

a function may be considered a pure function if both of the following statements about the function hold:

  1. The function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change while program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices.
  2. Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices.

关于functional-programming - 纯函数可以在同一个类中使用私有(private)常量变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39497947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com