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

简介:

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.

目录
相关文章
|
8月前
|
JavaScript 前端开发
避免将变量和函数暴露给全局作用域可能导致的命名冲突和代码可维护性
保护变量和函数不暴露于全局作用域可防止命名冲突,提升代码可维护性。
|
8月前
|
自然语言处理
如何在箭头函数中访问非封闭作用域中的变量?
【2月更文挑战第20天】【2月更文挑战第63篇】如何在箭头函数中访问非封闭作用域中的变量?
56 1
|
3月前
|
JavaScript 前端开发
箭头函数可以使用哪些方式定义
箭头函数是ES6引入的一种新的函数定义方式,使用`=>`符号。它可以简化函数的书写,主要有两种定义方式:1. 简单表达式,如`const fn = () => {}`;2. 带参数表达式,如`const fn = (arg) => {}`。
|
6月前
|
语音技术
语音识别----函数基础定义联系案例,函数的参数,函数的参数练习案例,函数的返回值定义语法,函数返回值之None,函数的说明文档,函数的嵌套调用,变量在函数中的作用域,内部变量变全局变量用global
语音识别----函数基础定义联系案例,函数的参数,函数的参数练习案例,函数的返回值定义语法,函数返回值之None,函数的说明文档,函数的嵌套调用,变量在函数中的作用域,内部变量变全局变量用global
|
7月前
|
自然语言处理 JavaScript 前端开发
JavaScript闭包是函数访问外部作用域变量的能力体现,它用于封装私有变量、持久化状态、避免全局污染和处理异步操作。
【6月更文挑战第25天】JavaScript闭包是函数访问外部作用域变量的能力体现,它用于封装私有变量、持久化状态、避免全局污染和处理异步操作。闭包基于作用域链和垃圾回收机制,允许函数记住其定义时的环境。例如,`createCounter`函数返回的内部函数能访问并更新`count`,每次调用`counter()`计数器递增,展示了闭包维持状态的特性。
58 5
|
7月前
|
JavaScript 前端开发
null、未定义或未声明的变量之间有什么区别
null、未定义或未声明的变量之间有什么区别
|
6月前
|
JavaScript
S外部函数可以访问函数内部的变量的闭包-闭包最简单的用不了,闭包是内层函数+外层函数的变量,简称为函数套函数,外部函数可以访问函数内部的变量,存在函数套函数
S外部函数可以访问函数内部的变量的闭包-闭包最简单的用不了,闭包是内层函数+外层函数的变量,简称为函数套函数,外部函数可以访问函数内部的变量,存在函数套函数
|
8月前
|
JavaScript 前端开发
JavaScript闭包允许内部函数访问并保留外部函数的变量,即使外部函数执行结束
【5月更文挑战第13天】JavaScript闭包允许内部函数访问并保留外部函数的变量,即使外部函数执行结束。在游戏开发中,闭包常用于创建独立状态的角色实例。例如,`createCharacter`函数生成角色,内部函数(如`getHealth`、`setHealth`)形成闭包,保存角色的属性(如生命值)。这样,每个角色实例都有自己的变量副本,不互相影响,从而实现角色系统的独立性。
60 0
|
8月前
|
算法 编译器 C++
【C++ 概念区分】C++ 中覆盖,重写,隐藏 三者的区别
【C++ 概念区分】C++ 中覆盖,重写,隐藏 三者的区别
213 0
|
8月前
this的含义,什么情况下使用this,改变this指针的两种办法。 === 由于this关键字很混乱,如何解决这个问题
this的含义,什么情况下使用this,改变this指针的两种办法。 === 由于this关键字很混乱,如何解决这个问题
51 0