函数式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函数式编程

2023-05-16 16:03:10

2022-01-04 19:21:04

函数TypeScript重载

2010-01-28 14:51:24

Scala后函数式

2023-04-14 15:44:20

TypeScrip函数重载

2022-02-25 09:19:32

TypeScript辅助函数枚举

2019-09-09 11:40:18

编程函数开发

2023-08-24 16:24:44

TypeScript

2013-09-09 09:41:34

2017-06-08 14:25:46

Kotlin函数

2012-03-14 10:09:51

ibmdw

2021-12-10 09:11:36

TypeScript 函数重载 TS 前端

2021-05-25 07:39:18

TypeScript 前端函数与类

2022-07-07 09:03:36

Python返回函数匿名函数

2014-09-05 10:15:41

函数式编程

2013-07-09 09:43:04

函数式思维函数式编程编程

2011-08-24 09:13:40

编程

2023-12-14 15:31:43

函数式编程python编程

2022-09-22 08:19:26

WebFlux函数式编程

2012-11-01 11:33:55

IBMdw
点赞
收藏

51CTO技术栈公众号