1. 程式人生 > >"Becoming Functional" 閱讀筆記+思維導圖

"Becoming Functional" 閱讀筆記+思維導圖

 

  <Becoming Functional>是O'Reilly公司今年(2014)7月釋出的一本薄薄的小冊子,151頁,介紹了函數語言程式設計的基本概念.全書使用程式碼範例都是基於JVM的程式語言,比如Java,Groovy,Scala.為了能夠講解所有的知識點,作者不得不在多個語言之間做切換,其實使用Erlang,Elixir甚至是C#做範例都不會這麼累(因為C#有Linq,Lazy.....).

   這本書側重點是講解基本概念,以及思維方式的轉變.所以無論是搞哪一種函數語言程式設計語言,都可以讀一讀,一開始接觸Erlang的小夥伴問的最多的就是:Tail Recursion,Side Effects,Higher-Order Functions,Anonymous Functions,closures,這裡恰好有一個不太複雜的講解.     這本書作為一個入門的小冊子,內容足夠簡單,可以快速翻完,下面是我閱讀該書的時候做下的思維導圖及其文字版.

 

附文字版

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.