纯函数、透明引用、副作用的含义

简介:

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.

• Reading input.

• 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.

目录
相关文章
|
6月前
|
JavaScript 前端开发
避免将变量和函数暴露给全局作用域可能导致的命名冲突和代码可维护性
保护变量和函数不暴露于全局作用域可防止命名冲突,提升代码可维护性。
|
1月前
|
JavaScript 前端开发
箭头函数可以使用哪些方式定义
箭头函数是ES6引入的一种新的函数定义方式,使用`=>`符号。它可以简化函数的书写,主要有两种定义方式:1. 简单表达式,如`const fn = () => {}`;2. 带参数表达式,如`const fn = (arg) => {}`。
|
5月前
|
自然语言处理 JavaScript 前端开发
JavaScript闭包是函数访问外部作用域变量的能力体现,它用于封装私有变量、持久化状态、避免全局污染和处理异步操作。
【6月更文挑战第25天】JavaScript闭包是函数访问外部作用域变量的能力体现,它用于封装私有变量、持久化状态、避免全局污染和处理异步操作。闭包基于作用域链和垃圾回收机制,允许函数记住其定义时的环境。例如,`createCounter`函数返回的内部函数能访问并更新`count`,每次调用`counter()`计数器递增,展示了闭包维持状态的特性。
53 5
|
4月前
|
JavaScript
S外部函数可以访问函数内部的变量的闭包-闭包最简单的用不了,闭包是内层函数+外层函数的变量,简称为函数套函数,外部函数可以访问函数内部的变量,存在函数套函数
S外部函数可以访问函数内部的变量的闭包-闭包最简单的用不了,闭包是内层函数+外层函数的变量,简称为函数套函数,外部函数可以访问函数内部的变量,存在函数套函数
【C++11特性篇】右值引用变量的属性会被编译器识别成左值【详解&证明&代码演示】
【C++11特性篇】右值引用变量的属性会被编译器识别成左值【详解&证明&代码演示】
|
6月前
this的含义,什么情况下使用this,改变this指针的两种办法。 === 由于this关键字很混乱,如何解决这个问题
this的含义,什么情况下使用this,改变this指针的两种办法。 === 由于this关键字很混乱,如何解决这个问题
43 0
|
11月前
定义和声明的区别 以及 内部和外部函数
把建立存储空间的变量声明称定义; 不需要建立存储空间的声明称为声明; c为例, 在函数中出现的对变量的声明(除了用extern声明的以外)都是定义 在函数中对其他函数的声明不是函数的定义 详细见c程序设计-谭浩强 196-197页
62 0
|
JSON C语言 数据格式
【C/C++】防止不必要的局部宏替换
如何避免和防止宏定义在不必要的位置进行替换
230 0
|
JavaScript 前端开发
JavaScirpt基础 之 变量 之 声明变量
声明(创建) JavaScript 变量
76 0
JavaScirpt基础 之 变量 之 声明变量
|
安全 Unix vr&ar
一文刨析C/C++全局常量的定义
一文刨析C/C++全局常量的定义