成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

函數式TypeScript

開發 前端
談到函數式編程時,我們常提到機制、方法,而不是核心原則。函數式編程不是關于 Monad、Monoid 和 Zipper 這些概念的,雖然它們確實很有用。從根本上來說,函數式編程就是關于如使用通用的可復用函數進行組合編程。本文是我在重構 TypeScript 代碼時使用函數式的一些思考的結果。

 [[173137]]

談到函數式編程時,我們常提到機制、方法,而不是核心原則。函數式編程不是關于 Monad、Monoid 和 Zipper 這些概念的,雖然它們確實很有用。從根本上來說,函數式編程就是關于如使用通用的可復用函數進行組合編程。本文是我在重構 TypeScript 代碼時使用函數式的一些思考的結果。

首先,我們需要用到以下幾項技術:

  • 盡可能使用函數代替簡單值
  • 數據轉換過程管道化
  • 提取通用函數

來,開始吧!

假設我們有兩個類,Employee 和 Department。Employee 有 name 和 salary 屬性,Department 只是 Employee 的簡單集合。

  1. class Employee { 
  2.   constructor(public name: string, public salary: number) {} 
  3. class Department { 
  4.   constructor(public employees: Employee[]) {} 
  5.   works(employee: Employee): boolean { 
  6.     return this.employees.indexOf(employee) > -1; 
  7.   } 

我們要重構的是 averageSalary 函數。

  1. function averageSalary(employees: Employee[], minSalary: number, department?: Department): number { 
  2.    let total = 0; 
  3.    let count = 0; 
  4.    employees.forEach((e) => { 
  5.      if(minSalary <= e.salary && (department === undefined || department.works(e))){ 
  6.        total += e.salary; 
  7.        count += 1; 
  8.      } 
  9.    }); 
  10.   return total === 0 ? 0 : total / count

averageSalary 函數接收 employee 數組、***薪資 minSalary 以及可選的 department 作為參數。如果傳了 department 參數,函數會計算該部門中所有員工的平均薪資;若不傳,則對全部員工進行計算。

該函數的使用方式如下:

  1. describe("average salary", () => { 
  2.   const empls = [ 
  3.     new Employee("Jim", 100), 
  4.     new Employee("John", 200), 
  5.     new Employee("Liz", 120), 
  6.     new Employee("Penny", 30) 
  7.   ]; 
  8.   const sales = new Department([empls[0], empls[1]]); 
  9.    
  10.   it("calculates the average salary", () => { 
  11.     expect(averageSalary(empls, 50, sales)).toEqual(150); 
  12.     expect(averageSalary(empls, 50)).toEqual(140); 
  13.   }); 

需求雖簡單粗暴,可就算不提代碼難以拓展,其混亂是顯而易見的。若新增條件,函數簽名及接口就不得不發生變動,if 語句也會也越來越臃腫可怕。

我們一起用一些函數式編程的辦法重構這個函數吧。

使用函數代替簡單值

使用函數代替簡單值看起來似乎不太直觀,但這卻是整理歸納代碼的強大辦法。在我們的例子中,這樣做,意味著要將 minSalary 和 department 參數替換成兩個條件檢驗的函數。

  1. type Predicate = (e: Employee) => boolean; 
  2. function averageSalary(employees: Employee[], salaryCondition: Predicate,  
  3.   departmentCondition?: Predicate): number { 
  4.   let total = 0; 
  5.   let count = 0; 
  6.   employees.forEach((e) => { 
  7.     if(salaryCondition(e) && (departmentCondition === undefined || departmentCondition(e))){ 
  8.       total += e.salary; 
  9.       count += 1; 
  10.     } 
  11.   }); 
  12.   return total === 0 ? 0 : total / count
  13. // ... 
  14. expect(averageSalary(empls, (e) => e.salary > 50, (e) => sales.works(e))).toEqual(150); 

我們所做的就是將 salary、department 兩個條件接口統一起來。而此前這兩個條件是寫死的,現在它們被明確定義了,并且遵循一致的接口。這次整合允許我們將所有條件作為數組傳遞。

  1. function averageSalary(employees: Employee[], conditions: Predicate[]): number { 
  2.   let total = 0; 
  3.   let count = 0; 
  4.   employees.forEach((e) => { 
  5.     if(conditions.every(c => c(e))){ 
  6.       total += e.salary; 
  7.       count += 1; 
  8.     } 
  9.   }); 
  10.   return (count === 0) ? 0 : total / count
  11. //... 
  12. expect(averageSalary(empls, [(e) => e.salary > 50, (e) => sales.works(e)])).toEqual(150); 

條件數組只不過是組合的條件,可以用一個簡單的組合器將它們放到一起,這樣看起來更加明晰。

  1. function and(predicates: Predicate[]): Predicate{ 
  2.   return (e) => predicates.every(p => p(e)); 
  3. function averageSalary(employees: Employee[], conditions: Predicate[]): number { 
  4.   let total = 0; 
  5.   let count = 0; 
  6.   employees.forEach((e) => { 
  7.     if(and(conditions)(e)){ 
  8.       total += e.salary; 
  9.       count += 1; 
  10.     } 
  11.   }); 
  12.   return (count == 0) ? 0 : total / count

值得注意的是,“and” 組合器是通用的,可以復用并且還可能拓展為庫。

提起結果

現在,averageSalary 函數已健壯得多了。我們可以加入新條件,無需破壞函數接口或改變函數實現。

數據轉換過程管道化

函數式編程的另外一個很有用的實踐是將所有數據轉換過程變成管道。在本例中,就是將 filter 過程提取到循環外面。

  1. function averageSalary(employees: Employee[], conditions: Predicate[]): number { 
  2.   const filtered = employees.filter(and(conditions)); 
  3.   let total = 0 
  4.   let count = 0 
  5.   filtered.forEach((e) => { 
  6.     total += e.salary; 
  7.     count += 1; 
  8.   }); 
  9.   return (count == 0) ? 0 : total / count

這樣一來計數的 count 就沒什么用了。

  1. function averageSalary(employees: Employee[], conditions: Predicate[]): number{ 
  2.   const filtered = employees.filter(and(conditions)); 
  3.   let total = 0 
  4.   filtered.forEach((e) => { 
  5.     total += e.salary; 
  6.   }); 
  7.   return (filtered.length == 0) ? 0 : total / filtered.length; 

接下來,如在疊加之前將 salary 摘取出來,求和過程就變成簡單的 reduce 了。

  1. function averageSalary(employees: Employee[], conditions: Predicate[]): number { 
  2.   const filtered = employees.filter(and(conditions)); 
  3.   const salaries = filtered.map(e => e.salary); 
  4.   const total = salaries.reduce((a,b) => a + b, 0); 
  5.   return (salaries.length == 0) ? 0 : total / salaries.length; 

提取通用函數

接著我們發現,***兩行代碼和當前域完全沒什么關系。其中不包含任何與員工、部門相關的信息。僅僅只是一個計算平均數的函數。所以也將其提取出來。

  1. function average(nums: number[]): number { 
  2.   const total = nums.reduce((a,b) => a + b, 0); 
  3.   return (nums.length == 0) ? 0 : total / nums.length; 
  4. function averageSalary(employees: Employee[], conditions: Predicate[]): number { 
  5.   const filtered = employees.filter(and(conditions)); 
  6.   const salaries = filtered.map(e => e.salary); 
  7.   return average(salaries); 

又一次,提取出的函數是完全通用的。

***,將所有 salary 部分提出來之后,我們得到***方案。

  1. function employeeSalaries(employees: Employee[], conditions: Predicate[]): number[] { 
  2.   const filtered = employees.filter(and(conditions)); 
  3.   return filtered.map(e => e.salary); 
  4. function averageSalary(employees: Employee[], conditions: Predicate[]): number { 
  5.   return average(employeeSalaries(employees, conditions)); 

對比原始方案和***方案,我敢說,毫無疑問,后者更棒。首先,它更通用(我們可以不破壞函數接口的情況下添加新類型的判斷條件)。其次,我們從可變狀態(mutable state)和 if 語句中解脫出來,這使代碼更容易閱讀、理解。

何時收手

函數式風格的編程中,我們會編寫許多小型函數,它們接收一個集合,返回新的集合。這些函數能夠以不同方式組合、復用 —— 棒極了。不過,這種風格的一個缺點是代碼可能會變得過度抽象,導致難以讀懂,那些函數組合在一起到底要干嘛?

我喜歡使用樂高來類比:樂高積木能夠以不同形式放在一起 —— 它們是可組合的。但注意,并不是所有積木都是一小塊。所以,在使用本文所述技巧進行代碼重構時,千萬別妄圖將一切都變成接收數組、返回數組的函數。誠然,這樣一些函數組合使用極度容易,可它們也會顯著降低我們對程序的理解能力。

小結

本文展示了如何使用函數式思維重構 TypeScript 代碼。我所遵循的是以下幾點規則:

  • 盡可能使用函數代替簡單值
  • 數據轉換過程管道化
  • 提取通用函數

了解更多

強烈推薦以下兩本書:

 

責任編輯:龐桂玉 來源: Linux中國
相關推薦

2016-09-30 09:43:17

JavascriptTypeScript函數式編程

2010-01-28 14:51:24

Scala后函數式

2023-05-16 16:03:10

2022-01-04 19:21:04

函數TypeScript重載

2023-04-14 15:44:20

TypeScrip函數重載

2022-02-25 09:19:32

TypeScript輔助函數枚舉

2019-09-09 11:40:18

編程函數開發

2013-09-09 09:41:34

2023-08-24 16:24:44

TypeScript

2017-06-08 14:25:46

Kotlin函數

2012-03-14 10:09:51

ibmdw

2022-07-07 09:03:36

Python返回函數匿名函數

2016-10-31 20:46:22

函數式編程Javascript

2011-03-08 15:47:32

函數式編程

2012-11-01 11:33:55

IBMdw

2020-09-24 10:57:12

編程函數式前端

2025-03-11 10:00:20

Golang編程函數

2014-09-05 10:15:41

函數式編程

2013-07-09 09:43:04

函數式思維函數式編程編程

2011-08-24 09:13:40

編程
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 天天拍天天草 | 成人免费视频观看 | 亚洲 欧美 日韩在线 | 亚洲美女视频 | 日韩精品成人在线 | 中文字幕亚洲精品 | 精品粉嫩aⅴ一区二区三区四区 | 亚洲欧洲精品成人久久奇米网 | 日韩电影中文字幕 | 国产成人精品久久二区二区91 | 亚洲自拍偷拍欧美 | 国产福利资源在线 | 亚洲天堂一区 | 午夜精品福利视频 | 欧美区在线观看 | 成人3d动漫一区二区三区91 | 在线播放国产一区二区三区 | 欧美日韩成人影院 | 国产资源视频 | 欧洲高清转码区一二区 | 精品欧美乱码久久久久久 | 欧美三级视频 | 久久久久91| 久草欧美 | 精品国产一区二区三区av片 | 激情欧美日韩一区二区 | 色婷婷影院 | 亚洲成人免费观看 | 日韩欧美一级精品久久 | 亚洲欧美精品在线 | 国产精品福利在线 | 亚洲精品乱码久久久久久9色 | 一区二区不卡 | 精品国产99 | 国产精品视频不卡 | 中文字幕免费在线 | 午夜视频在线观看网址 | 影音先锋中文字幕在线观看 | 中文字幕亚洲视频 | 三级av在线 | 欧美成人a|