SlideShare ist ein Scribd-Unternehmen logo
1 von 16
函数式编程二三事
编程范式
编程范式:计算机编程的基本风格,指导着如何构建程
序的结构和元素,如何编写程序的方法论
编程范式提供并决定了程序员对程序执行的看法
• 函数式编程
• 指令式编程
• 过程式编程
• 面向对象编程
函数式
函数式编程:将电脑运算视为函数运算,并且避免使用程序状态以及易变对象,
λ 演算为其最重要的基础。
命令式编程关心解决问题的步骤,函数式编程关心数据的映射
λ 演算
二十世纪三十年代,在研究可计算问题时,对计算本
身进行形式化,提出了一些计算模型。
著名的包括图灵提出的图灵机模型,丘奇和克莱尼提
出的 λ 演算等
λ 演算是一种形式系统,实质上是为一个函数,返回值也是函数超级机器设计的编程语言。
在这种语言里面,函数的参数是。
三大变换
λ 抽象:
α-变换:可以将 λ 表达式中的一个变量,
重新命名为另一个变量
β-变换:将构建好的函数应用到参数值上,
代入参数的方向叫做 β-规约,
反向应用称为 β-抽象
η-变换:去除多余的 λ 抽象
丘奇数
一等公民的函数
减少不必要的胶水函数,降低代码量
一等公民的函数
正确地为参数命名,提高代码通
用性,避免重复造轮子
(特别容易把自己限定在特定的
业务上)
纯函数
纯函数是这样一种函数,即相同的输
入,永远会得到相同的输出,而且没
有任何可观察的副作用。
副作用
副作用是在计算结果的过程中,系统
状态的一种变化,或者与外部世界进
行的可观察的交互。
• 更改文件系统
• 往数据库插入记录
• 发送一个 http 请求
• 可变数据
• 打印 / log
• 获取用户输入
副作用并不都是贬义的,在计算机中,
副作用有时正是“主要作用”。
为何钟爱纯函数
• 可缓存性,因为对于同样的输入,总是同样的输出
• 可移植性/自文档化,因为纯函数的依赖很明确,不依赖于系统变量,可以很清
楚地观察函数行为
• 可测试性
• 并行性,纯函数根本不需要访问共享的内存,而且根据其定义,纯函数也不会因
副作用而进入竞争态
point free
永远不必说出你的数据,函数无须提及将
要操作的数据是什么样的。
并非所有的函数式代码都是 point free 的
柯里化
只传递给函数一部分参数来调用
它,让它返回一个函数去处理剩
下的参数。
compose
组合像一系列管道那样把不同的函数联
系在一起,数据就可以也必须在其中流
动
组合看起来像是在饲养函数,通过不同
的函数结合产生新函数
示例
向 flickr 的 api 请求小狗的图片,展示在页面上
函数式编程二三事.pptx

Weitere ähnliche Inhalte

Empfohlen

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Empfohlen (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

函数式编程二三事.pptx

Hinweis der Redaktion

  1. “万物皆数”是毕达哥拉斯学派的哲学主张,就是认为:数是万物的本原。 数学语言是是可以脱离现实而存在的,大家把数学想成了一种符号游戏,脱离生活常识,从公理开始, 进行大量的推导和证明——最终产生了一个系统,里面有公里、定理、推论、猜想... 上述这种自成体系,有公理又承认推理证明方法的体系,称为形式系统。
  2. λ演算是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义、函数如何被应用以及递归的形式系统
  3. 邱奇编码给出了编程语言所需的几乎所有元素,这是以λ演算为核心的函数式编程语言的理论基础。
  4. 大意是说,在编程语言中,所谓一等公民,是指支持所有操作的实体, 这些操作通常包括作为参数传递,从函数返回,修改并分配给变量等。 当我们说函数是“一等公民”的时候,我们实际上说的是它们和其他对象都一样,函数真没什么特殊的,你可以像对待任何其他数据类型一样对待它们——把它们存在数组里,当作参数传递,赋值给变量...等等。
  5. 转变思维,函数有可能被复用
  6. 疫苗 函数式编程的哲学就是假定副作用是造成不正当行为的主要原因。这并不是说,要禁止使用一切副作用,而是说,要让它们在可控的范围内发生。
  7. 将参数缺席称为“point-free”导致了不必要的晦涩,故而有了别名为“pointless”
  8. 对函数进行柯里化时,需要策略性地把要操作的数据放到最后一个参数里。 柯里化在做函数组合 compose 时非常有用,让人们将关注的重点聚焦到函数本身,而不因冗余的数据参数分散注意力。 能让函数回归到原始的:一个参数进去一个值出来的状态。
  9. 纯函数只针对输入和输出 传统,数学上定义