@ 2014-09-25T14:18:05Z <?php
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);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 5.5.25 - 5.5.38 , 5.6.9 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.25 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.7 Warning: The use statement with non-compound name 'ArrayIterator' has no effect in /in/04Udl on line 3
Warning: The use statement with non-compound name 'Traversable' has no effect in /in/04Udl on line 4
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.5.0 - 5.5.24 , 5.6.0 - 5.6.8 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/04Udl on line 48
Process exited with code 255 . Output for 5.3.0 - 5.3.29 Parse error: syntax error, unexpected T_VARIABLE in /in/04Udl on line 48
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 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/04Udl on line 3
Parse error: syntax error, unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING or '(' in /in/04Udl 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 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/04Udl on line 3
Parse error: parse error, unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING or '(' in /in/04Udl on line 3
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/04Udl on line 3
Parse error: parse error, expecting `T_CONSTANT_ENCAPSED_STRING' or `'('' in /in/04Udl on line 3
Process exited with code 255 . preferences:dark mode live preview
243.83 ms | 402 KiB | 369 Q