On Sunday, 20 October 2024 at 18:42, Gina P. Banyard <intern...@gpb.moe> wrote:

> Hello internals,
> 
> I would like to propose a short RFC to make the return value of the sort() 
> and similar functions more useful:
> https://wiki.php.net/rfc/array-sort-return-array

I am going to respond out of threads.

This RFC does *NOT* change the by-ref parameter passing *NOR* the in-place 
sorting of these functions.
I don't see why people are thinking this would be changed, as I only ever talk 
about the return value.
But I added a sentence in the Unaffected PHP Functionality section.

I really struggle to see the benefit of introducing new functions that would do 
the exact same thing as this RFC,
except the in-place modification, as seen by the already existing bikesheeding 
on function naming.

For an example, I'm going to pull out my solution to day 1 of the 2022 advents 
of code:

https://github.com/Girgias/advent-of-code/blob/19283e1f5ef503c8a4478e58aaa57ff2fb7164c7/2022/01/puzzle.php#L25

If we had a pipe operator, or if decided to write this less legible with nested 
calls, it would like this:

$elves = array_map(
    'array_sum',
    array_map(
        fn($v) => explode("\n", $v),
        explode(
            "\n\n",
            $input
        )
    )
);
rsort($elves);
$top3 = array_slice($elves, 0, length: 3);

However, if the sort functions would return a useful value instead of `true` I 
could chain it together completely as follows:

$top3 = array_slice(
    rsort(
        array_map(
            'array_sum',
            array_map(
                fn($v) => explode("\n", $v),
                explode(
                    "\n\n",
                    $input
                )
            )
        )
    ),
    0,
    length: 3
);

In either case, the fact the array is sorted in place is something I don't 
care, as it is just a temporary step.
However, in the current state, I need to stop chaining function calls just to 
be able to get the sorted array that I am going to process and discard at the 
next function call.

This type of data processing pattern can be generalized to any collection of 
data that needs to be sorted/filtered and processed upon.

This RFC is "just" making the return value of these functions *useful*.

Would a "proper" functional variant of these functions which do not take the 
input argument by reference be useful? Yes.
Is this completely orthogonal to the point of this RFC? Also yes.

Best regards,

Gina P. Banyard

Reply via email to