Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118899 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 16659 invoked from network); 28 Oct 2022 15:41:09 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 28 Oct 2022 15:41:09 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C773D18055A for ; Fri, 28 Oct 2022 08:41:08 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS24940 176.9.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from chrono.xqk7.com (chrono.xqk7.com [176.9.45.72]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 28 Oct 2022 08:41:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1666971666; bh=GGtnr3W7W1hr4+TZmrnZ90TF890CwvwTpy5/RGzWzBY=; h=Date:Subject:To:References:From:In-Reply-To:From; b=YB2ZOy3BcJnlIB4SC7s03wE6On/UZ7gEBpgVXzaddRJRxbAmBfqqXbmYrCnI3ga+c gSYw1zDzaUn68cciup2usCndMJtVME8fcckQfzAtPqFi5AEoUOs+DGKUdzt0AkhH6D EF9bXiQoNGa7o7TVg2LRenZYarjKx3wlErJRwnKmmcSfMg8QtUFS5z5dJ21+i/KBBu JA9pZ0Vt5zW2b7DqYwtF1QY2zSXDH6CUeHB/Wwqhuy/868d8aLG3T0+eSskC88OoaY yktYJTf+5k+T0Wm4mDu4YIdUFRJ0ep46fzKoWixBUj6FpwIX5cE1EpWu0Ay5Gh1ljh zM1ngcR82TBPg== Message-ID: <0b47f7c1-909f-f81d-0d27-7c8981e8b56a@bastelstu.be> Date: Fri, 28 Oct 2022 17:41:05 +0200 MIME-Version: 1.0 Content-Language: en-US To: tyson andre , "internals@lists.php.net" References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] Proposal: Expanded iterable helper functions and aliasing iterator_to_array in `iterable\` namespace From: tim@bastelstu.be (=?UTF-8?Q?Tim_D=c3=bcsterhus?=) 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