<Becoming Functional>是O'Reilly公司今年(2014)7月发布的一本薄薄的小册子,151页,介绍了函数式编程的基本概念.全书使用代码范例都是基于JVM的编程语言,比如Java,Groovy,Scala.为了能够讲解所有的知识点,作者不得不在多个语言之间做切换,其实使用Erlang,Elixir甚至是C#做范例都不会这么累(因为C#有Linq,Lazy.....).
附文字版
Becoming Functional
First-class functions
can either accept another function as an argument or return a function.
Pure functions
are functions that have no side effects. Side effects are actions a function may perform that are not solely contained within the function itself.
Side Effects
Recursion
allows us to write smaller, more concise algorithms and to operate by looking only at the inputs to our functions.
Tail Recursion
Immutable variables
Immutable variables, once set, cannot be changed.
Nonstrict (Lazy) evaluation
allow us to have variables that have not been computed yet.
Strict evaluations-assigning a variable as soon as it is defined-are what we are used to
Nonstrict means that we can have a variable that does not get assigned (computed) until the first time it is referenced.
Statements
are evaluable pieces of code that have a return value.
Each line of code should be considered a statement, meaning there are very few side effects within the application itself.
Pattern matching
allows us to better type-check and extract elements from an object, making for simpler and more concise statements with less need for variable definitions.
extracting value
First-class functions
Anonymous Functions
closures
Closures are much like lambdas, except they reference variables outside the scope of the function.
In the simplest explanation, the body references a variable that doesn't exist in either the body or the parameter list.
lambda functions
Lambda functions are unnamed functions that contain a parameter list, a body, and a return.
Higher-Order Functions
A function becomes "higher order" if it accepts or returns a function.
Pure functions
Output Depends on Input
When we don't return the result of our execution but rather mutate another external (i.e., not contained within the function scope) object, we call this a side effect.
Pure functions are functions that have no side effects and always perform the same computation,resulting in the same output, given a set of inputs.
Really, you want to make a function pure whenever possible; it makes the function much more testable and improves understandability from a troubleshooting perspective.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2007-08-06 [本周] 就来说说Asp.net 身份验证、授权