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

简介:

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.

目录
相关文章
|
移动开发 算法 JavaScript
平滑切换——PPT动画的全新玩法(原理篇)/04/O365智能系列(一)
平滑切换——PPT动画的全新玩法(原理篇)/04/O365智能系列(一)
4351 1
平滑切换——PPT动画的全新玩法(原理篇)/04/O365智能系列(一)
|
5月前
|
人工智能 自然语言处理 测试技术
内部流出:快手质量中台用大模型做“智能冒烟”,提测就打回,研发再也不敢敷衍
快手质量中台将冒烟测试升级为AI智能门禁:基于大模型自动生成/进化用例、多模态视觉判定结果,并与CI深度集成,实现提测自动拦截。半年内提测通过率从43%跃升至91%,人力投入归零,打回次数下降83%,真正把质量门槛“焊死”在代码合入前。
|
8月前
|
弹性计算 人工智能 网络安全
阿里云ECS部署OpenClaw攻略及iMessage深度集成+千问Qwen3.6-Plus配置+新手避坑指南
2026年,开源AI智能体框架OpenClaw(曾用名Clawdbot、Moltbot,被用户称为“小龙虾”)已成为个人与企业搭建专属AI助手的核心选择。它支持自然语言指令理解、主动任务规划、多工具调用与多渠道消息接入,能实现7×24小时自动化办公、客户服务与内容处理。
441 6
|
测试技术 数据安全/隐私保护 Windows
|
SQL Oracle 关系型数据库
|
存储 canal 缓存
【高并发项目实战】千万级并发的购物车系统设计与代码详解
本文主要介绍redis在千万级系统中设计架构方案,如何在设计上抛弃mysql,主架构设计、缓存一致性方案、大value处理方案和redis限流和故障恢复降级方案设计与代码详解
|
运维 前端开发 rax
迈向应用研发新时代,前后端一体化研发模式即刻体验
随着 Serverless 基础服务带来的技术红利,前端工程师能够以更低成本和更高可靠性掌控后端的服务。在应用开发过程中不仅仅要考量运维的成本,如何让前端开发架构同后端应用架构结合,来实现应用研发提效,同样是开发者关注的核心,本文将从前后端一体化研发模式的开发实践上同大家一起讨论探索。
2422 0
迈向应用研发新时代,前后端一体化研发模式即刻体验
|
程序员 数据安全/隐私保护
那串「73 76 79 86 85」究竟是什么意思?我一直等你能够解答
这篇文章打动我的不是程序猿的那串代码,而却是妹子的努力。如果妹子不喜欢你,那么这段代码就是明天问问你是什么意思就好了,也不会去熬夜查,而妹子却没有,她自己弄清楚了这段代码的意思,说明:程序猿哇,她在乎你!希望阅读了这篇文章的童鞋们都能找到在乎你的那个人。
16814 0