3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php namespace Atom; interface CollectionInterface extends \IteratorAggregate { /** * * @param \Atom\AggregatorInterface $aggregator * @return mixed */ function aggregate(\Atom\AggregatorInterface $aggregator); /** * * @param \Atom\EnumeratorInterface $enumerator * @return \Atom\CollectionInterface */ function enumerate(\Atom\EnumeratorInterface $enumerator); /** * * @return array */ function getArray(); } class Collection implements \Atom\CollectionInterface { /** * * @param mixed $iterable * @return \Atom\Collection */ public static function create($iterable) { if (is_array($iterable)) { return new \Atom\Collection(new \ArrayIterator($iterable)); } return new \Atom\Collection(new \IteratorIterator($iterable)); } /** * * @var \Iterator */ private $iterator; /** * * @param \Iterator $iterator */ public function __construct(\Iterator $iterator) { $this->iterator = $iterator; } /** * * @param \Atom\AggregatorInterface $aggregator * @return mixed */ public function aggregate(\Atom\AggregatorInterface $aggregator) { return $aggregator->aggregate($this); } /** * * @param \Atom\EnumeratorInterface $enumerator * @return \Atom\CollectionInterface */ public function enumerate(\Atom\EnumeratorInterface $enumerator) { return self::create($enumerator->enumerate($this)); } /** * * @return \Iterator */ public function getIterator() { return $this->iterator; } /** * * @return array */ public function getArray() { return iterator_to_array($this->iterator); } } interface EnumeratorInterface { /** * * @param \Atom\CollectionInterface $collection * @return mixed */ function enumerate(\Atom\CollectionInterface $collection); } interface AggregatorInterface { /** * * @param \Atom\CollectionInterface $collection * @return mixed */ function aggregate(\Atom\CollectionInterface $collection); } class MapEnumerator implements \Atom\EnumeratorInterface { /** * * @var callable */ private $function; /** * * @param callable $function */ public function __construct(callable $function) { $this->function = $function; } /** * * @param \Atom\CollectionInterface $collection * @return mixed */ public function enumerate(\Atom\CollectionInterface $collection) { $function = $this->function; foreach ($collection as $key => $value) { yield $key => $function($value, $key); } } } class FilterEnumerator implements \Atom\EnumeratorInterface { /** * * @var callable */ private $function; /** * * @param callable $function */ public function __construct(callable $function) { $this->function = $function; } /** * * @param \Atom\CollectionInterface $collection * @return mixed */ public function enumerate(\Atom\CollectionInterface $collection) { $function = $this->function; foreach ($collection as $key => $value) { if ($function($value, $key)) { yield $key => $value; } } } } class SumAggregator implements \Atom\AggregatorInterface { /** * * @param \Atom\CollectionInterface $collection * @return mixed */ public function aggregate(\Atom\CollectionInterface $collection) { return array_sum($collection->getArray()); } } $sum = \Collection::create([1, 2, 3, 4, 5])->enumerate(new \MapEnumerator(function ($value) { return $value * 3; }))->enumerate(new \FilterEnumerator(function ($value) { return $value % 2 == 0; }))->aggregate(new \SumAggregator()); var_dump($sum);
Output for 7.0.0 - 7.1.0
Fatal error: Uncaught Error: Class 'Collection' not found in /in/psg7u:208 Stack trace: #0 {main} thrown in /in/psg7u on line 208
Process exited with code 255.
Output for hhvm-3.12.0
Fatal error: Class undefined: Collection in /in/psg7u on line 208
Process exited with code 255.
Output for hhvm-3.10.0

Process exited with code 153.
Output for 5.5.24 - 5.6.28
Fatal error: Class 'Collection' not found in /in/psg7u on line 208
Process exited with code 255.
Output for 5.4.0 - 5.4.45
Parse error: syntax error, unexpected '$key' (T_VARIABLE) in /in/psg7u on line 152
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected T_VARIABLE in /in/psg7u on line 152
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/psg7u 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/psg7u on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/psg7u on line 3
Process exited with code 255.