Re: RFC [Discussion] array_column results grouping
| From: | Hendra Gunawan | Date: | Tue, 07 Dec 2021 23:26:23 +0000 |
| Subject: | Re: RFC [Discussion] array_column results grouping | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-116585@lists.php.net to get a copy of this message | ||
>
> And if you want to map, reduce, or filter without grouping? Then you can't
> really use this function. And as noted, the order in which you apply those
> operations may matter, and no order is necessarily more obvious or beneficial
You can modify it slightly if you eager to:
array_group(
...
null|int|string|array $index_key,
...
): array
If You provide
null, it will operate on a bare array, no grouping involved.
As a consequence, users have to provide reducer or mapper. So virtually,
You design array_group to swallow array_reduce and array_map.
Though,
array_map is actually a different API compared to array_reduce or
array_group.
Interestingly, you will get a better quality of code if you include reducer:
and mapper: consistently, whatever array level you work with.
// array_reduce and array_map alternative
array_group($array, null, reducer: $fnReducer);
array_group($array, null, mapper: $fnMapper);
array_group($array, "level1", reducer: $fnReducer);
array_group($array, "level1", mapper: $fnMapper);
array_group($array, ["level1", "level2"], reducer: $fnReducer);
array_group($array, ["level1", "level2"], mapper: $fnMapper);
I hope there is no bias between the proposed name (array_group) and its
functionality.
>
> Either of these approaches would be superior to cramming more functionality
> into a single-purpose function to turn it into a single-function swiss army
> knife:
Not a swiss army knife, just a penknife. AS i wrote before, it is just a
shortcut. If you really mind about that, we can cancel filter and sorter,
leaving only reducer and mapper. Thought, we can filtering and sorting
before array_group, but i don't know whether it is as accurate as embedding
them into array_group or not. If not, we have to iterate manually, do
filtering and/or sorting, then reducing/mapping.
>
> https://wiki.php.net/rfc/comprehensions
> https://wiki.php.net/rfc/pipe-operator-v2
Of course these are wonderful proposals. Yes, they are superior. To be honest,
I really hope they are landed in PHP. I already imagine what part of my code
will be replaced by them. Since I am not a C coder and totally blind about the
engine, I don't have my own opinion whether we lost good things or not, by not
adding them to PHP.