php-src icon indicating copy to clipboard operation
php-src copied to clipboard

New functions array_avg and array_median

Open niconoe- opened this issue 4 years ago • 6 comments

Description

Hi !

Maybe this has been already requested but I can't find it on GitHub. If this issue is a duplicate, please close it.

In order to avoid user implementations of the calculations of averages and medians from an array of floats, I'm proposing the addition of 2 new PHP internal functions in the "array functions list":

  • array_avg(array<float> $array): float
  • array_median(array<float> $array): float

Those functions would act just like array_sum and array_product, but to respectively calculate the arithmetical average and the median of a numeric list.
Such functions already exist in some other languages, and I think it could be helpful for PHP developers too.

As the average and median calculation is implying a division by the number of elements in the list, and such number could be 0, in order to be errorless and not mixing return types, my proposal would be to return 0 if $array is empty for each of those functions.

Here are some possible polyfills:

function array_avg(array $array): float 
{
    return [] === $array ? 0 : array_sum($array) / count($array);
}

function array_median(array $array): float 
{
    $nbElements = count($array); 
    if (0 === $nbElements) {
        return 0;
    }
    // Sort the array values (Thanks @TimWolla !).
    sort($array);
    if ($nbElements % 2) {
        return $array[floor($nbElements / 2)];
    }
    return ($array[$nbElements / 2 - 1] + $array[$nbElements / 2]) / 2;
}

Those mathematical functions are often use even if not working on the statistical domain, and could be easily added into the PHP ecosystem IMO.

Only BCBreak possible is when there are already user-defined functions called "array_avg" or "array_median".

What do you think about?

Thanks a lot! Best regards

niconoe- avatar Mar 09 '22 13:03 niconoe-

Hi! Historically the topic of adding more functions to the standard library has been polarizing. Many people are against the addition of functions that can be implemented in userland without a clear disadvantage. You might want to send a message to the internals mailing list to get some initial impressions. This would also require an RFC.

iluuu1994 avatar Mar 09 '22 13:03 iluuu1994

Note that there is PECL/stats which already may have such functions.

cmb69 avatar Mar 09 '22 14:03 cmb69

Note that there is PECL/stats which already may have such functions.

This is nice, I didn't know about PECL/stats! Unfortunately, this package looks like unmaintained but I understand the idea: if this does already exist elsewhere, there is no need to have it internally.

However I'm feeling those addition to the PHP library is a good thing. I subscribed to the internals mailing list and I'll try to inform them and get some opinions, as sugested by @iluuu1994 . I understand the polarization as if internals would add every request, we'll probably have a functions with too much specific behaviors, but those functions are really technical.
Implementation in userland is forcing people to build a specific wheel, but when such wheel is hyper-generic, it should be implemented in the language IMO.

niconoe- avatar Mar 09 '22 14:03 niconoe-

function array_median(array $array): float 

This polyfill is not correct, because it assumes that the input array is sorted.

TimWolla avatar Mar 16 '22 11:03 TimWolla

function array_median(array $array): float 

This polyfill is not correct, because it assumes that the input array is sorted.

True. Maybe that's an additionnal reason to have those new functions internally: avoid errors functions in userland could bring by an incomplete implementation... :smile:

EDIT: I added the sort, just in case this polyfill is used by someone looking for a working example.

niconoe- avatar Mar 16 '22 11:03 niconoe-

This likely won't go anywhere unless somebody creates an RFC and implementation. So I'm closing this issue.

iluuu1994 avatar Apr 15 '22 13:04 iluuu1994