-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSample.php
More file actions
69 lines (56 loc) · 1.8 KB
/
Sample.php
File metadata and controls
69 lines (56 loc) · 1.8 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
58
59
60
61
62
63
64
65
66
67
68
69
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Newnakashima\TypedArray\TypedArray;
use Newnakashima\TypedArray\Primitives;
class Example1
{
public int $value;
public function __construct(int $value)
{
$this->value = $value;
}
}
class Example2
{
public int $value;
public function __construct(int $value)
{
$this->value = $value;
}
}
class Sample
{
public function handle()
{
$length = 10000;
$list = new TypedArray(Primitives::Int->value, range(1, $length));
$empty = new TypedArray('int');
foreach ($list as $item) {
$empty->add($item * 2);
}
assert($empty->count() === $length);
$mapped = $list->map(fn ($item) => $item * 2);
assert($mapped[$length - 1], $length * 2);
$filtered = $list->filter(fn ($item) => $item % 2 === 0);
assert($filtered->count() === $length / 2);
$filtered->each(fn ($item) => assert($item % 2 === 0));
$exampleArray = new TypedArray(Example1::class, [
new Example1(1),
new Example1(2),
]);
// This causes an error because the type is not matched.
// $exampleArray->add(new Example2(3));
// This is OK because the type is matched.
$exampleArray->add(new Example1(3));
// This also is OK. TypedArray will try to initialize the object with the given value.
// Within add() method, Example1::__construct(3) is called.
$exampleArray->add(3);
assert($exampleArray[2]->value === 3);
assert($exampleArray[2] instanceof Example1);
}
}
$start = microtime(true);
(new Sample())->handle();
$elapsed = microtime(true) - $start;
echo "elapsed: {$elapsed} sec\n";
echo "memory: " . memory_get_peak_usage(true) / 1024 / 1024 . " MB\n";