|
5 | 5 | - [Omit values from tuple or shape](#Omit-values-from-tuple-or-shape) |
6 | 6 | - [Ctor function](#Ctor-function) |
7 | 7 | - [Caveats](#Caveats) |
| 8 | +- [\*KV combinators](#\*KV-combinators) |
| 9 | + - [Map](#Map) |
| 10 | + - [Functions](#Functions) |
8 | 11 |
|
9 | 12 | # \*N combinators |
10 | 13 |
|
@@ -35,6 +38,7 @@ use function Fp\Collection\sequenceOptionT; |
35 | 38 | use function Fp\Evidence\proveArray; |
36 | 39 | use function Fp\Evidence\proveBool; |
37 | 40 | use function Fp\Evidence\proveInt; |
| 41 | +use function Fp\Json\jsonDecode; |
38 | 42 |
|
39 | 43 | $json = <<<JSON |
40 | 44 | { |
|
49 | 53 | */ |
50 | 54 | function fooFromJson(string $json): Option |
51 | 55 | { |
52 | | - return Option::try(fn(): mixed => json_decode($json, associative: true, flags: JSON_THROW_ON_ERROR)) |
| 56 | + return jsonDecode($json)->toOption() |
53 | 57 | ->flatMap(proveArray(...)) |
54 | 58 | ->flatMap(fn(array $data) => sequenceOptionT( |
55 | 59 | fn() => at($data, 'a')->flatMap(proveInt(...)), |
@@ -200,3 +204,71 @@ test(...['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]); |
200 | 204 |
|
201 | 205 | So `ReflectionFunction` used for filtering extra arguments before array |
202 | 206 | 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