在计算机科学中,柯里化(英语:Currying),又译为卡瑞化或加里化,是把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数,并且返回接受余下的参数而且返回结果的新函数的技术。
在理论计算机科学中,柯里化提供了在简单的理论模型中,比如:只接受一个单一参数的lambda演算中,研究带有多个参数的函数的方式。
函数柯里化的对偶是Uncurrying,一种使用匿名单参数函数来实现多参数函数的方法。
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
public class Letter {
private String returnAddress;
private String insideAddress;
private LocalDate dateOfLetter;
private String salutation;
private String body;
private String closing;
public Letter(
String returnAddress,
String insideAddress,
LocalDate dateOfLetter,
String salutation,
String body,
String closing) {
this.returnAddress = returnAddress;
this.insideAddress = insideAddress;
this.dateOfLetter = dateOfLetter;
this.salutation = salutation;
this.body = body;
this.closing = closing;
}
public static final Function<
String,
Function<
String,
Function<LocalDate, Function<String, Function<String, Function<String, Letter>>>>>>
LETTER_CREATER =
returnAddress ->
insideAddress ->
dateOfLetter ->
salutation ->
body ->
closing ->
new Letter(
returnAddress,
insideAddress,
dateOfLetter,
salutation,
body,
closing);
static AddReturnAddress builder() {
return returnAddress ->
closing ->
dateOfLetter ->
insideAddress ->
salutation ->
body ->
new Letter(
returnAddress,
insideAddress,
dateOfLetter,
salutation,
body,
closing);
}
interface AddReturnAddress {
Letter.AddClosing withReturnAddress(String returnAddress);
}
interface AddClosing {
Letter.AddDateOfLetter withClosing(String closing);
}
interface AddDateOfLetter {
Letter.AddInsideAddress withDateOfLetter(LocalDate dateOfLetter);
}
interface AddInsideAddress {
Letter.AddSalutation withInsideAddress(String insideAddress);
}
interface AddSalutation {
Letter.AddBody withSalutation(String salutation);
}
interface AddBody {
Letter withBody(String body);
}
@Override
public String toString() {
return "Letter{"
+ "returnAddress='"
+ returnAddress
+ '\''
+ ", insideAddress='"
+ insideAddress
+ '\''
+ ", dateOfLetter="
+ dateOfLetter
+ ", salutation='"
+ salutation
+ '\''
+ ", body='"
+ body
+ '\''
+ ", closing='"
+ closing
+ '\''
+ '}';
}
public String getReturnAddress() {
return returnAddress;
}
public void setReturnAddress(String returnAddress) {
this.returnAddress = returnAddress;
}
public String getInsideAddress() {
return insideAddress;
}
public void setInsideAddress(String insideAddress) {
this.insideAddress = insideAddress;
}
public LocalDate getDateOfLetter() {
return dateOfLetter;
}
public void setDateOfLetter(LocalDate dateOfLetter) {
this.dateOfLetter = dateOfLetter;
}
public String getSalutation() {
return salutation;
}
public void setSalutation(String salutation) {
this.salutation = salutation;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getClosing() {
return closing;
}
public void setClosing(String closing) {
this.closing = closing;
}
public static void main(String[] args) {
Letter apply =
LETTER_CREATER
.apply("return address")
.apply("inside address")
.apply(LocalDate.now())
.apply("salutation")
.apply("body")
.apply("closing");
System.out.println(apply);
Letter letter =
Letter.builder()
.withReturnAddress("return address")
.withClosing("closing")
.withDateOfLetter(LocalDate.now())
.withInsideAddress("inside address")
.withSalutation("salutation")
.withBody("body");
System.out.println(letter);
}
}
|