2015/08/07 10:58

# Side Effects(副作用）

According to Wikipedia, a function is said to have a side effect “if, in addition to returning a value, it also modifies some state or has an observable interaction with calling functions or the outside world.”

Side effects include things like:

• Writing or printing output.

• Mutating the state of a variable that was given as input, changing data in a data structure, or modifying the value of a field in an object.

• Throwing an exception, or stopping the application when an error occurs.

• Calling other functions that have side effects.

# referential transparency（透明引用）

An expression is referentially transparent (RT) if it can be replaced by its resulting value without changing the behavior of the program. This must be true regardless of where the expression is used in the program.

For instance, assume that x and y are immutable variables within some scope of an

application, and within that scope they’re used to form this expression:

x + y

You can assign this expression to a third variable, like this:

val z = x + y

Now, throughout the given scope of your program, anywhere the expression x + y is used, it can be replaced by z without affecting the result of the program

# Pure functions（纯函数）

## 纯函数定义

Wikipedia defines a pure function as follows:

1. The function always evaluates to the same result value given the same argument value(s). It cannot depend on any hidden state or value, and it cannot depend on any I/O.

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.

“A function f is pure if the expression f(x)  is referentially transparent for all referentially transparent values x.”

To summarize, a pure function is referentially transparent and has no side effects

“A telltale sign of a function with side effects is that its result type is Unit.”

From these definitions, we can make these statements about pure functions:

• A pure function is given one or more input parameters.

• Its result is based solely off of those parameters and its algorithm. The algorithm will not be based on any hidden state in the class or object it's contained in.

• It won't mutate the parameters it’s given.

• It won't mutate the state of its class or object.

• It doesn't perform any I/O operations, such as reading from disk, writing to disk, prompting for input, or reading input.

## 纯函数的例子

These are some examples of pure functions:

• Mathematical functions, such as addition, subtraction, multiplication.

• Methods like split and length on the String class.

• The to* methods on the String class (toInt, toDouble, etc.)

• Methods on immutable collections, including map, drop, take, filter, etc.

• The functions that extract values from an HTML string in Recipe 20.3.

## 不是纯函数的例子

The following functions are not pure functions:

• Methods like getDayOfWeek, getHour, or getMinute. They return a different valuedepending on when they are called.

• A getRandomNumber function.

• A function that reads user input or prints output.

• A function that writes to an external data store, or reads from a data store.

0
1 收藏

0 评论
1 收藏
0