Re: Proposal: Expanded iterable helper functions and aliasing iterator_to_array in `iterable\` namespace

From: Date: Fri, 28 Oct 2022 15:41:05 +0000
Subject: Re: Proposal: Expanded iterable helper functions and aliasing iterator_to_array in `iterable\` namespace
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-118899@lists.php.net to get a copy of this message
Hi On 10/28/22 15:45, tyson andre wrote:
- 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)
I wonder if this should be made an alias or if the introduction of the namespace this is a good opportunity to revisit the `$preserve_keys = true` default choice. See also this email in the voting discussion for a previous iter*able*_to_array() RFC: https://externals.io/message/102562#102616
- 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.
I would not make the callable optional. If one really wants to filter on the falsyness of the items, any($i, fn ($v) => !!$v) is easy enough. I'd likely also swap the order of parameters, having the callable first is more natural with a possible pipe operator or partial application. It also is arguably more readable when using nested function calls, because the callback appears immediately beside the function name: (Callback first) iterable\count( iterable\filter(
    fn ($v) => $v > 5
    iterable\map(
      fn ($v) => strlen($v),
      $iterable
    )
) ) vs (Callable last) iterable\count( iterable\filter(
    iterable\map(
      $iterable,
      fn ($v) => strlen($v)
    ),
    fn ($v) => $v > 5
) )
- 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
It appears you mixed up the descriptions here.
Any comments?
Your proposals look good to me. Especially any and all are something I'm missing somewhat regularly. Is there a reason there is no iterable\map() and iterable\filter() (that I used in my example above)? Best regards Tim Düsterhus

« previous php.internals (#118899) next »