@ 2021-06-24T21:50:00Z )<?php
class SinglePlaceholder
{
}
class VariadicPlaceholder
{
}
$PLACEHOLDER = new SinglePlaceholder();
$VARIADIC_PLACEHOLDER = new VariadicPlaceholder();
function partial(callable $in, ...$args) {
$fmap = [];
$partialIndex = 0;
$variadic = false;
foreach ($args as $k => $arg) {
if ($arg instanceof SinglePlaceholder) {
$fmap[$partialIndex++] = $k;
}
if ($arg instanceof VariadicPlaceholder) {
$variadic = true;
if ($k < count($args) - 1) {
throw new ArgumentCountError('Only the last parameter may be a variadic placeholder');
}
$fmap[$partialIndex++] = $k;
}
}
return static function (...$partialArgs) use ($in, $args, $fmap, $variadic) {
if (count($partialArgs) < count($fmap)) {
throw new ArgumentCountError('Expected ' . count($fmap) . ' only received ' . count($partialArgs));
}
foreach ($partialArgs as $pIndex => $pValue) {
$targetSlot = $fmap[$pIndex] ?? null;
if ($targetSlot === null) {
if ($variadic) {
$args[] = $pValue;
}
else {
throw new ArgumentCountError('Too many arguments without a variadic placeholder');
}
}
else {
$args[$targetSlot] = $pValue;
}
}
return $in(...$args);
};
}
function foo($a, $b, $c, $d, $e) {
var_dump($a, $b, $c, $d, $e);
}
print_r(partial('foo', 1, $PLACEHOLDER, 3, $PLACEHOLDER, 5)(2, 4));
print_r(partial('foo', $VARIADIC_PLACEHOLDER)('a', 'b', 'c', 'd', 'e'));
print_r(partial('foo', 1, 2, 3, 4, 5)());
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) 8.3.6 0.011 0.004 16.75 8.3.5 0.011 0.011 18.09 8.3.4 0.011 0.011 19.28 8.3.3 0.007 0.007 18.75 8.3.2 0.004 0.004 24.18 8.3.1 0.005 0.003 24.66 8.3.0 0.008 0.000 26.16 8.2.18 0.010 0.010 25.92 8.2.17 0.004 0.016 18.95 8.2.16 0.000 0.014 22.96 8.2.15 0.004 0.004 25.66 8.2.14 0.000 0.008 24.66 8.2.13 0.000 0.008 26.16 8.2.12 0.004 0.004 26.16 8.2.11 0.006 0.003 20.27 8.2.10 0.005 0.005 20.62 8.1.28 0.007 0.011 25.92 8.1.27 0.004 0.004 23.99 8.1.26 0.008 0.000 26.35 8.1.25 0.000 0.009 28.09 8.1.24 0.010 0.003 18.61 8.1.23 0.009 0.000 22.18 8.0.7 0.008 0.010 16.92
preferences:dark mode live preview
45.25 ms | 401 KiB | 5 Q