3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php // Everything you enter here will be executed by our servers. Try it! function iterator_compose(\Traversable $source, $steps) { $gen = $source; foreach ($steps as $step) { if (is_array($step) and !is_callable($step)) { $g = array_shift($step); array_unshift($step, $gen); if (class_exists($g)) { $class = new \ReflectionClass($className); $gen = $class->newInstanceArgs($step); } else if (is_callable($g)) { $gen = call_user_func_array($g, $step); } } else if (is_callable($step)) { $gen = $step($gen); } } return $gen; } function iterator_chunk(\Traversable $it, $chunkSize) { $it->rewind(); $chunk = []; $steps = range(1, $chunkSize); while (true) { foreach ($steps as $_) { if (!$it->valid()) { break 2; } $chunk[] = $it->current(); $it->next(); } yield $chunk; $chunk = []; } if ($chunk) { yield $chunk; } } $steps = [ function($it) { foreach ($it as $val) { echo "Value: $val\n"; yield $val; } }, ["iterator_chunk", 32], ]; $it = iterator_compose(new \ArrayIterator(range(0, 127)), $steps); foreach ($it as $i => $c) { echo "====== Chunk $i ======\n"; var_export($c); echo "\n"; }
Output for 5.5.0 - 7.1.0
Value: 0 Value: 1 Value: 2 Value: 3 Value: 4 Value: 5 Value: 6 Value: 7 Value: 8 Value: 9 Value: 10 Value: 11 Value: 12 Value: 13 Value: 14 Value: 15 Value: 16 Value: 17 Value: 18 Value: 19 Value: 20 Value: 21 Value: 22 Value: 23 Value: 24 Value: 25 Value: 26 Value: 27 Value: 28 Value: 29 Value: 30 Value: 31 Value: 32 ====== 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, ) Value: 33 Value: 34 Value: 35 Value: 36 Value: 37 Value: 38 Value: 39 Value: 40 Value: 41 Value: 42 Value: 43 Value: 44 Value: 45 Value: 46 Value: 47 Value: 48 Value: 49 Value: 50 Value: 51 Value: 52 Value: 53 Value: 54 Value: 55 Value: 56 Value: 57 Value: 58 Value: 59 Value: 60 Value: 61 Value: 62 Value: 63 Value: 64 ====== Chunk 1 ====== array ( 0 => 32, 1 => 33, 2 => 34, 3 => 35, 4 => 36, 5 => 37, 6 => 38, 7 => 39, 8 => 40, 9 => 41, 10 => 42, 11 => 43, 12 => 44, 13 => 45, 14 => 46, 15 => 47, 16 => 48, 17 => 49, 18 => 50, 19 => 51, 20 => 52, 21 => 53, 22 => 54, 23 => 55, 24 => 56, 25 => 57, 26 => 58, 27 => 59, 28 => 60, 29 => 61, 30 => 62, 31 => 63, ) Value: 65 Value: 66 Value: 67 Value: 68 Value: 69 Value: 70 Value: 71 Value: 72 Value: 73 Value: 74 Value: 75 Value: 76 Value: 77 Value: 78 Value: 79 Value: 80 Value: 81 Value: 82 Value: 83 Value: 84 Value: 85 Value: 86 Value: 87 Value: 88 Value: 89 Value: 90 Value: 91 Value: 92 Value: 93 Value: 94 Value: 95 Value: 96 ====== Chunk 2 ====== array ( 0 => 64, 1 => 65, 2 => 66, 3 => 67, 4 => 68, 5 => 69, 6 => 70, 7 => 71, 8 => 72, 9 => 73, 10 => 74, 11 => 75, 12 => 76, 13 => 77, 14 => 78, 15 => 79, 16 => 80, 17 => 81, 18 => 82, 19 => 83, 20 => 84, 21 => 85, 22 => 86, 23 => 87, 24 => 88, 25 => 89, 26 => 90, 27 => 91, 28 => 92, 29 => 93, 30 => 94, 31 => 95, ) Value: 97 Value: 98 Value: 99 Value: 100 Value: 101 Value: 102 Value: 103 Value: 104 Value: 105 Value: 106 Value: 107 Value: 108 Value: 109 Value: 110 Value: 111 Value: 112 Value: 113 Value: 114 Value: 115 Value: 116 Value: 117 Value: 118 Value: 119 Value: 120 Value: 121 Value: 122 Value: 123 Value: 124 Value: 125 Value: 126 Value: 127 ====== Chunk 3 ====== array ( 0 => 96, 1 => 97, 2 => 98, 3 => 99, 4 => 100, 5 => 101, 6 => 102, 7 => 103, 8 => 104, 9 => 105, 10 => 106, 11 => 107, 12 => 108, 13 => 109, 14 => 110, 15 => 111, 16 => 112, 17 => 113, 18 => 114, 19 => 115, 20 => 116, 21 => 117, 22 => 118, 23 => 119, 24 => 120, 25 => 121, 26 => 122, 27 => 123, 28 => 124, 29 => 125, 30 => 126, 31 => 127, )
Output for 5.4.0 - 5.4.45
Parse error: syntax error, unexpected '$chunk' (T_VARIABLE) in /in/DanGD on line 43
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/DanGD on line 30
Process exited with code 255.
Output for 5.1.0 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/DanGD on line 4 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/DanGD on line 14 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/DanGD on line 27 Parse error: syntax error, unexpected '[' in /in/DanGD on line 30
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/DanGD on line 4 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/DanGD on line 14 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/DanGD on line 27 Parse error: parse error, unexpected '[' in /in/DanGD on line 30
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/DanGD on line 4 Parse error: syntax error, unexpected T_STRING, expecting ')' in /in/DanGD on line 4
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/DanGD on line 4 Parse error: parse error, unexpected T_STRING, expecting ')' in /in/DanGD on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/DanGD on line 4 Parse error: parse error, expecting `')'' in /in/DanGD on line 4
Process exited with code 255.