-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleTypeHint.php
More file actions
57 lines (42 loc) · 1.19 KB
/
ExampleTypeHint.php
File metadata and controls
57 lines (42 loc) · 1.19 KB
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
54
55
56
57
<?hh // strict
// Example name: typehint
class ExampleTypeHint implements ExampleInterface
{
public function __construct() {}
public function run(): void
{
$this->sumReduction();
$this->stringReduction();
}
private function sumReduction(): void
{
echo "Testing sum reduction\n";
$sumReduction = function (int $currentElement, int $carry): int {
return $carry + $currentElement;
};
$reducer = $this->makeReducer($sumReduction);
$aVector = Vector {1, 2, 3};
var_dump($reducer($aVector, 0));
}
private function stringReduction(): void
{
echo "Testing string reduction\n";
$stringReduction = function (int $currentElement, string $carry): string {
return $currentElement . $carry;
};
$reducer = $this->makeReducer($stringReduction);
$aVector = Vector {1, 2, 3};
var_dump($reducer($aVector, ''));
}
private function makeReducer<Te, Tv>((function (Te, Tv): Tv) $singleStep):
(function (Traversable<Te>, Tv): Tv)
{
return function (Traversable<Te> $elements, Tv $initial): Tv use ($singleStep) {
$currentValue = $initial;
foreach ($elements as $element) {
$currentValue = $singleStep($element, $currentValue);
}
return $currentValue;
};
}
}