@ 2014-10-30T17:09:45Z <?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 = \Atom\Collection::create([1, 2, 3, 4, 5])->enumerate(new \Atom\MapEnumerator(function ($value) {
return $value * 3;
}))->enumerate(new \Atom\FilterEnumerator(function ($value) {
return $value % 2 == 0;
}))->aggregate(new \Atom\SumAggregator());
var_dump($sum);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 Deprecated: Return type of Atom\Collection::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/q93Ti on line 85
int(18)
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 int(18)
Output for 5.4.0 - 5.4.45 Parse error: syntax error, unexpected '$key' (T_VARIABLE) in /in/q93Ti 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/q93Ti on line 152
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 Parse error: syntax error, unexpected T_STRING in /in/q93Ti on line 3
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 T_STRING in /in/q93Ti on line 3
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error in /in/q93Ti on line 3
Process exited with code 255 . preferences:dark mode live preview
239.55 ms | 401 KiB | 459 Q