高阶函数可以提高代码的可复用性
下例有许多重复的代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54import java.util.ArrayList;
import java.util.List;
public class Customer {
static public ArrayList<Customer> allCustomers = new ArrayList<Customer>();
public Integer id = 0;
public String name = "";
public String address = "";
public String state = "";
public String primaryContact = "";
public String domain = "";
public Boolean enabled = true;
public Customer() {}
public static List<String> getEnabledCustomerNames() {
ArrayList<String> outList = new ArrayList<String>();
for(Customer customer : Customer.allCustomers) {
if(customer.enabled) {
outList.add(customer.name);
}
}
return outList;
}
public static List<String> getEnabledCustomerStates() {
ArrayList<String> outList = new ArrayList<String>();
for(Customer customer : Customer.allCustomers) {
if(customer.enabled) {
outList.add(customer.state);
}
}
return outList;
}
public static List<String> getEnabledCustomerPrimaryContacts() {
ArrayList<String> outList = new ArrayList<String>();
for(Customer customer : Customer.allCustomers) {
if(customer.enabled) {
outList.add(customer.primaryContact);
}
}
return outList;
}
public static List<String> getEnabledCustomerDomains() {
ArrayList<String> outList = new ArrayList<String>();
for(Customer customer : Customer.allCustomers) {
if(customer.enabled) {
outList.add(customer.domain);
}
}
return outList;
}
/* TODO: functions getting other fields */
}
上面的代码,有许多重复的行,如1
2
3
4
5ArrayList<String> outList = new ArrayList<String>();
for(Customer customer : Customer.allCustomers) {
...
}
return outList;
如果可以传递函数,那么...的部分就可以通过调用一个函数参数来做,这部分代码就不需要重复了
用函数式编程语言下面这样的代码相当于上面的函数getEnabledCustomerNames,其中就传递了两个匿名函数
实现了lists:map和lists:filter这两个函数的复用1
2lists:map(fun(#customer{name = Name}) -> Name end,
lists:filter(fun(#customer{enabled = E}) -> E end, L)).
或者更简洁的方式1
[Name || #customer{enabled = true, name = Name} <- L].