3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php namespace Enumerator; use \ArrayIterator; use \Traversable; class Enumerable { /** * * @var Traversable */ private $traversable; /** * * @param Traversable|array $traversable * @return Enumerable */ public static function enumerate($traversable) { return new Enumerable($traversable); } /** * * @param Traversable|array $traversable */ public function __construct($traversable) { if ($traversable instanceof Traversable) { $this->traversable = $traversable; } else { $this->traversable = new ArrayIterator((array) $traversable); } } /** * * @param callable $callable * @return Enumerable */ public function select(callable $callable) { $generator = function () use ($callable) { foreach ($this->traversable as $key => $value) { yield $key => $callable($value, $key); } }; return new Enumerable($generator()); } /** * * @param callable $callable * @return Enumerable */ public function filter(callable $callable) { $generator = function () use ($callable) { foreach ($this->traversable as $key => $value) { if ($callable($value, $key)) { yield $key => $value; } } }; return new Enumerable($generator()); } /** * * @return array */ public function toArray() { return iterator_to_array($this->traversable); } } $array = Enumerable::enumerate([ 1, 6, 3, 8, 6, 1, 9, 4, 2, 4, 6, 4, 2, ])->select(function ($i) { echo sprintf('Selecting %d * 2', $i) . PHP_EOL; return $i * 2; })->filter(function ($i) { echo sprintf(' Filtering %d > 10', $i) . PHP_EOL; return $i > 10; })->select(function ($i) { echo sprintf(' Selecting %d * 3', $i) . PHP_EOL; return $i * 3; })->filter(function ($i) { echo sprintf(' Filtering %d > 40', $i) . PHP_EOL; return $i > 40; })->select(function ($i) { echo sprintf(' Selecting %d * 4', $i) . PHP_EOL; return $i * 3; })->toArray(); var_dump($array);
based on 04Udl
Output for 5.5.24 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
Selecting 1 * 2 Filtering 2 > 10 Selecting 6 * 2 Filtering 12 > 10 Selecting 12 * 3 Filtering 36 > 40 Selecting 3 * 2 Filtering 6 > 10 Selecting 8 * 2 Filtering 16 > 10 Selecting 16 * 3 Filtering 48 > 40 Selecting 48 * 4 Selecting 6 * 2 Filtering 12 > 10 Selecting 12 * 3 Filtering 36 > 40 Selecting 1 * 2 Filtering 2 > 10 Selecting 9 * 2 Filtering 18 > 10 Selecting 18 * 3 Filtering 54 > 40 Selecting 54 * 4 Selecting 4 * 2 Filtering 8 > 10 Selecting 2 * 2 Filtering 4 > 10 Selecting 4 * 2 Filtering 8 > 10 Selecting 6 * 2 Filtering 12 > 10 Selecting 12 * 3 Filtering 36 > 40 Selecting 4 * 2 Filtering 8 > 10 Selecting 2 * 2 Filtering 4 > 10 array(2) { [3]=> int(144) [6]=> int(162) }
Output for 5.4.0 - 5.4.45
Parse error: syntax error, unexpected '$key' (T_VARIABLE) in /in/I30Sb on line 50
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected T_VARIABLE in /in/I30Sb on line 50
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.2.17
Parse error: syntax error, unexpected T_STRING in /in/I30Sb on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/I30Sb on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/I30Sb on line 3
Process exited with code 255.