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

简介:

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.

目录
相关文章
|
监控 Linux 应用服务中间件
centos7 部署zabbix5 踩坑笔记
centos7 部署zabbix5 踩坑笔记
512 0
|
弹性计算 Ubuntu 安全
基于Ubuntu20.4搭建WordPress个人博客
在Ubuntu20.4系统上成功搭建了WordPress个人博客并且对WordPress进行了简单的配置。
1513 2
基于Ubuntu20.4搭建WordPress个人博客
|
12月前
|
人工智能 自然语言处理
Promptriever:信息检索模型,支持自然语言提示响应用户搜索需求
Promptriever 是一种新型信息检索模型,由约翰斯·霍普金斯大学和 Samaya AI 联合推出。该模型能够接受自然语言提示,并以直观的方式响应用户的搜索需求。通过在 MS MARCO 数据集上的训练,Promptriever 在标准检索任务上表现出色,能够更有效地遵循详细指令,提高查询的鲁棒性和检索性能。
254 6
Promptriever:信息检索模型,支持自然语言提示响应用户搜索需求
|
机器学习/深度学习 人工智能 分布式计算
【AI系统】分布式通信与 NVLink
进入大模型时代后,AI的核心转向大模型发展,训练这类模型需克服大量GPU资源及长时间的需求。面对单个GPU内存限制,跨多个GPU的分布式训练成为必要,这涉及到分布式通信和NVLink技术的应用。分布式通信允许多个节点协作完成任务,而NVLink则是一种高速、低延迟的通信技术,用于连接GPU或GPU与其它设备,以实现高性能计算。随着大模型的参数、数据规模扩大及算力需求增长,分布式并行策略,如数据并行和模型并行,变得至关重要。这些策略通过将模型或数据分割在多个GPU上处理,提高了训练效率。此外,NVLink和NVSwitch技术的持续演进,为GPU间的高效通信提供了更强的支持,推动了大模型训练的快
400 0
|
前端开发 Python
60行Python代码开发在线markdown编辑器
60行Python代码开发在线markdown编辑器
254 2
|
分布式计算 大数据 API
|
SQL Cloud Native 架构师
深入浅出Presto:大数据查询引擎的原理与应用
【4月更文挑战第7天】Presto是高性能的分布式SQL查询引擎,专为大规模数据交互式分析设计。它采用分离式架构,内存计算和动态规划优化查询,支持跨源查询、交互式查询和ANSI SQL兼容性。应用于大数据分析、实时数据湖查询和云原生部署。Presto的灵活性和效率使其在大数据处理领域备受推崇,适合分析师、数据科学家和IT架构师使用。未来将在博客中分享更多实践和案例。
1534 1
|
弹性计算 缓存 运维
2024阿里云服务器4核8G配置最新租用收费标准与活动价格参考
4核8G配置是大部分企业级用户购买阿里云服务器的首选配置,2024年经过调价之后,4核8G配置的阿里云服务器按量收费标准最低为0.45元/小时,按月租用平均优惠月价最低收费标准为216.0元/1个月,按年购买最低活动价格为1322.78元/1年,云服务器实例规格和配置不同,收费标准与活动价格也不同,下面是小编为大家整理汇总的阿里云服务器4核8G配置2024年最新收费标准与活动价格。
2024阿里云服务器4核8G配置最新租用收费标准与活动价格参考
|
Linux 开发工具 开发者
智谱ChatGLM3本地私有化部署(Linux)
智谱ChatGLM3本地私有化部署(Linux)
524 0
|
Java Spring NoSQL
Spring Boot 环境变量读取 和 属性对象的绑定
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/catoop/article/details/50548009 凡是被Spring管理的类,实现接口 EnvironmentAware 重写方法 setEnvironment 可以在工程启动时,获取到系统环境变量和application配置文件中的变量。
2633 0