@ 2015-12-23T02:49:49Z <?php
namespace Acme {
class Collection {
private $_items;
public function __construct(array $items = [])/*: void*/ {
$this->_items = $items;
}
public function filter(callable $callback): self {
foreach ($this->_items as $key => $value) {
if (!call_user_func($callback, $value)) {
unset($this->_items[$key]);
}
}
return $this;
}
public function map(callable $callback): self {
foreach ($this->_items as $key => $value) {
$this->_items[$key] = call_user_func($callback, $key, $value);
}
return $this;
}
public function reindex(): self {
$this->_items = array_values($this->_items);
}
public function sprintf(string $format): self {
return $this->map(function ($key, $value) use ($format) {
return substr_count($format, '%s') === 1
? sprintf($format, $value)
: sprintf($format, $key, $value);
});
}
public function implode(string $delimiter = ''): string {
$output = '';
$first = TRUE;
foreach ($this->_items as $value) {
if ($first) {
$output .= $value;
$first = FALSE;
} else {
$output .= "$delimiter$value";
}
}
return $output;
}
public static function property(string $property): callable {
return function ($key, $value) use ($name) {
return $value->$property;
};
}
}
}
namespace {
use Acme\Collection;
echo (new Collection([
'one',
'two',
(object)['foo' => 'FOO', 'bar' => 'BAR', 'baz' => 'BAZ'],
'three',
(object)['bar' => 'BAR', 'qux' => 'QUX'],
]))
->filter('is_object')
->map(Collection::property('bar'))
->reindex()
->sprintf('(%s,%s)')
->implode('_');
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 7.0.1 0.007 0.043 20.16 7.0.0 0.003 0.043 20.10 5.6.16 0.010 0.053 20.54 5.6.15 0.007 0.073 18.14 5.6.14 0.010 0.080 18.12 5.6.13 0.000 0.043 18.14 5.6.12 0.017 0.073 21.13 5.6.11 0.003 0.043 21.00 5.6.10 0.017 0.077 21.00 5.6.9 0.003 0.080 21.08 5.6.8 0.010 0.037 20.37 5.5.30 0.003 0.083 17.98 5.5.29 0.000 0.097 17.96 5.5.28 0.003 0.060 20.78 5.5.27 0.017 0.077 20.73 5.5.26 0.017 0.073 20.68 5.5.25 0.010 0.033 20.67 5.5.24 0.013 0.070 20.20
preferences:dark mode live preview
146.6 ms | 939 KiB | 8 Q