Skip to content

Commit befafac

Browse files
committed
Update docs
1 parent 72c3336 commit befafac

24 files changed

+1637
-1013
lines changed

doc/Collections.md

+149-57
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,24 @@
4141
Collection with O(1) `Seq::at()` and `Seq::__invoke()` operations.
4242

4343
``` php
44+
<?php
45+
46+
declare(strict_types=1);
47+
48+
use Fp\Collections\ArrayList;
49+
use Tests\Mock\Foo;
50+
4451
$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),
4756
]);
4857

4958
$collection
5059
->map(fn(Foo $elem) => $elem->a)
5160
->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
5462
```
5563

5664
# LinkedList
@@ -60,6 +68,12 @@ $collection
6068
Collection with O(1) prepend operation.
6169

6270
``` php
71+
<?php
72+
73+
declare(strict_types=1);
74+
75+
use Fp\Collections\LinkedList;
76+
6377
$collection = LinkedList::collect([
6478
new Foo(1), new Foo(2)
6579
new Foo(3), new Foo(4),
@@ -68,8 +82,7 @@ $collection = LinkedList::collect([
6882
$collection
6983
->map(fn(Foo $elem) => $elem->a)
7084
->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
7386
```
7487

7588
# HashMap
@@ -84,11 +97,18 @@ implement HashContract interface for your classes which objects will be
8497
used as keys in HashMap.
8598

8699
``` 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
88107
{
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+
) {}
92112

93113
public function equals(mixed $that): bool
94114
{
@@ -104,16 +124,18 @@ class Foo implements HashContract
104124
}
105125

106126
$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]
109131
]);
110132

111133
$collection(new Foo(2))->getOrElse(0); // 2
112134

113135
$collection
114136
->map(fn(int $value) => $value + 1)
115137
->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
117139
```
118140

119141
# HashSet
@@ -128,11 +150,16 @@ HashContract interface for your classes which objects will be used as
128150
elements in HashSet.
129151

130152
``` php
131-
class Foo implements HashContract
153+
<?php
154+
155+
use Fp\Collections\HashSet;
156+
157+
final class Foo implements HashContract
132158
{
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+
) {}
136163

137164
public function equals(mixed $that): bool
138165
{
@@ -148,15 +175,18 @@ class Foo implements HashContract
148175
}
149176

150177
$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),
153184
]);
154185

155186
$collection
156187
->map(fn(Foo $elem) => $elem->a)
157188
->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);
160190

161191
/**
162192
* Check if set contains given element
@@ -167,8 +197,12 @@ $collection(new Foo(2)); // true
167197
* Check if one set is contained in another set
168198
*/
169199
$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),
172206
])); // true
173207
```
174208

@@ -177,19 +211,33 @@ $collection->subsetOf(HashSet::collect([
177211
<!-- end list -->
178212

179213
``` php
180-
class Ceo
214+
<?php
215+
216+
declare(strict_types=1);
217+
218+
use Fp\Collections\HashSet;
219+
220+
final class Ceo
181221
{
182-
public function __construct(public string $name) { }
222+
public function __construct(
223+
public readonly string $name,
224+
) {}
183225
}
184226

185-
class Manager
227+
final class Manager
186228
{
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+
) {}
188233
}
189234

190-
class Developer
235+
final class Developer
191236
{
192-
public function __construct(public string $name, public Manager $manager) { }
237+
public function __construct(
238+
public string $name,
239+
public Manager $manager,
240+
) {}
193241
}
194242

195243
$ceo = new Ceo('CEO');
@@ -213,14 +261,23 @@ Collection with O(1) `NonEmptySeq::at()` and `NonEmptySeq::__invoke()`
213261
operations.
214262

215263
``` 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),
219276
]);
220277

221278
$collection
222279
->map(fn(Foo $elem) => $elem->a)
223-
->reduce(fn($acc, $elem) => $acc + $elem); // 10
280+
->fold(0)(fn($acc, $elem) => $acc + $elem); // 10
224281
```
225282

226283
# NonEmptyLinkedList
@@ -230,14 +287,23 @@ $collection
230287
Collection with O(1) prepend operation.
231288

232289
``` 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),
236302
]);
237303

238304
$collection
239305
->map(fn(Foo $elem) => $elem->a)
240-
->reduce(fn($acc, $elem) => $acc + $elem); // 10
306+
->fold(0)(fn($acc, $elem) => $acc + $elem);
241307
```
242308

243309
# NonEmptyHashMap
@@ -252,11 +318,18 @@ implement HashContract interface for your classes which objects will be
252318
used as keys in HashMap.
253319

254320
``` 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
256328
{
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+
) {}
260333

261334
public function equals(mixed $that): bool
262335
{
@@ -272,8 +345,10 @@ class Foo implements HashContract
272345
}
273346

274347
$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]
277352
]);
278353

279354
$collection(new Foo(2))->getOrElse(0); // 2
@@ -289,17 +364,24 @@ $collection
289364

290365
Collection of unique elements.
291366

292-
Object comparison by default uses spl\_object\_hash function. If you
367+
Object comparison by default uses `spl_object_hash` function. If you
293368
want to override default comparison behaviour then you need to implement
294369
HashContract interface for your classes which objects will be used as
295370
elements in HashSet.
296371

297372
``` 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
299380
{
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+
) {}
303385

304386
public function equals(mixed $that): bool
305387
{
@@ -314,15 +396,19 @@ class Foo implements HashContract
314396
}
315397
}
316398

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),
320406
]);
321407

322408
$collection
323409
->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+
326412
/**
327413
* Check if set contains given element
328414
*/
@@ -331,8 +417,14 @@ $collection(new Foo(2)); // true
331417
/**
332418
* Check if one set is contained in another set
333419
*/
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
338430
```

0 commit comments

Comments
 (0)