Skip to content

Commit 6b7287d

Browse files
committed
Merge branch 'v5'
2 parents 615821b + 26f8535 commit 6b7287d

File tree

3 files changed

+167
-1
lines changed

3 files changed

+167
-1
lines changed

UPGRADING-v5.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
1+
## Remove psalm-pure and psalm-immutable
2+
3+
Before v5 all data structures and most function was pure.
4+
Since v5 `@psalm-pure` and `@psalm-immutable` has been removed.
5+
It has many false positives, and it's to difficult use in the real applications.
6+
7+
Some examples with useful code (but with false positives):
8+
- https://psalm.dev/r/e58c340477
9+
- https://psalm.dev/r/de37ebb8ed
10+
- https://psalm.dev/r/627b50d716
11+
12+
Discussion of the problem: https://github.com/vimeo/psalm/issues/8116
13+
14+
This code is invalid since v5:
15+
```php
16+
/**
17+
* @param ArrayList<int> $list
18+
* @return ArrayList<int>
19+
* @psalm-pure
20+
*/
21+
function pureFn(ArrayList $list): ArrayList
22+
{
23+
// ERROR: ImpureMethodCall
24+
return $list->map(fn($i) => $i + 2);
25+
}
26+
```
27+
28+
Removing `@psalm-pure` from the `pureFn` fix that problem.
29+
130
## Functions BC
231

332
- All collection functions with `$callback`/`$predicate` params does not allow key as second parameter anymore.

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+
```
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# *KV combinators
2+
3+
- #### Map
4+
5+
Before v5 `Fp\Collections\Map` used `Fp\Collections\Entry` to represents kv pair.
6+
It was unfriendly for ide (lack autocompletion ability).
7+
8+
Since v5 `Fp\Collections\Entry` has been removed. Instead, each method of `Fp\Collections\Map` has *KV version:
9+
10+
```php
11+
<?php
12+
13+
use Fp\Collections\HashMap;
14+
15+
/**
16+
* @param HashMap<int, int> $hashMap
17+
* @return HashMap<int, int>
18+
*/
19+
function addOne(HashMap $hashMap): HashMap
20+
{
21+
return $hashMap->map(fn(int $value) => $value + 2);
22+
}
23+
24+
/**
25+
* @param HashMap<int, int> $hashMap
26+
* @return HashMap<int, int>
27+
*/
28+
function sumWithKeys(HashMap $hashMap): HashMap
29+
{
30+
return $hashMap->mapKV(fn(int $key, int $value) => $key + $value);
31+
}
32+
```
33+
34+
This makes sense since the key and value are rarely needed at the same time.
35+
36+
- #### Functions
37+
38+
Regular functions has *KV combinators too:
39+
40+
```php
41+
<?php
42+
43+
use Fp\Collections\HashMap;
44+
45+
use function Fp\Collection\map;
46+
use function Fp\Collection\mapKV;
47+
48+
/**
49+
* @param array<int, int> $hashMap
50+
* @return array<int, int>
51+
*/
52+
function addOne(HashMap $hashMap): HashMap
53+
{
54+
return map($hashMap, fn(int $value) => $value + 2);
55+
}
56+
57+
/**
58+
* @param array<int, int> $hashMap
59+
* @return array<int, int>
60+
*/
61+
function sumWithKeys(array $hashMap): HashMap
62+
{
63+
return mapKV($hashMap, fn(int $key, int $value) => $key + $value);
64+
}
65+
```

0 commit comments

Comments
 (0)