-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Compact.php
53 lines (46 loc) · 1.5 KB
/
Compact.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
declare(strict_types=1);
namespace loophp\collection\Operation;
use Closure;
use Generator;
use Iterator;
use function in_array;
/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*/
final class Compact extends AbstractOperation
{
/**
* @psalm-return Closure(T...): Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param T ...$values
*/
static function (...$values): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($values): Generator {
$values = [] === $values ? [null] : $values;
/** @psalm-var callable(Iterator<TKey, T>):Generator<TKey, T> $filter */
$filter = Filter::of()(
/**
* @param mixed $value
* @psalm-param T $value
*/
static function ($value) use ($values): bool {
return !in_array($value, $values, true);
}
);
return yield from $filter($iterator);
};
};
}
}