This repository was archived by the owner on Apr 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFSharpList.php
More file actions
489 lines (410 loc) · 10.5 KB
/
Copy pathFSharpList.php
File metadata and controls
489 lines (410 loc) · 10.5 KB
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
<?php
abstract class FSharpList implements IteratorAggregate, iComparable {
abstract function isEmpty();
function __debugInfo() {
return FSharpList::toArray($this);
}
static function length(FSharpList $list) {
$len = 0;
while($list instanceof Cons)
{
$len++;
$list = $list->next;
}
return $len;
}
static function ofArray($a) {
$list = NULL;
$p = &$list;
foreach ($a as $item) {
$p = new Cons($item, $list);
$p = &$p->next;
}
$p = FSharpList::get_Nil();
return $list;
}
static function toArray($list) {
$array = [];
while($list instanceof Cons)
{
$array[] = $list->value;
$list = $list->next;
}
return $array;
}
static function ofSeq($seq) {
$list = NULL;
$p = &$list;
foreach ($seq as $item) {
$p = new Cons($item, $list);
$p = &$p->next;
}
$p = FSharpList::get_Nil();
return $list;
}
static function toSeq($list) {
return $list;
}
static function contains($item, $list, $comparer)
{
$eq = $comparer['Equals'];
while($list instanceof Cons)
{
if ($eq($item, $list->value))
{
return true;
}
$list = $list->next;
}
return false;
}
static function tryFindIndex($filter, $list)
{
$i = 0;
while ($list instanceof Cons)
{
$v = $filter($list->value);
if ($v)
{
return $i;
}
$i++;
$list = $list->next;
}
return NULL;
}
static function tryFind($filter, $list)
{
while ($list instanceof Cons)
{
$v = $filter($list->value);
if ($v)
{
return $list->value;
}
$list = $list->next;
}
return NULL;
}
static function skip($count, $list)
{
while ($count-- > 0 && $list instanceof Cons)
{
$list = $list->next;
}
return $list;
}
static function reverse($list)
{
$result = FSharpList::get_Nil();
while ($list instanceof Cons)
{
$result = new Cons($list->value, $result);
$list = $list->next;
}
return $result;
}
static function splitAt($i, $list)
{
$left = NULL;
$p = &$left;
while ($i-- > 0 && $list instanceof Cons)
{
$p = new Cons($list->value, $left);
$p = &$p->next;
$list = $list->next;
}
$p = FSharpList::get_Nil();
return [ $left, $list];
}
static function append($left,$right)
{
$result = NULL;
$p = &$result;
while ($left instanceof Cons)
{
$p = new Cons($left->value, NULL);
$p = &$p->next;
$left = $left->next;
}
$p = $right;
return $result;
}
static function head($list)
{
return $list->value;
}
static function tail($list)
{
return $list->next;
}
static function last($list)
{
$value = NULL;
while($list instanceof Cons)
{
$value = $list->value;
$list = $list->next;
}
return $value;
}
static function truncate($count, $list)
{
$lst = NULL;
$p = &$lst;
while ($list instanceof Cons && $count-- > 0)
{
$p = new Cons($list->value, NULL);
$p = &$p->next;
$list = $list->next;
}
$p = FSharpList::get_Nil();
return $lst;
}
static function map($projection, $list)
{
$lst = NULL;
$p = &$lst;
while ($list instanceof Cons)
{
$p = new Cons($projection($list->value), NULL);
$p = &$p->next;
$list = $list->next;
}
$p = FSharpList::get_Nil();
return $lst;
}
static function choose($projection, $list)
{
$lst = NULL;
$p = &$lst;
while ($list instanceof Cons)
{
$v = $projection($list->value);
if (!is_null($v))
{
$p = new Cons($v, NULL);
$p = &$p->next;
}
$list = $list->next;
}
$p = FSharpList::get_Nil();
return $lst;
}
static function filter($predicate, $list)
{
$lst = NULL;
$p = &$lst;
while ($list instanceof Cons)
{
if ($predicate($list->value))
{
$p = new Cons($list->value, NULL);
$p = &$p->next;
}
$list = $list->next;
}
$p = FSharpList::get_Nil();
return $lst;
}
static function fold($aggregator, $state, $list)
{
while ($list instanceof Cons)
{
$state = $aggregator($state,$list->value);
$list = $list->next;
}
return $state;
}
static function mapFold($aggregator, $state, $list)
{
$lst = NULL;
$p = &$lst;
while ($list instanceof Cons)
{
$v = $aggregator($state,$list->value);
$p = new Cons($v[0], NULL);
$p = &$p->next;
$state = $v[1];
$list = $list->next;
}
$p = FSharpList::get_Nil();
return [$lst,$state];
}
static function sortBy($projection, $list)
{
$array = [];
while ($list instanceof Cons)
{
$array[] = [ $list->value, $projection($list->value) ];
$list = $list->next;
}
usort($array, function($x,$y) { return Util::compare($x[1],$y[1]); } );
$list = NULL;
$p = &$list;
foreach ($array as $item) {
$p = new Cons($item[0], $list);
$p = &$p->next;
}
$p = FSharpList::get_Nil();
return $list;
return FSharpList::ofArray($array);
}
static function collect($projection, $list)
{
return FSharpList::ofSeq(Seq::collect($projection, $list));
}
static function groupBy($property, $list)
{
$comparer = [ 'Compare' => 'Util::compare' ];
$map = Map::empty($comparer);
while($list instanceof Cons)
{
$key = $property($list->value);
$items = Map::tryFind($key, $map) ?? [];
array_push($items, $list->value);
$map = Map::add($key, $items, $map);
$list = $list->next;
}
$lst = NULL;
$p = &$lst;
foreach (Map::toSeq($map) as $kv)
{
$p = new Cons([ $kv[0], FSharpList::ofArray($kv[1])], NULL);
$p = &$p->next;
}
$p = FSharpList::get_Nil();
return $lst;
}
static function chunkBySize($size, $list)
{
$lst = NULL;
$p = &$lst;
$chunk = NULL;
$pc = &$chunk;
$c = $size;
while ($list instanceof Cons)
{
$pc = new Cons($list->value, NULL);
$pc = &$pc->next;
$list = $list->next;
if (--$c == 0)
{
$pc = FSharpList::get_Nil();
$p = new Cons($chunk, NULL);
$p = &$p->next;
$chunk = NULL;
$pc = &$chunk;
$c = $size;
}
}
if ($chunk instanceof Cons)
{
$pc = FSharpList::get_Nil();
$p = new Cons($chunk, NULL);
$p = &$p->next;
}
$p = FSharpList::get_Nil();
return $lst;
}
static function sumBy($property, $list)
{
$sum = 0;
while ($list instanceof Cons)
{
$sum += $property($list->value);
$list = $list->next;
}
return $sum;
}
static function maxBy($property, $list, $comparerArray)
{
$max = NULL;
$maxVal = NULL;
$comparer = $comparerArray['Compare'];
while($list instanceof Cons)
{
$prop = $property($list->value);
if (is_null($max) || $comparer($prop,$max) > 0)
{
$max = $prop;
$maxVal = $list->value;
}
$list = $list->next;
}
return $maxVal;
}
static function forAll($predicate, $list)
{
while($list instanceof Cons)
{
if (!$predicate($list->value))
return false;
$list = $list->next;
}
return true;
}
static function exists($predicate, $list)
{
while($list instanceof Cons)
{
if ($predicate($list->value))
return true;
$list = $list->next;
}
return false;
}
static function map2($projection, $list1, $list2)
{
$lst = NULL;
$p = &$lst;
while($list1 instanceof Cons and $list2 instanceof Cons)
{
$p = new Cons($projection($list1->value, $list2->value), NULL);
$p = &$p->next;
$list1 = $list1->next;
$list2 = $list2->next;
}
$p = FSharpList::get_Nil();
return $lst;
}
public function CompareTo($other)
{
if ($this instanceof Nil)
return $other instanceof Nil ? 0 : -1;
if ($other instanceof Nil)
return 1;
$c = Util::compare($this->value,$other->value);
if ($c != 0)
return $c;
return $this->next->CompareTo($other->next);
}
public function getIterator() {
$list = $this;
while ($list instanceof Cons)
{
yield $list->value;
$list = $list->next;
}
}
static public function get_Nil() {
static $nil;
if ($nil === null) {
$nil = new Nil();
}
return $nil;
}
}
class Nil extends FSharpList {
public function __construct() {}
function isEmpty() { return true; }
}
class Cons extends FSharpList {
public $value;
public $next;
function __construct($value, FSharpList $next = NULL) {
$this->value = $value;
$this->next = $next;
}
function isEmpty() { return false; }
}