讓咱們談談什麼是:lambdas(匿名函數)、 first-class functions(頭等函數)、higher-order functions(高階函數)、unary functions(一元函數)、currying(柯里化 )和pure functions(純函數)。express
Lambdas (λ) 在 JavaScript 做爲arrow functions(箭頭函數)被廣爲所知:編程
// this is your regular named function in JavaScriptfunction namedFunction (a, b) { return a + b;}// this is a lambda, i.e. an arrow functionconst lambda = (a, b) => a + b;
術語lambda是一個正式的數學邏輯系統,起源於lambda演算。Lambda演算是由圖靈完成的,它表明了可以構建任何圖靈機的通用計算模型。 Lambda expressions(匿名函數表達式) 是函數式編程的基石。若是它對你有所幫助,只需將它視爲函數的新縮短語法就行。然而,在對象或類中使用它們時要注意this的指向。數組
First-class type 意味着,該類型能夠用做變量的值。在JavaScript中一個字符串是頭等類型,一個函數也是頭等類型。因此函數能夠接受其餘函數做爲參數,並返回函數做爲返回值。函數式編程
在綁定事件監聽器時,函數做爲first-class被使用:函數
const handler = () => console.log ('I am function');document.addEventListener ('click', handler);
高階函數是一個接受其餘函數做爲參數或將函數做爲返回值返回的函數。 First-order function(一階函數)是一個函數,它不接受其餘函數做爲參數,而且不返回函數做爲其返回值。單元測試
const firstOrder = () => console.log ('First order strikes back!');const higherOrder = whoStrikesBack => whoStrikesBack ();higherOrder (firstOrder);
該術語涉及一個函數接受一些參數的元數。一元函數(i.e. monadic)是一個只接受一個參數的函數。測試
const unaryFunction = message => console.log (message);const binaryFunction = (color, message) => console.log (`%c${message}`, `color:${color}`);const ternaryFunction = (fnc, color, message) => fnc (`%c${message}`, `color:${color}`);
Currying(柯里化)是一個帶有多個參數的函數並將其轉換爲函數序列的過程,每一個函數只有一個參數。this
一個有n個參數的函數,可使用柯里化將它變成一個一元函數。code
const binaryFunction = (a, b) => a + b;const curryUnaryFunction = a => b => a + b;curryUnaryFunction (1); // returns a function: b => 1 + bcurryUnaryFunction (1) (2); // returns the number 3
Currying(柯里化)以數學家 Haskell Curry的名字命名,不是吃的。對象
柯里化函數很是適合提升代碼的可重用性和函數式結構。想了解更多,請參考: JavaScript ES6 curry functions with practical examples。它可能會讓人習慣,可是我如今寫的全部函數都歸功於柯里化。
純函數是一種其返回值僅由其參數決定,沒有任何反作用的函數。
這意味着若是你在整個應用程序中的不一樣的一百個地放調用一個純函數相同的參數一百次,該函數始終返回相同的值。純函數不會更改或讀取外部狀態。
let myArray = [];const impureAddNumber = number => myArray.push (number);const pureAddNumber = number => anArray => anArray.concat ([number]);console.log (impureAddNumber (2)); // returns 1console.log (myArray); // returns [2]console.log (pureAddNumber (3) (myArray)); // returns [2, 3]console.log (myArray); // returns [2]myArray = pureAddNumber (3) (myArray);console.log (myArray); // returns [2, 3]
在數組中,Push函數就是不純的,它會改變它所調用的數組,所以會產生反作用。 push的返回值是一個數字索引。
另外,Concat接受數組並將其與另外一個數組鏈接,從而產生一個沒有反作用的全新數組。而後返回兩個數組串聯後的新數組。
純函數很重要,由於它們簡化了單元測試(沒有反作用,也不須要依賴注入),它們避免緊密耦合,並經過消除反作用,使應用程序更加簡潔。