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

简介:

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.

目录
相关文章
|
11月前
|
存储 缓存 Linux
docker的底层原理六: 联合文件系统(UnionFS)
本文介绍了Docker使用的联合文件系统(UnionFS),它通过分层存储和写时复制(CoW)机制,实现了容器的轻量级、高性能存储,支持了镜像继承、数据持久化和隔离性。
455 0
|
9月前
|
人工智能 自然语言处理
Promptriever:信息检索模型,支持自然语言提示响应用户搜索需求
Promptriever 是一种新型信息检索模型,由约翰斯·霍普金斯大学和 Samaya AI 联合推出。该模型能够接受自然语言提示,并以直观的方式响应用户的搜索需求。通过在 MS MARCO 数据集上的训练,Promptriever 在标准检索任务上表现出色,能够更有效地遵循详细指令,提高查询的鲁棒性和检索性能。
175 6
Promptriever:信息检索模型,支持自然语言提示响应用户搜索需求
|
存储 分布式计算 资源调度
|
前端开发 Python
60行Python代码开发在线markdown编辑器
60行Python代码开发在线markdown编辑器
190 2
|
SQL Cloud Native 架构师
深入浅出Presto:大数据查询引擎的原理与应用
【4月更文挑战第7天】Presto是高性能的分布式SQL查询引擎,专为大规模数据交互式分析设计。它采用分离式架构,内存计算和动态规划优化查询,支持跨源查询、交互式查询和ANSI SQL兼容性。应用于大数据分析、实时数据湖查询和云原生部署。Presto的灵活性和效率使其在大数据处理领域备受推崇,适合分析师、数据科学家和IT架构师使用。未来将在博客中分享更多实践和案例。
1432 1
|
存储 数据处理 调度
Python实现定时任务的方案及其比较
Python实现定时任务的方案及其比较
317 0
|
存储 关系型数据库 MySQL
【MySQL系列】-回表、覆盖索引真的懂吗
【MySQL系列】-回表、覆盖索引真的懂吗
1613 1
抓包工具 -- Fiddler(拦截包)
抓包工具 -- Fiddler(拦截包)
339 0
|
关系型数据库 MySQL 数据库
【MySQL】MySQL事务保姆级教程(适合MySQL初学者学习)
【MySQL】MySQL事务保姆级教程(适合MySQL初学者学习)
865 0
|
缓存 负载均衡 架构师
阿里资深架构师钟华曰:中台战略思想与架构实战;含内部实施手册
最近在读一本书,叫做《企业IT架构转型之道:阿里巴巴中台战略思想与架构实战》,在写此文时本书还没有看完,因为担心如果把书全部看完后再来写这篇文章,很多精彩的内容可能已经忘记了,所以中途先写一篇来分享给大家。