-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDFun.List.pas
524 lines (471 loc) · 14.2 KB
/
DFun.List.pas
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
unit DFun.List;
interface
uses
System.Classes,
System.SysUtils,
System.Generics.Collections,
System.Generics.Defaults,
DFun.Maybe,
DFun.Pair;
type
IList<A> = interface
['{F2AD450F-F208-4026-B603-A95C9C69191D}']
end;
IEmptyCell<A> = interface(IList<A>)
['{F382F9DC-3615-4FFA-BC44-4A055FC8A78B}']
end;
IConsCell<A> = interface(IList<A>)
['{C9CA2B0E-AFAF-4597-A139-57CA5EAAA9C6}']
function GetHead: A;
function GetTail: IList<A>;
property Head: A read GetHead;
property Tail: IList<A> read GetTail;
end;
List = class
class function Empty<A>: IList<A>;
class function Cons<A>(const AHead: A; const ATail: IList<A>): IList<A>;
class function IsEmpty<A>(const AList: IList<A>): Boolean;
class function Singleton<A>(const AValue: A): IList<A>;
class function Generate<A>(const AFunc: TFunc<A>;
const ACount: Integer): IList<A>;
class function ToString(const AList: IList<String>): String; reintroduce;
class function ToStringWith<A>(const AFunc: TFunc<A, String>;
const AList: IList<A>): String;
class function FromArray<A>(const AList: array of A): IList<A>;
class function FromEnumerable<A>(const AList: IEnumerable<A>): IList<A>;
class function FromTEnumerable<A>(const AList: TEnumerable<A>): IList<A>;
class function FromTList<A>(const AList: TList<A>): IList<A>;
class function ToTList<A>(const AList: IList<A>): TList<A>;
class function Head<A>(const AList: IList<A>): IMaybe<A>;
class function Tail<A>(const AList: IList<A>): IMaybe<IList<A>>;
class function Append<A>(const ALeft, ARight: IList<A>): IList<A>;
class function Join<A>(const ALists: IList<IList<A>>): IList<A>;
class function Map<A, B>(const AFunc: TFunc<A, B>;
const AList: IList<A>): IList<B>;
class function AndThen<A, B>(const AFunc: TFunc<A, IList<B>>;
const AList: IList<A>): IList<B>;
class function Filter<A>(const AFunc: TFunc<A, Boolean>;
const AList: IList<A>): IList<A>;
class function FoldLeft<A, B>(const AFunc: TFunc<A, B, B>;
const AInit: B;
const AList: IList<A>): B;
class function FoldRight<A, B>(const AFunc: TFunc<A, B, B>;
const AInit: B;
const AList: IList<A>): B;
class function Reverse<A>(const AList: IList<A>): IList<A>;
class function Sum(const AList: IList<Integer>): Integer; overload;
class function Sum(const AList: IList<Extended>): Extended; overload;
class function Product(const AList: IList<Integer>): Integer; overload;
class function Product(const AList: IList<Extended>): Extended; overload;
class function All<A>(const AFunc: TFunc<A, Boolean>;
const AList: IList<A>): Boolean;
class function Any<A>(const AFunc: TFunc<A, Boolean>;
const AList: IList<A>): Boolean;
class procedure Each<A>(const AProc: TProc<A>;
const AList: IList<A>);
class function SortBy<A>(const AFunc: TFunc<A, A, Integer>;
const AList: IList<A>): IList<A>;
class function GroupBy<A>(const AFunc: TFunc<A, A, Integer>;
const AList: IList<A>): IList<IList<A>>;
class function SortAndGroupBy<A>(const AFunc: TFunc<A, A, Integer>;
const AList: IList<A>): IList<IList<A>>;
class function Length<A>(const AList: IList<A>): Integer;
end;
EmptyCell<A> = class(TInterfacedObject, IList<A>, IEmptyCell<A>)
end;
ConsCell<A> = class(TInterfacedObject, IList<A>, IConsCell<A>)
strict private
fHead: A;
fTail: IList<A>;
public
constructor Create(const AHead: A; const ATail: IList<A>);
destructor Destroy; override;
function GetHead: A;
function GetTail: IList<A>;
property Head: A read GetHead;
property Tail: IList<A> read GetTail;
end;
TFunComparer<A> = class(TInterfacedObject, IComparer<A>)
private
fFunc: TFunc<A, A, Integer>;
public
constructor Create(const AFunc: TFunc<A, A, Integer>);
function Compare(const Left, Right: A): Integer;
end;
implementation
{ List }
class function List.All<A>(const AFunc: TFunc<A, Boolean>;
const AList: IList<A>): Boolean;
begin
Result := FoldLeft<A, Boolean>(
function(X: A; Acc: Boolean): Boolean begin Result := Acc and AFunc(X) end,
True,
AList);
end;
class function List.AndThen<A, B>(const AFunc: TFunc<A, IList<B>>;
const AList: IList<A>): IList<B>;
begin
Result := List.Join<B>(List.Map<A, IList<B>>(AFunc, AList));
end;
class function List.Any<A>(const AFunc: TFunc<A, Boolean>;
const AList: IList<A>): Boolean;
begin
Result := FoldLeft<A, Boolean>(
function(X: A; Acc: Boolean): Boolean begin Result := Acc or AFunc(X) end,
False,
AList);
end;
class function List.Append<A>(const ALeft, ARight: IList<A>): IList<A>;
begin
if IsEmpty<A>(ALeft) then begin
Result := ARight;
end else if IsEmpty<A>(ARight) then begin
Result := ALeft;
end else begin
Result := FoldRight<A, IList<A>>(
function(X: A; Acc: IList<A>): IList<A> begin
Result := Cons<A>(X, Acc);
end,
ARight,
ALeft);
end;
end;
class function List.Tail<A>(const AList: IList<A>): IMaybe<IList<A>>;
var
Cc: IConsCell<A>;
begin
if Supports(AList, IConsCell<A>, Cc) then begin
Result := Maybe.Just<IList<A>>(Cc.Tail);
end else begin
Result := Maybe.Nothing<IList<A>>;
end;
end;
class function List.ToString(const AList: IList<String>): String;
begin
Result := ToStringWith<String>(
function(X: String): String begin Result := X end,
AList);
end;
class function List.ToStringWith<A>(const AFunc: TFunc<A, String>;
const AList: IList<A>): String;
var
Sb: TStringBuilder;
First: Boolean;
begin
First := True;
Sb := TStringBuilder.Create;
try
Sb.Append('[');
Each<A>(
procedure(X: A) begin
if First then begin
First := False;
end else begin
Sb.Append(', ');
end;
Sb.Append(AFunc(X));
end,
AList);
Sb.Append(']');
Result := Sb.ToString;
finally
Sb.Free;
end;
end;
class function List.Cons<A>(const AHead: A;
const ATail: IList<A>): IList<A>;
begin
Result := ConsCell<A>.Create(AHead, ATail);
end;
class procedure List.Each<A>(const AProc: TProc<A>; const AList: IList<A>);
begin
FoldLeft<A, Integer>(
function(X: A; Acc: Integer): Integer begin AProc(X); Result := 0; end,
0,
AList);
end;
class function List.Empty<A>: IList<A>;
begin
Result := EmptyCell<A>.Create;
end;
class function List.Filter<A>(const AFunc: TFunc<A, Boolean>;
const AList: IList<A>): IList<A>;
begin
Result := FoldRight<A, IList<A>>(
function(X: A; Acc: IList<A>): IList<A> begin
if AFunc(X) then begin
Result := Cons<A>(X, Acc);
end else begin
Result := Acc;
end;
end,
Empty<A>,
AList);
end;
class function List.FoldLeft<A, B>(const AFunc: TFunc<A, B, B>;
const AInit: B; const AList: IList<A>): B;
var
Xs: IList<A>;
Cc: IConsCell<A>;
begin
Xs := AList;
Result := AInit;
while Supports(Xs, IConsCell<A>, Cc) do begin
Result := AFunc(Cc.Head, Result);
Xs := Cc.Tail;
end;
end;
class function List.FoldRight<A, B>(const AFunc: TFunc<A, B, B>;
const AInit: B; const AList: IList<A>): B;
begin
Result := FoldLeft<A, B>(AFunc, AInit, Reverse<A>(AList));
end;
class function List.FromArray<A>(const AList: array of A): IList<A>;
var
I: Integer;
begin
Result := Empty<A>;
for I := System.Length(AList) - 1 downto 0 do begin
Result := Cons<A>(AList[I], Result);
end;
end;
class function List.FromEnumerable<A>(
const AList: IEnumerable<A>): IList<A>;
var
X: A;
begin
Result := Empty<A>;
for X in AList do begin
Result := Cons<A>(X, Result);
end;
Result := Reverse<A>(Result);
end;
class function List.FromTEnumerable<A>(
const AList: TEnumerable<A>): IList<A>;
var
X: A;
begin
Result := Empty<A>;
for X in AList do begin
Result := Cons<A>(X, Result);
end;
Result := Reverse<A>(Result);
end;
class function List.FromTList<A>(const AList: TList<A>): IList<A>;
var
I: Integer;
begin
Result := Empty<A>;
for I := AList.Count - 1 downto 0 do begin
Result := Cons<A>(AList[I], Result);
end;
end;
class function List.Generate<A>(const AFunc: TFunc<A>;
const ACount: Integer): IList<A>;
var
I: Integer;
begin
Result := Empty<A>;
for I := 0 to ACount - 1 do begin
Result := Cons<A>(AFunc, Result);
end;
Result := Reverse<A>(Result);
end;
class function List.GroupBy<A>(const AFunc: TFunc<A, A, Integer>;
const AList: IList<A>): IList<IList<A>>;
var
Acc: IPair<IMaybe<IPair<A, IList<A>>>, IList<IList<A>>>;
begin
Acc :=
FoldLeft<A, IPair<IMaybe<IPair<A, IList<A>>>, IList<IList<A>>>>(
function(X: A; Acc: IPair<IMaybe<IPair<A, IList<A>>>, IList<IList<A>>>): IPair<IMaybe<IPair<A, IList<A>>>, IList<IList<A>>> begin
Result := Maybe.Match<IPair<A, IList<A>>, IPair<IMaybe<IPair<A, IList<A>>>, IList<IList<A>>>>(
function: IPair<IMaybe<IPair<A, IList<A>>>, IList<IList<A>>> begin
Result := Pair.Create(
Maybe.Just(
Pair.Create(
X,
List.Singleton(X))),
List.Empty<IList<A>>);
end,
function(Y: IPair<A, IList<A>>): IPair<IMaybe<IPair<A, IList<A>>>, IList<IList<A>>> begin
if AFunc(X, Y.First) = 0 then begin
Result := Pair.Create(
Maybe.Just(
Pair.Create(
Y.First,
List.Cons<A>(X, Y.Second))),
Acc.Second);
end else begin
Result := Pair.Create(
Maybe.Just(
Pair.Create(
X,
List.Singleton(X))),
List.Cons<IList<A>>(Y.Second, Acc.Second));
end;
end,
Acc.First);
end,
Pair.Create(Maybe.Nothing<IPair<A, IList<A>>>, List.Empty<IList<A>>),
AList);
Result :=
List.Reverse<IList<A>>(
Maybe.Match<IPair<A, IList<A>>, IList<IList<A>>>(
function: IList<IList<A>> begin
Result := Acc.Second;
end,
function(X: IPair<A, IList<A>>): IList<IList<A>> begin
Result := List.Cons<IList<A>>(X.Second, Acc.Second);
end,
Acc.First));
end;
class function List.Head<A>(const AList: IList<A>): IMaybe<A>;
var
Cc: IConsCell<A>;
begin
if Supports(AList, IConsCell<A>, Cc) then begin
Result := Maybe.Just<A>(Cc.Head);
end else begin
Result := Maybe.Nothing<A>;
end;
end;
class function List.IsEmpty<A>(const AList: IList<A>): Boolean;
begin
Result := Supports(AList, IEmptyCell<A>);
end;
class function List.Join<A>(const ALists: IList<IList<A>>): IList<A>;
begin
Result := List.FoldLeft<IList<A>, IList<A>>(
function(X: IList<A>; Acc: IList<A>): IList<A> begin
Result := List.Append<A>(Acc, X);
end,
List.Empty<A>,
ALists);
end;
class function List.Length<A>(const AList: IList<A>): Integer;
begin
Result := FoldLeft<A, Integer>(
function(X: A; Acc: Integer): Integer begin Result := Acc + 1 end,
0,
AList);
end;
class function List.Map<A, B>(const AFunc: TFunc<A, B>;
const AList: IList<A>): IList<B>;
begin
Result := FoldRight<A, IList<B>>(
function(X: A; Acc: IList<B>): IList<B> begin
Result := Cons<B>(AFunc(X), Acc);
end,
Empty<B>,
AList);
end;
class function List.Product(const AList: IList<Integer>): Integer;
begin
Result := FoldLeft<Integer, Integer>(
function (A, B: Integer): Integer begin Result := A * B end,
1,
AList);
end;
class function List.Product(const AList: IList<Extended>): Extended;
begin
Result := FoldLeft<Extended, Extended>(
function (A, B: Extended): Extended begin Result := A * B end,
1.0,
AList);
end;
class function List.Reverse<A>(const AList: IList<A>): IList<A>;
begin
Result := FoldLeft<A, IList<A>>(
function(X: A; Acc: IList<A>): IList<A> begin
Result := Cons<A>(X, Acc);
end,
Empty<A>,
AList);
end;
class function List.Sum(const AList: IList<Integer>): Integer;
begin
Result := FoldLeft<Integer, Integer>(
function (A, B: Integer): Integer begin Result := A + B end,
0,
AList);
end;
class function List.Singleton<A>(const AValue: A): IList<A>;
begin
Result := Cons<A>(AValue, Empty<A>);
end;
class function List.SortAndGroupBy<A>(const AFunc: TFunc<A, A, Integer>;
const AList: IList<A>): IList<IList<A>>;
begin
Result := GroupBy<A>(AFunc, SortBy<A>(AFunc, AList));
end;
class function List.SortBy<A>(const AFunc: TFunc<A, A, Integer>;
const AList: IList<A>): IList<A>;
var
Xs: TList<A>;
begin
Xs := List.ToTList<A>(AList);
try
Xs.Sort(TFunComparer<A>.Create(AFunc));
Result := List.FromTList<A>(Xs);
finally
Xs.Free;
end;
end;
class function List.Sum(const AList: IList<Extended>): Extended;
begin
Result := FoldLeft<Extended, Extended>(
function (A, B: Extended): Extended begin Result := A + B end,
0.0,
AList);
end;
class function List.ToTList<A>(const AList: IList<A>): TList<A>;
begin
Result := FoldLeft<A, TList<A>>(
function(X: A; Acc: TList<A>): TList<A> begin
Acc.Add(X);
Result := Acc;
end,
TList<A>.Create,
AList);
end;
{ ConsCell<A> }
constructor ConsCell<A>.Create(const AHead: A; const ATail: IList<A>);
begin
inherited Create;
fHead := AHead;
fTail := ATail;
end;
destructor ConsCell<A>.Destroy;
var
Xs: IList<A>;
Ys: IList<A>;
begin
Xs := Tail;
while Xs is ConsCell<A> do begin
Ys := ConsCell<A>(Xs).Tail;
ConsCell<A>(Xs).fTail := nil;
Xs := Ys;
Ys := nil;
end;
inherited;
end;
function ConsCell<A>.GetHead: A;
begin
Result := fHead;
end;
function ConsCell<A>.GetTail: IList<A>;
begin
Result := fTail;
end;
{ TFunComparer<A> }
function TFunComparer<A>.Compare(const Left, Right: A): Integer;
begin
Result := fFunc(Left, Right);
end;
constructor TFunComparer<A>.Create(const AFunc: TFunc<A, A, Integer>);
begin
inherited Create;
fFunc := AFunc;
end;
end.