-
Notifications
You must be signed in to change notification settings - Fork 7
/
generate_types.dart
354 lines (298 loc) · 10.3 KB
/
generate_types.dart
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import 'dart:io';
import 'utils/generating.dart';
/// Number of types to generate (exclusive).
const int max = 11;
/// Root directory of paths.
File getFile(String name) => File('lib/src/functional/types/$name.dart');
Future<void> generateCallback() async {
final file = getFile('callback');
final out = file.openWrite();
generateWarning(out);
out.writeln('/// Function types for generic callbacks.');
out.writeln('library;');
out.writeln();
out.writeln("import 'mapping.dart';");
for (var i = 0; i < max; i++) {
out.writeln('/// Callback function type with $i positional '
'argument${i == 1 ? '' : 's'}.');
out.writeln('typedef Callback$i${generify(generateTypes(i))} = '
'Map$i${generify([...generateTypes(i), 'void'])};');
out.writeln();
}
await out.close();
await format(file);
}
Future<void> generateMapping() async {
final file = getFile('mapping');
final out = file.openWrite();
generateWarning(out);
out.writeln('/// Function type for generic mapping functions.');
out.writeln('library;');
out.writeln();
for (var i = 0; i < max; i++) {
out.writeln('/// Mapping function type with $i positional '
'argument${i == 1 ? '' : 's'}.');
out.writeln('typedef Map$i${generify([...generateTypes(i), 'R'])} = '
'R Function(${listify(generateTypeAndArgs(i))});');
out.writeln();
}
await out.close();
await format(file);
}
Future<void> generatePredicate() async {
final file = getFile('predicate');
final out = file.openWrite();
generateWarning(out);
out.writeln('/// Function type for generic predicate functions.');
out.writeln('library;');
out.writeln();
out.writeln("import 'mapping.dart';");
for (var i = 0; i < max; i++) {
out.writeln('/// Predicate function type with $i positional '
'argument${i == 1 ? '' : 's'}.');
out.writeln('typedef Predicate$i${generify(generateTypes(i))} = '
'Map$i${generify([...generateTypes(i), 'bool'])};');
out.writeln();
}
await out.close();
await format(file);
}
Future<void> generateEmpty() async {
final file = getFile('empty');
final out = file.openWrite();
generateWarning(out);
out.writeln('/// Generic empty functions returning nothing.');
out.writeln('library;');
out.writeln();
for (var i = 0; i < max; i++) {
out.writeln('/// Empty function with $i positional '
'argument${i == 1 ? '' : 's'}.');
out.writeln('void emptyFunction$i${generify(generateTypes(i))} '
'(${listify(generateTypeAndArgs(i))}) {}');
out.writeln();
}
await out.close();
await format(file);
}
Future<void> generateIdentity() async {
final file = getFile('identity');
final out = file.openWrite();
generateWarning(out);
out.writeln('/// Generic identity functions.');
out.writeln('library;');
out.writeln();
out.writeln('/// Generic identity function with 1 positional argument.');
out.writeln('T identityFunction<T>(T arg) => arg;');
out.writeln();
await out.close();
await format(file);
}
Future<void> generateConstant() async {
final file = getFile('constant');
final out = file.openWrite();
generateWarning(out);
out.writeln('/// The constant functions.');
out.writeln('library;');
out.writeln();
out.writeln("import 'mapping.dart';");
out.writeln();
for (var i = 0; i < max; i++) {
final prefix = '$i${generify([...generateTypes(i), 'R'])}';
out.writeln('/// Constant function with $i positional '
'argument${i == 1 ? '' : 's'}.');
out.writeln('Map$prefix constantFunction$prefix(R value) '
'=> (${listify(generateArgs(i))}) '
'=> value;');
out.writeln();
}
await out.close();
await format(file);
}
Future<void> generateThrowing() async {
final file = getFile('throwing');
final out = file.openWrite();
generateWarning(out);
out.writeln('/// The throwing functions.');
out.writeln('library;');
out.writeln();
out.writeln("import 'mapping.dart';");
out.writeln();
for (var i = 0; i < max; i++) {
final mapType = '$i${generify([...generateTypes(i), 'Never'])}';
final throwType = '$i${generify([...generateTypes(i)])}';
out.writeln('/// Throwing function with $i positional '
'argument${i == 1 ? '' : 's'}.');
out.writeln('Map$mapType throwFunction$throwType(Object throwable) '
'=> (${listify(generateArgs(i))}) '
'=> throw throwable;');
out.writeln();
}
await out.close();
await format(file);
}
Future<void> generatePartial() async {
final file = getFile('partial');
final out = file.openWrite();
generateWarning(out);
out.writeln('/// Binds positional arguments and returns a new function:');
out.writeln('/// https://en.wikipedia.org/wiki/Partial_application');
out.writeln('library;');
out.writeln();
out.writeln("import 'mapping.dart';");
out.writeln();
for (var i = 1; i < max; i++) {
final types = [...generateTypes(i), 'R'];
final argsAndTypes = generateTypeAndArgs(i);
final args = generateArgs(i);
out.writeln('extension Partial$i${generify(types)} '
'on Map$i${generify(types)} {');
for (var j = 0; j < i; j++) {
final resultType = types.toList()..removeAt(j);
final unboundArgs = argsAndTypes.toList()..removeAt(j);
out.writeln('/// Returns a new function with the ${ordinal.print(j)} '
'argument bound to `arg$j`.');
out.writeln('Map${i - 1}${generify(resultType)} '
'bind$j(${argsAndTypes[j]}) => ');
out.writeln('(${listify(unboundArgs)}) => this(${listify(args)});');
out.writeln();
}
out.writeln('}');
out.writeln();
}
await out.close();
await format(file);
}
Future<void> generateCurry() async {
final file = getFile('curry');
final out = file.openWrite();
generateWarning(out);
out.writeln('/// Converts a function with positional arguments into a '
'sequence of functions ');
out.writeln('/// taking a single argument: '
'https://en.wikipedia.org/wiki/Currying');
out.writeln('library;');
out.writeln();
out.writeln("import 'mapping.dart';");
out.writeln();
for (var i = 1; i < max; i++) {
final types = [...generateTypes(i), 'R'];
final argsAndTypes = generateTypeAndArgs(i);
final args = generateArgs(i);
out.writeln('extension Curry$i${generify(types)} '
'on Map$i${generify(types)} {');
out.writeln('/// Converts a function with $i positional arguments into a '
'sequence of $i ');
out.writeln('/// functions taking a single argument.');
for (var j = 0; j < i; j++) {
out.write('Map1<${types[j]}, ');
}
out.write('${types.last}${'>' * i} get curry => ');
for (var j = 0; j < i; j++) {
out.write('(${argsAndTypes[j]}) =>');
}
out.writeln('this(${listify(args)});');
out.writeln('}');
out.writeln();
}
await out.close();
await format(file);
}
Future<void> generateTest() async {
final file = File('test/functional_type_test.dart');
final out = file.openWrite();
generateWarning(out);
void nest(String type, String name, void Function() callback) {
out.writeln('$type(\'$name\', () {');
callback();
out.writeln('});');
}
out.writeln('// ignore_for_file: unnecessary_lambdas');
out.writeln();
out.writeln('import \'package:more/functional.dart\';');
out.writeln('import \'package:test/test.dart\';');
out.writeln();
out.writeln('void main() {');
nest('group', 'constant', () {
for (var i = 0; i < max; i++) {
nest('test', 'constantFunction$i', () {
final types = generify([for (var j = 0; j < i; j++) 'int', 'String']);
final values = listify(List.generate(i, (i) => '$i'));
out.writeln('final function = constantFunction$i$types(\'default\');');
out.writeln('expect(function($values), \'default\');');
});
}
});
nest('group', 'empty', () {
for (var i = 0; i < max; i++) {
nest('test', 'emptyFunction$i', () {
final values = listify(List.generate(i, (i) => '$i'));
out.writeln('expect(() => emptyFunction$i($values), '
'isNot(throwsException));');
});
}
});
nest('test', 'identity', () {
out.writeln('expect(identityFunction(42), 42);');
out.writeln('expect(identityFunction(\'foo\'), \'foo\');');
});
nest('group', 'throwing', () {
out.writeln('final throwable = UnimplementedError();');
for (var i = 0; i < max; i++) {
nest('test', 'throwFunction$i', () {
final types = generify([for (var j = 0; j < i; j++) 'int']);
final values = listify(List.generate(i, (i) => '$i'));
out.writeln('final function = throwFunction$i$types(throwable);');
out.writeln('expect(() => function($values), '
'throwsUnimplementedError);');
});
}
});
nest('group', 'partial', () {
for (var i = 1; i < max; i++) {
nest('group', '$i-ary function', () {
for (var j = 0; j < i; j++) {
nest('test', 'bind ${ordinal.print(j)} argument', () {
final args = generateArgs(i);
final typesAndArgs = args.map((each) => 'int $each');
out.writeln('List<int> function(${listify(typesAndArgs)}) => '
'[${listify(args)}];');
out.writeln('final bound = function.bind$j(-1);');
final arguments = List.generate(i - 1, (i) => '$i');
final expected = arguments.toList()..insert(j, '-1');
out.writeln('expect(bound(${listify(arguments)}), '
'[${listify(expected)}]);');
});
}
});
}
});
nest('group', 'curry', () {
for (var i = 1; i < max; i++) {
nest('test', '$i-ary function', () {
final args = generateArgs(i);
final typesAndArgs = args.map((each) => 'int $each');
out.writeln('List<int> function(${listify(typesAndArgs)}) => '
'[${listify(args)}];');
final calls = List.generate(i, (i) => '($i)');
final result = List.generate(i, (i) => '$i');
out.writeln('expect(function.curry${calls.join()}, '
'[${listify(result)}]);');
});
}
});
out.writeln('}');
await out.close();
await format(file);
}
Future<void> main() => Future.wait([
generateCallback(),
generateConstant(),
generateCurry(),
generateEmpty(),
generateIdentity(),
generateMapping(),
generatePartial(),
generatePredicate(),
generateThrowing(),
generateTest(),
]);