Skip to content

Commit 26f8535

Browse files
committed
Build doc
1 parent f8637bd commit 26f8535

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

doc/Combinators.md

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
- [Omit values from tuple or shape](#Omit-values-from-tuple-or-shape)
66
- [Ctor function](#Ctor-function)
77
- [Caveats](#Caveats)
8+
- [\*KV combinators](#\*KV-combinators)
9+
- [Map](#Map)
10+
- [Functions](#Functions)
811

912
# \*N combinators
1013

@@ -35,6 +38,7 @@ use function Fp\Collection\sequenceOptionT;
3538
use function Fp\Evidence\proveArray;
3639
use function Fp\Evidence\proveBool;
3740
use function Fp\Evidence\proveInt;
41+
use function Fp\Json\jsonDecode;
3842

3943
$json = <<<JSON
4044
{
@@ -49,7 +53,7 @@ JSON;
4953
*/
5054
function fooFromJson(string $json): Option
5155
{
52-
return Option::try(fn(): mixed => json_decode($json, associative: true, flags: JSON_THROW_ON_ERROR))
56+
return jsonDecode($json)->toOption()
5357
->flatMap(proveArray(...))
5458
->flatMap(fn(array $data) => sequenceOptionT(
5559
fn() => at($data, 'a')->flatMap(proveInt(...)),
@@ -200,3 +204,71 @@ test(...['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]);
200204

201205
So `ReflectionFunction` used for filtering extra arguments before array
202206
will be spread.
207+
208+
# \*KV combinators
209+
210+
- #### Map
211+
212+
Before v5 `Fp\Collections\Map` used `Fp\Collections\Entry` to represents
213+
kv pair. It was unfriendly for ide (lack autocompletion ability).
214+
215+
Since v5 `Fp\Collections\Entry` has been removed. Instead, each method
216+
of `Fp\Collections\Map` has \*KV version:
217+
218+
``` php
219+
<?php
220+
221+
use Fp\Collections\HashMap;
222+
223+
/**
224+
* @param HashMap<int, int> $hashMap
225+
* @return HashMap<int, int>
226+
*/
227+
function addOne(HashMap $hashMap): HashMap
228+
{
229+
return $hashMap->map(fn(int $value) => $value + 2);
230+
}
231+
232+
/**
233+
* @param HashMap<int, int> $hashMap
234+
* @return HashMap<int, int>
235+
*/
236+
function sumWithKeys(HashMap $hashMap): HashMap
237+
{
238+
return $hashMap->mapKV(fn(int $key, int $value) => $key + $value);
239+
}
240+
```
241+
242+
This makes sense since the key and value are rarely needed at the same
243+
time.
244+
245+
- #### Functions
246+
247+
Regular functions has \*KV combinators too:
248+
249+
``` php
250+
<?php
251+
252+
use Fp\Collections\HashMap;
253+
254+
use function Fp\Collection\map;
255+
use function Fp\Collection\mapKV;
256+
257+
/**
258+
* @param array<int, int> $hashMap
259+
* @return array<int, int>
260+
*/
261+
function addOne(HashMap $hashMap): HashMap
262+
{
263+
return map($hashMap, fn(int $value) => $value + 2);
264+
}
265+
266+
/**
267+
* @param array<int, int> $hashMap
268+
* @return array<int, int>
269+
*/
270+
function sumWithKeys(array $hashMap): HashMap
271+
{
272+
return mapKV($hashMap, fn(int $key, int $value) => $key + $value);
273+
}
274+
```

0 commit comments

Comments
 (0)