1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace loophp\collection\Operation; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Generator; |
9
|
|
|
|
10
|
|
|
use const INF; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @immutable |
14
|
|
|
* |
15
|
|
|
* @template TKey |
16
|
|
|
* @template T |
17
|
|
|
*/ |
18
|
|
|
final class Scale extends AbstractOperation |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @return Closure(float): Closure(float): Closure(float): Closure(float): Closure(float): Closure(iterable<TKey, float|int>): Generator<TKey, float|int> |
22
|
|
|
*/ |
23
|
2 |
|
public function __invoke(): Closure |
24
|
|
|
{ |
25
|
2 |
|
return |
26
|
|
|
/** |
27
|
|
|
* @return Closure(float): Closure(float): Closure(float): Closure(float): Closure(iterable<TKey, float|int>): Generator<TKey, float|int> |
28
|
|
|
*/ |
29
|
2 |
|
static fn (float $lowerBound): Closure => |
30
|
|
|
/** |
31
|
|
|
* @return Closure(float): Closure(float): Closure(float): Closure(iterable<TKey, float|int>): Generator<TKey, float|int> |
32
|
|
|
*/ |
33
|
2 |
|
static fn (float $upperBound): Closure => |
34
|
|
|
/** |
35
|
|
|
* @return Closure(float): Closure(float): Closure(iterable<TKey, float|int>): Generator<TKey, float|int> |
36
|
|
|
*/ |
37
|
2 |
|
static fn (float $wantedLowerBound = 0.0): Closure => |
38
|
|
|
/** |
39
|
|
|
* @return Closure(float): Closure(iterable<TKey, float|int>): Generator<TKey, float|int> |
40
|
|
|
*/ |
41
|
2 |
|
static fn (float $wantedUpperBound = 1.0): Closure => |
42
|
|
|
/** |
43
|
|
|
* @return Closure(iterable<TKey, float|int>): Generator<TKey, float|int> |
44
|
|
|
*/ |
45
|
2 |
|
static function (float $base = 0.0) use ($lowerBound, $upperBound, $wantedLowerBound, $wantedUpperBound): Closure { |
46
|
2 |
|
$wantedLowerBound = (0.0 === $wantedLowerBound) ? (0.0 === $base ? 0.0 : 1.0) : $wantedLowerBound; |
47
|
2 |
|
$wantedUpperBound = (1.0 === $wantedUpperBound) ? (0.0 === $base ? 1.0 : $base) : $wantedUpperBound; |
48
|
|
|
|
49
|
2 |
|
$mapper = (new Map())()( |
50
|
2 |
|
static function (float|int $v) use ($lowerBound, $upperBound, $wantedLowerBound, $wantedUpperBound, $base): float { |
51
|
2 |
|
$mx = 0.0 === $base ? |
52
|
2 |
|
($v - $lowerBound) / ($upperBound - $lowerBound) : |
53
|
|
|
log($v - $lowerBound, $base) / log($upperBound - $lowerBound, $base); |
54
|
|
|
|
55
|
2 |
|
$mx = $mx === -INF ? 0 : $mx; |
56
|
|
|
|
57
|
2 |
|
return $wantedLowerBound + $mx * ($wantedUpperBound - $wantedLowerBound); |
58
|
2 |
|
} |
59
|
2 |
|
); |
60
|
|
|
|
61
|
2 |
|
$filter = (new Filter())()( |
62
|
2 |
|
static fn (float|int $item): bool => $item > $lowerBound, |
63
|
2 |
|
static fn (float|int $item): bool => $item <= $upperBound |
64
|
2 |
|
); |
65
|
|
|
|
66
|
|
|
/** @var Closure(iterable<TKey, (float|int)>):(Generator<TKey, float|int>) $pipe */ |
67
|
2 |
|
$pipe = (new Pipe())()($filter, $mapper); |
68
|
|
|
|
69
|
|
|
// Point free style. |
70
|
2 |
|
return $pipe; |
71
|
2 |
|
}; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|