Java 纯函数和不纯函数的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22395311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:30:25  来源:igfitidea点击:

Difference between pure and impure function?

javaaccessor

提问by Amith KK

I assumed that pure functions must always have a return type (i.e., must not be void) and must have the same output regardless of the state of the object and that Impure functions change the state of the object or print the state of the object.

我假设纯函数必须始终具有返回类型(即,不得为void)并且无论对象的状态如何都必须具有相同的输出,并且不纯函数会更改对象的状态或打印对象的状态。

But the textbook I use states that:

但我使用的教科书指出:

An accessor usually contains a return statement, but a method that prints information about an objects state may also be classified as an accessor.

访问器通常包含一个 return 语句,但打印有关对象状态的信息的方法也可以归类为访问器。

I'm confused. Which one is correct?

我糊涂了。哪一个是正确的?

EDIT

编辑

A bit of clarification,The thing that makes me ask is this question:

有点澄清,让我问的是这个问题:

The last question is to "Give the type of function used", and the people who commented there stated that it is an impure function as it is printing.

最后一个问题是“给出使用的函数类型”,在那里评论的人说这是一个不纯的函数,因为它正在打印。

So is this function pure or impure?

那么这个函数是纯的还是不纯的?

采纳答案by A.T.

Content taken from this link

内容来自此链接

Characteristics of Pure Function:

纯函数的特点:

  1. The return value of the pure func-tions solely depends on its arguments Hence, if you call the pure func-tions with the same set of argu-ments, you will always get the same return values.

  2. They do not have any side effects like net-work or data-base calls

  3. They do not mod-ify the argu-ments which are passed to them
  1. 纯函数的返回值仅取决于其参数。因此,如果您使用相同的参数集调用纯函数,您将始终获得相同的返回值。

  2. 它们没有任何副作用,如网络或数据库调用

  3. 他们不修改传递给他们的参数

Char-ac-ter-isitcs of Impure functions

不纯函数的特征

  1. The return value of the impure func-tions does not solely depend on its arguments Hence, if you call the impure func-tions with the same set of argu-ments, you might get the dif-fer-ent return values For exam-ple, Math.random(), Date.now()

  2. They may have any side effects like net-work or data-base calls

  3. They may mod-ify the argu-ments which are passed to them

  1. 不纯函数的返回值不仅仅取决于它的参数 因此,如果你用相同的参数集调用不纯函数,你可能会得到不同的返回值 例如, Math.random(), Date.now()

  2. 它们可能有任何副作用,例如网络或数据库调用

  3. 他们可以修改传递给他们的参数

function impureFunc(value){
  return Math.random() * value;
}

function pureFunc(value){
  return value * value;
}

var impureOutput = [];
for(var i = 0; i < 5; i++){
   impureOutput.push(impureFunc(5));
}

var pureOutput = [];
for(var i = 0; i < 5; i++){
   pureOutput.push(pureFunc(5));
}

console.log("Impure result: " + impureOutput); // result is inconsistent however input is same. 

console.log("Pure result: " + pureOutput); // result is consistent with same input

回答by Jeffrey Hantin

Mu.You seem to be assuming that an accessor is a pure function by definition. This is not necessarily the case -- an accessor (even a get-accessor returning a value) may be impure, such as the getmethod of LinkedHashMapwhen in access-order mode (which moves the requested entry to last position in iteration order).

亩。您似乎假设访问器根据定义是纯函数。这不是必需的情况-一个访问器(即使是get-accessor返回值)可以是不纯的,如get的方法LinkedHashMap在访问阶模式(其在移动迭代顺序请求的条目到最后的位置)时。

回答by Elliott Frisch

From Wikipedia- a function may be described as a pure functionif both these 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 as 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 observableside effect or output, such as mutation of mutable objects or output to I/O devices.
  1. 给定相同的参数值,该函数始终计算相同的结果值。函数结果值不能依赖于随着程序执行的进行或程序的不同执行之间可能发生变化的任何隐藏信息或状态,也不能依赖于来自 I/O 设备的任何外部输入。
  2. 结果的评估不会导致任何语义上可观察的副作用或输出,例如可变对象的突变或输出到 I/O 设备。

Therefore, if either statement is false when compared to your code then it is impure.

因此,如果与您的代码相比,任何一个语句都是错误的,那么它就是不纯的。

回答by CoderCroc

Both Statements are Correct.

两种说法都是正确的。

When you create methods for getting value which are called ACCESSOR METHODS

当您创建称为ACCESSOR METHODS 的获取价值的方法时

Ex:

前任:

public String getName(){
    return this.name;
}

and for Setting value we use methods with VOIDwhich are called MUTATOR METHODS

对于设置值,我们使用带有VOID 的方法,称为MUTATOR METHODS

Ex:

前任:

public void setName(String n){
    this.name=n;
}

回答by Amiya Upadhyay

Impure Functions or Mutator Methods change the state of object and modify the values that are stored in Instance Variables.

不纯函数或 Mutator 方法更改对象的状态并修改存储在实例变量中的值。