Proposal: Expanded iterable helper functions and aliasing iterator_to_array in `iterable\` namespace
| From: | tyson andre | Date: | Fri, 28 Oct 2022 13:45:06 +0000 |
| Subject: | Proposal: Expanded iterable helper functions and aliasing iterator_to_array in `iterable\` namespace | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-118896@lists.php.net to get a copy of this message | ||
Hi internals,
https://wiki.php.net/rfc/iterator_xyz_accept_array
recently passed in php 8..2,
fixing a common inconvenience of those functions throwing a TypeError for arrays.
However, from the
iterator_ name (https://www.php.net/manual/en/class.iterator.php),
it's likely to become a source of confusion when writing or reviewing code decades from now,
when the name suggests it only accepts objects (Traversable Iterator/IteratorAggregate).
I'm planning on creating an RFC adding the following functions to the iterable\
namespace as aliases of iterator_count/iterator_to_array.
Those accept iterables (https://www.php.net/manual/en/language.types.iterable.php), i.e. both
Traversable objects and arrays.
Namespaces were chosen after feedback on my previous RFC,
and I believe iterable\ follows the guidance from https://wiki.php.net/rfc/namespaces_in_bundled_extensions
and
https://wiki.php.net/rfc/namespaces_in_bundled_extensions#core_standard_spl
I plan to create an RFC with the following functionality in the iterable\ namespace, and wanted to
see what the preference on naming was, or if there was other feedback.
(Not having enough functionality and wanting a better idea of the overall
- iterable\count(...) (alias of iterator_count)
- iterable\to_array(Traversable $iterator, bool $preserve_keys = true): array (alias of
iterator_to_array, so that users can stop using a misleading name)
- iterable\any(iterable $input, ?callable $callback = null): bool - Determines whether
any value of the iterable satisfies the predicate.
and all() - Determines whether all values of the iterable satisfies the predicate.
This is a different namespace from https://wiki.php.net/rfc/any_all_on_iterable
- iterable\none(iterable $input, ?callable $callback = null): bool
returns the opposite of any()
- iterable\find(iterable $iterable, callable $callback, mixed $default = null): mixed
Returns the first value for which $callback($value) is truthy. On failure, returns default
- iterable\fold(iterable $iterable, callable $callback, mixed $initial): mixed
fold and requiring an initial value seems like better practice. See https://externals.io/message/112558#112834
and https://stackoverflow.com/questions/25149359/difference-between-reduce-and-fold
- iterable\unique_values(iterable $iterable): array {}
Returns true if this iterable includes a value identical to $value (===).
- iterable\includes_value(iterable $iterable, mixed $value): bool {}
Returns a list of unique values of $iterable
There's other functionality that I was less certain about proposing, such as
iterable\keys(iterable $iterable): array,
which would work similarly to array_keys but also work on Traversables (e.g.. to be used with
userland/internal collections, generators, etc.)
Or functions to get the iterable\first()/last() value in an iterable. Any thoughts on those?
I also wanted to know if more verbose names such as find_value(), fold_values(), any_values(),
all_values() were generally preferred before proposing this,
since I only had feedback from a small number of names. My assumption was short names were generally
preferred when possible.
See https://github.com/TysonAndre/pecl-teds/blob/main/teds.stub.php
for documentation of the other functions mentioned here. The functionality can be tried out by
installing https://pecl.php.net/package/teds
Background
-----------
In February 2021, I proposed expanded iterable functionality and brought it to a vote,
https://wiki.php.net/rfc/any_all_on_iterable
, where feedback was mainly about being too small in scope and the choice of naming.
Later, after https://externals.io/message/112558#112780 ,
https://wiki.php.net/rfc/namespaces_in_bundled_extensions#proposal
was created and brought to a vote in April 2021 that passed,
offering useful recommendations on how to standardize namespaces in future proposals of new
categories of functionality
(e.g. iterable\any() and iterable\all())
Any comments?
Thanks,
Tyson