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_pipeline(\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; } } function iterator_consume(\Traversable $it) { foreach ($it as $key => $value) {} } function iterator_pass(\Traversable $it) { foreach ($it as $key => $value) { yield $key => $value; } } function iterator_tee(\Traversable $source, array $drains) { foreach ($source as $key => $value) { foreach ($drains as $gen) { $gen->send($value); } yield $key => $value; } } $receiver = function() { for (;;) { print_r((yield)); } }; $steps = [ function($it) { foreach ($it as $val) { yield $val * 2; } }, "iterator_pass", ["iterator_chunk", 32], ["iterator_tee", [$receiver]] ]; $it = iterator_pipeline(new \ArrayIterator(range(0, 127)), $steps); iterator_consume($it);
Output for 7.0.0 - 7.1.0
Fatal error: Uncaught Error: Call to undefined method Closure::send() in /in/1tVr7:68 Stack trace: #0 /in/1tVr7(54): iterator_tee(Object(Generator), Array) #1 /in/1tVr7(94): iterator_consume(Object(Generator)) #2 {main} thrown in /in/1tVr7 on line 68
Process exited with code 255.
Output for hhvm-3.10.0 - 3.12.0
Fatal error: syntax error, unexpected T_YIELD in /in/1tVr7 on line 77
Process exited with code 255.
Output for 5.5.0 - 5.6.28
Fatal error: Call to undefined method Closure::send() in /in/1tVr7 on line 68
Process exited with code 255.
Output for 5.4.0 - 5.4.45
Parse error: syntax error, unexpected '$chunk' (T_VARIABLE) in /in/1tVr7 on line 43
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/1tVr7 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/1tVr7 on line 4 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/1tVr7 on line 14 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/1tVr7 on line 27 Parse error: syntax error, unexpected '[' in /in/1tVr7 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/1tVr7 on line 4 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/1tVr7 on line 14 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/1tVr7 on line 27 Parse error: parse error, unexpected '[' in /in/1tVr7 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/1tVr7 on line 4 Parse error: syntax error, unexpected T_STRING, expecting ')' in /in/1tVr7 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/1tVr7 on line 4 Parse error: parse error, unexpected T_STRING, expecting ')' in /in/1tVr7 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/1tVr7 on line 4 Parse error: parse error, expecting `')'' in /in/1tVr7 on line 4
Process exited with code 255.