loophp /
collection
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace loophp\collection\Operation; |
||
| 6 | |||
| 7 | use Closure; |
||
| 8 | use Generator; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @immutable |
||
| 12 | * |
||
| 13 | * @template TKey |
||
| 14 | * @template T |
||
| 15 | */ |
||
| 16 | final class Dispersion extends AbstractOperation |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @return Closure(iterable<TKey, T>): Generator<int, float|int<0,1>> |
||
| 20 | */ |
||
| 21 | 4 | public function __invoke(): Closure |
|
| 22 | { |
||
| 23 | /** @var Closure(iterable<TKey, T>): Generator<int, float|int<0,1>> $pipe */ |
||
| 24 | 4 | $pipe = (new Pipe())()( |
|
| 25 | 4 | (new Normalize())(), |
|
| 26 | 4 | (new ScanLeft())()( |
|
| 27 | /** |
||
| 28 | * @param array<numeric-string|int<0,1>, T> $acc |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 29 | * @param T $value |
||
| 30 | * |
||
| 31 | * @return list{float|int<0,1>, T} |
||
| 32 | */ |
||
| 33 | 4 | static function (array $acc, mixed $value, int $key): array { |
|
| 34 | 3 | [$c, $v] = $acc; |
|
| 35 | |||
| 36 | 3 | return [(0 === $key) ? 0 : (($c * ($key - 1) + (($v === $value) ? 0 : 1)) / $key), $value]; |
|
|
0 ignored issues
–
show
|
|||
| 37 | 4 | } |
|
| 38 | 4 | )([0, null]), |
|
| 39 | 4 | (new Drop())()(1), |
|
| 40 | 4 | (new Map())()( |
|
| 41 | /** |
||
| 42 | * @param array<numeric-string|int<0,1>, T> $value |
||
|
0 ignored issues
–
show
|
|||
| 43 | * |
||
| 44 | * @return float|int<0,1> |
||
| 45 | */ |
||
| 46 | 4 | static fn (array $value): float|int => (0.0 === $value[0] || 1.0 === $value[0]) ? (int) $value[0] : $value[0] |
|
| 47 | 4 | ) |
|
| 48 | 4 | ); |
|
| 49 | |||
| 50 | 4 | return $pipe; |
|
| 51 | } |
||
| 52 | } |
||
| 53 |