41
41
Collection with O(1) ` Seq::at() ` and ` Seq::__invoke() ` operations.
42
42
43
43
``` php
44
+ <?php
45
+
46
+ declare(strict_types=1);
47
+
48
+ use Fp\Collections\ArrayList;
49
+ use Tests\Mock\Foo;
50
+
44
51
$collection = ArrayList::collect([
45
- new Foo(1), new Foo(2)
46
- new Foo(3), new Foo(4),
52
+ new Foo(1),
53
+ new Foo(2)
54
+ new Foo(3),
55
+ new Foo(4),
47
56
]);
48
57
49
58
$collection
50
59
->map(fn(Foo $elem) => $elem->a)
51
60
->filter(fn(int $elem) => $elem > 1)
52
- ->reduce(fn($acc, $elem) => $acc + $elem)
53
- ->getOrElse(0); // 9
61
+ ->fold(0)(fn($acc, $elem) => $acc + $elem); // 9
54
62
```
55
63
56
64
# LinkedList
@@ -60,6 +68,12 @@ $collection
60
68
Collection with O(1) prepend operation.
61
69
62
70
``` php
71
+ <?php
72
+
73
+ declare(strict_types=1);
74
+
75
+ use Fp\Collections\LinkedList;
76
+
63
77
$collection = LinkedList::collect([
64
78
new Foo(1), new Foo(2)
65
79
new Foo(3), new Foo(4),
@@ -68,8 +82,7 @@ $collection = LinkedList::collect([
68
82
$collection
69
83
->map(fn(Foo $elem) => $elem->a)
70
84
->filter(fn(int $elem) => $elem > 1)
71
- ->reduce(fn($acc, $elem) => $acc + $elem)
72
- ->getOrElse(0); // 9
85
+ ->fold(0)(fn($acc, $elem) => $acc + $elem); // 9
73
86
```
74
87
75
88
# HashMap
@@ -84,11 +97,18 @@ implement HashContract interface for your classes which objects will be
84
97
used as keys in HashMap.
85
98
86
99
``` php
87
- class Foo implements HashContract
100
+ <?php
101
+
102
+ declare(strict_types=1);
103
+
104
+ use Fp\Collections\HashMap;
105
+
106
+ final class Foo implements HashContract
88
107
{
89
- public function __construct(public int $a, public bool $b = true)
90
- {
91
- }
108
+ public function __construct(
109
+ public readonly int $a,
110
+ public readonly bool $b = true,
111
+ ) {}
92
112
93
113
public function equals(mixed $that): bool
94
114
{
@@ -104,16 +124,18 @@ class Foo implements HashContract
104
124
}
105
125
106
126
$collection = HashMap::collectPairs([
107
- [new Foo(1), 1], [new Foo(2), 2],
108
- [new Foo(3), 3], [new Foo(4), 4]
127
+ [new Foo(1), 1],
128
+ [new Foo(2), 2],
129
+ [new Foo(3), 3],
130
+ [new Foo(4), 4]
109
131
]);
110
132
111
133
$collection(new Foo(2))->getOrElse(0); // 2
112
134
113
135
$collection
114
136
->map(fn(int $value) => $value + 1)
115
137
->filter(fn(int $value) => $value > 2)
116
- ->fold(0, fn(int $acc, int $value) => $acc + $value); // 3+4+5=12
138
+ ->fold(0)( fn(int $acc, int $value) => $acc + $value); // 3+4+5=12
117
139
```
118
140
119
141
# HashSet
@@ -128,11 +150,16 @@ HashContract interface for your classes which objects will be used as
128
150
elements in HashSet.
129
151
130
152
``` php
131
- class Foo implements HashContract
153
+ <?php
154
+
155
+ use Fp\Collections\HashSet;
156
+
157
+ final class Foo implements HashContract
132
158
{
133
- public function __construct(public int $a, public bool $b = true)
134
- {
135
- }
159
+ public function __construct(
160
+ public readonly int $a,
161
+ public readonly bool $b = true,
162
+ ) {}
136
163
137
164
public function equals(mixed $that): bool
138
165
{
@@ -148,15 +175,18 @@ class Foo implements HashContract
148
175
}
149
176
150
177
$collection = HashSet::collect([
151
- new Foo(1), new Foo(2), new Foo(2),
152
- new Foo(3), new Foo(3), new Foo(4),
178
+ new Foo(1),
179
+ new Foo(2),
180
+ new Foo(2),
181
+ new Foo(3),
182
+ new Foo(3),
183
+ new Foo(4),
153
184
]);
154
185
155
186
$collection
156
187
->map(fn(Foo $elem) => $elem->a)
157
188
->filter(fn(int $elem) => $elem > 1)
158
- ->reduce(fn($acc, $elem) => $acc + $elem)
159
- ->getOrElse(0); // 9
189
+ ->fold(0)(fn($acc, $elem) => $acc + $elem);
160
190
161
191
/**
162
192
* Check if set contains given element
@@ -167,8 +197,12 @@ $collection(new Foo(2)); // true
167
197
* Check if one set is contained in another set
168
198
*/
169
199
$collection->subsetOf(HashSet::collect([
170
- new Foo(1), new Foo(2), new Foo(3),
171
- new Foo(4), new Foo(5), new Foo(6),
200
+ new Foo(1),
201
+ new Foo(2),
202
+ new Foo(3),
203
+ new Foo(4),
204
+ new Foo(5),
205
+ new Foo(6),
172
206
])); // true
173
207
```
174
208
@@ -177,19 +211,33 @@ $collection->subsetOf(HashSet::collect([
177
211
<!-- end list -->
178
212
179
213
``` php
180
- class Ceo
214
+ <?php
215
+
216
+ declare(strict_types=1);
217
+
218
+ use Fp\Collections\HashSet;
219
+
220
+ final class Ceo
181
221
{
182
- public function __construct(public string $name) { }
222
+ public function __construct(
223
+ public readonly string $name,
224
+ ) {}
183
225
}
184
226
185
- class Manager
227
+ final class Manager
186
228
{
187
- public function __construct(public string $name, public Ceo $ceo) { }
229
+ public function __construct(
230
+ public readonly string $name,
231
+ public readonly Ceo $ceo,
232
+ ) {}
188
233
}
189
234
190
- class Developer
235
+ final class Developer
191
236
{
192
- public function __construct(public string $name, public Manager $manager) { }
237
+ public function __construct(
238
+ public string $name,
239
+ public Manager $manager,
240
+ ) {}
193
241
}
194
242
195
243
$ceo = new Ceo('CEO');
@@ -213,14 +261,23 @@ Collection with O(1) `NonEmptySeq::at()` and `NonEmptySeq::__invoke()`
213
261
operations.
214
262
215
263
``` php
216
- $collection = NonEmptyArrayList::collect([
217
- new Foo(1), new Foo(2)
218
- new Foo(3), new Foo(4),
264
+ <?php
265
+
266
+ declare(strict_types=1);
267
+
268
+ use Tests\Mock\Foo;
269
+ use Fp\Collections\NonEmptyArrayList;
270
+
271
+ $collection = NonEmptyArrayList::collectNonEmpty([
272
+ new Foo(1),
273
+ new Foo(2)
274
+ new Foo(3),
275
+ new Foo(4),
219
276
]);
220
277
221
278
$collection
222
279
->map(fn(Foo $elem) => $elem->a)
223
- ->reduce (fn($acc, $elem) => $acc + $elem); // 10
280
+ ->fold(0) (fn($acc, $elem) => $acc + $elem); // 10
224
281
```
225
282
226
283
# NonEmptyLinkedList
@@ -230,14 +287,23 @@ $collection
230
287
Collection with O(1) prepend operation.
231
288
232
289
``` php
233
- $collection = NonEmptyLinkedList::collect([
234
- new Foo(1), new Foo(2)
235
- new Foo(3), new Foo(4),
290
+ <?php
291
+
292
+ declare(strict_types=1);
293
+
294
+ use Tests\Mock\Foo;
295
+ use Fp\Collections\NonEmptyLinkedList;
296
+
297
+ $collection = NonEmptyLinkedList::collectNonEmpty([
298
+ new Foo(1),
299
+ new Foo(2)
300
+ new Foo(3),
301
+ new Foo(4),
236
302
]);
237
303
238
304
$collection
239
305
->map(fn(Foo $elem) => $elem->a)
240
- ->reduce( fn($acc, $elem) => $acc + $elem); // 10
306
+ ->fold(0)( fn($acc, $elem) => $acc + $elem);
241
307
```
242
308
243
309
# NonEmptyHashMap
@@ -252,11 +318,18 @@ implement HashContract interface for your classes which objects will be
252
318
used as keys in HashMap.
253
319
254
320
``` php
255
- class Foo implements HashContract
321
+ <?php
322
+
323
+ declare(strict_types=1);
324
+
325
+ use Fp\Collections\NonEmptyHashMap;
326
+
327
+ final class Foo implements HashContract
256
328
{
257
- public function __construct(public int $a, public bool $b = true)
258
- {
259
- }
329
+ public function __construct(
330
+ public readonly int $a,
331
+ public readonly bool $b = true,
332
+ ) {}
260
333
261
334
public function equals(mixed $that): bool
262
335
{
@@ -272,8 +345,10 @@ class Foo implements HashContract
272
345
}
273
346
274
347
$collection = NonEmptyHashMap::collectPairsNonEmpty([
275
- [new Foo(1), 1], [new Foo(2), 2],
276
- [new Foo(3), 3], [new Foo(4), 4]
348
+ [new Foo(1), 1],
349
+ [new Foo(2), 2],
350
+ [new Foo(3), 3],
351
+ [new Foo(4), 4]
277
352
]);
278
353
279
354
$collection(new Foo(2))->getOrElse(0); // 2
@@ -289,17 +364,24 @@ $collection
289
364
290
365
Collection of unique elements.
291
366
292
- Object comparison by default uses spl \_ object \_ hash function. If you
367
+ Object comparison by default uses ` spl_object_hash ` function. If you
293
368
want to override default comparison behaviour then you need to implement
294
369
HashContract interface for your classes which objects will be used as
295
370
elements in HashSet.
296
371
297
372
``` php
298
- class Foo implements HashContract
373
+ <?php
374
+
375
+ declare(strict_types=1);
376
+
377
+ use Fp\Collections\NonEmptyHashSet;
378
+
379
+ final class Foo implements HashContract
299
380
{
300
- public function __construct(public int $a, public bool $b = true)
301
- {
302
- }
381
+ public function __construct(
382
+ public readonly int $a,
383
+ public readonly bool $b = true,
384
+ ) {}
303
385
304
386
public function equals(mixed $that): bool
305
387
{
@@ -314,15 +396,19 @@ class Foo implements HashContract
314
396
}
315
397
}
316
398
317
- $collection = NonEmptyHashSet::collect([
318
- new Foo(1), new Foo(2), new Foo(2),
319
- new Foo(3), new Foo(3), new Foo(4),
399
+ $collection = NonEmptyHashSet::collectNonEmpty([
400
+ new Foo(1),
401
+ new Foo(2),
402
+ new Foo(2),
403
+ new Foo(3),
404
+ new Foo(3),
405
+ new Foo(4),
320
406
]);
321
407
322
408
$collection
323
409
->map(fn(Foo $elem) => $elem->a)
324
- ->reduce (fn($acc, $elem) => $acc + $elem); // 10
325
-
410
+ ->fold(0) (fn($acc, $elem) => $acc + $elem); // 10
411
+
326
412
/**
327
413
* Check if set contains given element
328
414
*/
@@ -331,8 +417,14 @@ $collection(new Foo(2)); // true
331
417
/**
332
418
* Check if one set is contained in another set
333
419
*/
334
- $collection->subsetOf(NonEmptyHashSet::collect([
335
- new Foo(1), new Foo(2), new Foo(3),
336
- new Foo(4), new Foo(5), new Foo(6),
337
- ])); // true
420
+ $collection->subsetOf(
421
+ NonEmptyHashSet::collectNonEmpty([
422
+ new Foo(1),
423
+ new Foo(2),
424
+ new Foo(3),
425
+ new Foo(4),
426
+ new Foo(5),
427
+ new Foo(6),
428
+ ]),
429
+ ); // true
338
430
```
0 commit comments