本文承接自上一篇《iOS的block與鏈?zhǔn)骄幊趟枷搿?/p>
函數(shù)式編程
首先我們需要明白什么是函數(shù)式編程么葡公?先說一點比較枯燥的概念,具體詳細(xì)的定義可以詳見維基百科粱檀,摘抄如下一段:
In computer science, functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.
我是這樣理解的:
在計算機科學(xué)中笨鸡,函數(shù)式編程是一種編程范型沉桌,是一種構(gòu)建程序的數(shù)據(jù)結(jié)構(gòu)和元素的方法。它將計算機運算視為數(shù)學(xué)上的函數(shù)計算浴滴,并且避免使用程序狀態(tài)以及易變對象拓萌。
維基百科還提到函數(shù)編程語言最重要的基礎(chǔ)是λ演算(lambda calculus)。λ演算中最關(guān)鍵的要素就是函數(shù)被當(dāng)作變量處理升略,能夠參與運算微王。
概念說的有些云里霧里的,為了刨根問底品嚣,百科中還介紹了函數(shù)式編程的幾個特性:
First-class and higher-order functions:這樣拆成兩部分:First-class functions 和 higher-order functions炕倘,點進(jìn)去分別的定義如下。
First-class functions:
In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. Specifically, this means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures.[1] Some programming language theorists require support for anonymous functions (function literals) as well.
函數(shù)為“第一等公民”翰撑。這意味著這個語言支持函數(shù)作為其他函數(shù)的參數(shù)罩旋,也可以作為函數(shù)的返回值,并且可以被賦值給變量或者在數(shù)據(jù)結(jié)構(gòu)中存儲眶诈。一些編程語言也支持匿名函數(shù)或者函數(shù)字面量涨醋。
(感覺這明顯說的就是block啊2嵫)
Higher-order function
In mathematics and computer science, a higher-order function (also functional, functional form or functor) is a function that does at least one of the following:
takes one or more functions as arguments (i.e., procedural parameters),
returns a function as its result.
看到這里有點懵逼东帅,這兩個定義好相似啊。球拦。不怪我們英語水平問題靠闭,確實很相似:
*Higher-order functions are closely related to first-class functions in that higher-order functions and first-class functions both allow functions as arguments and results of other functions. *
The distinction between the two is subtle: "higher-order" describes a mathematical concept of functions that operate on other functions, while "first-class" is a computer science term that describes programming language entities that have no restriction on their use (thus first-class functions can appear anywhere in the program that other first-class entities like numbers can, including as arguments to other functions and as their return values).
區(qū)別就是高階函數(shù)描述的是數(shù)學(xué)概念上的函數(shù)操作,而一級函數(shù)是計算機科學(xué)中的專業(yè)名詞坎炼。愧膀。
未完待續(xù)。谣光。最近有點忙