<?php
// Everything you enter here will be executed by our servers. Try it!
function iterator_compose()
{
foreach (array_reverse(func_get_args()) as $it) {
$prev = null;
if (is_callable($it)) {
$prev = $it($prev);
} else if (is_object($it)) {
$prev = $it;
} else {
$prev = new $it($prev);
}
}
return $prev;
}
function cycle($it)
{
$saved = [];
foreach ($it as $key => $val) {
$saved[] = $val;
yield $val;
}
while ($saved) {
foreach ($saved as $val) {
yield $val;
}
}
}
function limit($it, $count, $offset = 0)
{
return new LimitIterator($it, $offset, $count);
}
function iterator_chunk(\Traversable $it, $chunkSize)
{
$it->rewind();
$chunk = [];
$steps = range(0, $chunkSize);
while (true) {
foreach ($steps as $_) {
if (!$it->valid()) {
break 2;
}
$chunk[] = $it->current();
$it->next();
}
yield $chunk;
$chunk = [];
}
if ($chunk) {
yield $chunk;
}
}
$it = iterator_chunk(
new ArrayIterator(range(0, 125))
, 32);
$colors = new ArrayIterator(['red', 'green', 'blue']);
foreach (limit(cycle($colors), 10) as $color) {
echo "$color\n";
}
foreach ($it as $i => $c) {
echo "====== Chunk $i ======\n";
var_export($c);
echo "\n";
}
- Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- red
green
blue
red
green
blue
red
green
blue
red
====== Chunk 0 ======
array (
0 => 0,
1 => 1,
2 => 2,
3 => 3,
4 => 4,
5 => 5,
6 => 6,
7 => 7,
8 => 8,
9 => 9,
10 => 10,
11 => 11,
12 => 12,
13 => 13,
14 => 14,
15 => 15,
16 => 16,
17 => 17,
18 => 18,
19 => 19,
20 => 20,
21 => 21,
22 => 22,
23 => 23,
24 => 24,
25 => 25,
26 => 26,
27 => 27,
28 => 28,
29 => 29,
30 => 30,
31 => 31,
32 => 32,
)
====== Chunk 1 ======
array (
0 => 33,
1 => 34,
2 => 35,
3 => 36,
4 => 37,
5 => 38,
6 => 39,
7 => 40,
8 => 41,
9 => 42,
10 => 43,
11 => 44,
12 => 45,
13 => 46,
14 => 47,
15 => 48,
16 => 49,
17 => 50,
18 => 51,
19 => 52,
20 => 53,
21 => 54,
22 => 55,
23 => 56,
24 => 57,
25 => 58,
26 => 59,
27 => 60,
28 => 61,
29 => 62,
30 => 63,
31 => 64,
32 => 65,
)
====== Chunk 2 ======
array (
0 => 66,
1 => 67,
2 => 68,
3 => 69,
4 => 70,
5 => 71,
6 => 72,
7 => 73,
8 => 74,
9 => 75,
10 => 76,
11 => 77,
12 => 78,
13 => 79,
14 => 80,
15 => 81,
16 => 82,
17 => 83,
18 => 84,
19 => 85,
20 => 86,
21 => 87,
22 => 88,
23 => 89,
24 => 90,
25 => 91,
26 => 92,
27 => 93,
28 => 94,
29 => 95,
30 => 96,
31 => 97,
32 => 98,
)
====== Chunk 3 ======
array (
0 => 99,
1 => 100,
2 => 101,
3 => 102,
4 => 103,
5 => 104,
6 => 105,
7 => 106,
8 => 107,
9 => 108,
10 => 109,
11 => 110,
12 => 111,
13 => 112,
14 => 113,
15 => 114,
16 => 115,
17 => 116,
18 => 117,
19 => 118,
20 => 119,
21 => 120,
22 => 121,
23 => 122,
24 => 123,
25 => 124,
26 => 125,
)
- Output for 5.4.0 - 5.4.45
- Parse error: syntax error, unexpected '$val' (T_VARIABLE) in /in/leTP7 on line 26
Process exited with code 255. - Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
- Parse error: syntax error, unexpected '[' in /in/leTP7 on line 22
Process exited with code 255. - Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
- Parse error: parse error, unexpected '[' in /in/leTP7 on line 22
Process exited with code 255. - Output for 4.3.2 - 4.3.4
- Parse error: parse error in /in/leTP7 on line 22
Process exited with code 255.
preferences:
286 ms | 401 KiB | 457 Q