Difference between pure and impure function?

后端 未结 5 1250
再見小時候
再見小時候 2020-12-03 01:34

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 th

相关标签:
5条回答
  • 2020-12-03 02:05

    From Wikipedia - a function may be described as a pure function if 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 observable side effect or output, such as mutation of mutable objects or output to I/O devices.

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

    0 讨论(0)
  • 2020-12-03 02:07

    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

    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

    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

    0 讨论(0)
  • 2020-12-03 02:16

    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 get method of LinkedHashMap when in access-order mode (which moves the requested entry to last position in iteration order).

    0 讨论(0)
  • 2020-12-03 02:27

    Both Statements are Correct.

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

    Ex:

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

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

    Ex:

    public void setName(String n){
        this.name=n;
    }
    
    0 讨论(0)
  • 2020-12-03 02:29

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

    0 讨论(0)
提交回复
热议问题