Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a pure function change input arguments?

Is a function that changes the values of an input argument still a pure function?

My example (Kotlin):

data class Klicker(
    var id: Long = 0,
    var value: Int = 0
)

fun Klicker.increment() = this.value++

fun Klicker.decrement() = this.value--

fun Klicker.reset() {
    this.value = 0
}

Wikipedia says a pure function has these two requirements:

  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.

From my understanding, all functions from my example comply with the first requirement.

My uncertainty starts with the second requirement. With the change of the input argument, I mutate an object (rule violation), but this object is not outside of the function scope, so maybe no rule violation?

Also, does a pure function always need to return a completely new value?

I presume, this function is considert 100% pure:

fun pureIncrement(klicker: Klicker): Klicker {
    return klicker.copy(value = klicker.value++)
}

Be gentle, this is my first Stackoverflow question.

like image 763
addyi Avatar asked Mar 02 '18 12:03

addyi


People also ask

What does a pure function do?

A pure function is a function which: Given the same input, will always return the same output. Produces no side effects.

Do pure functions have side effects?

A pure function does not have side-effects and its result does not depend on anything other than its inputs. A pure function guarantees that for a given input it will produce the same output no matter how many times it is called.

Is a pure function immutable?

Pure functions don't modify their input. They treat the input values as immutable. An immutable value is a value that, once created, cannot be changed.

Does impure function change the state of an object?

Any function that changes the internal state of one of its arguments or the value of some external variable is an impure function . They may have any side effects like network or database calls and it may modify the arguments which are passed to them.


1 Answers

The increment and decrement functions fulfill neither of the requirements for a pure function. Their return value depends on the state of the Klicker class, which may change while program execution proceeds, so the first requirement is not fulfilled. The evaluation of the result mutates the mutable Klicker instance, so the second requirement is also not fulfilled. It doesn't matter in which scope the mutable data is; a pure function must not mutate any data at all.

The reset function violates only the second requirement.

The pureIncrement function can be made pure if you change it to:

fun pureIncrement(klicker: Klicker): Klicker {
    return klicker.copy(value = klicker.value + 1)
}
like image 69
yole Avatar answered Sep 23 '22 04:09

yole