Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111768 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 53368 invoked from network); 1 Sep 2020 21:48:40 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 1 Sep 2020 21:48:40 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C2D0518050B for ; Tue, 1 Sep 2020 13:53:23 -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,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-ej1-f53.google.com (mail-ej1-f53.google.com [209.85.218.53]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 1 Sep 2020 13:53:23 -0700 (PDT) Received: by mail-ej1-f53.google.com with SMTP id q13so3584196ejo.9 for ; Tue, 01 Sep 2020 13:53:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=YdO7hYt1RenYDUyR3hKFKq8Ibb6x3ukaYIpjpJW6/+g=; b=pPsRtsrcu1XmwFjWwvCLL+ss7vLSeGla/16a3yz8nyrcTXrcsHIep7TwJVgghJ/2nz BWJXAQWUdc+IGjRsXYhRKXh0n614NT6rs6K0FnLIyOvGvv0eL5PrQPw4Nd/owoMGkFcE fLM5Dk/9BuKyfMGRo5U1efAElO3d/caizfAqhgxShYtDkYmSoF3tq7hrJPB4mYTadYsD tM9mqc5AfXHyjCZNWBJbo+sTCCbdrixyNecoOoupKrZ/rw9l4BPRCv3HFtPNh4tyEfcx rYfVu1U+09Ybpwqpn3U5XIfOmmTmr1MXyNnI9r6KFjvsVrcHF61t8iN3PEP8M+8TTIql OM+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=YdO7hYt1RenYDUyR3hKFKq8Ibb6x3ukaYIpjpJW6/+g=; b=WntK5oPUwhgTqKvhWmSzLuEGqS5mMOC0gu0eBBik9nEDV6s7myUqU+5fPkFFaTlG1z bQqixZuk1n5b8qJ0nkxGtkZ1Sb/sP2ZFd0ApvhvrD+gssnR8YarWqhZUkJnt/FxcVb1T hViJwGEKHOKHjss95WHrVz/gyg8YC+K8B6OuO6IBQTgBlVm9YmDE8Yee0LVdiB6DjAto HT6I/AEH1PzZ/pwADF5YYPSPzGIaexKjaa0p9RjwxTi9F+BDrQpHWkMGxU1MaEwgapiM wQnWh88s8C0vz8kqSZ/PGRFNqJyN+daS75sSKznwMoVcI/I7ZLVWzxHYCuW+1tqo/msc LvEA== X-Gm-Message-State: AOAM530Z6kQ0r8TFzPIAHrzw1qFSOGno6HRrYktTg+MJpjM9M/9Cp+Px LHOvuNablfNOqPZ58B3ylmY///wZpDCnyw== X-Google-Smtp-Source: ABdhPJxQKIaTora0egXjo33sptSX77T1ShVy8UrNZRif7CSOTiUXryQ1uxaB1IblzONuMIEdN+6V+w== X-Received: by 2002:a17:906:19db:: with SMTP id h27mr3251044ejd.154.1598993600875; Tue, 01 Sep 2020 13:53:20 -0700 (PDT) Received: from ?IPv6:2001:983:6fc5:1:49c7:735:8c90:b4f5? ([2001:983:6fc5:1:49c7:735:8c90:b4f5]) by smtp.gmail.com with ESMTPSA id q16sm2185377ejo.49.2020.09.01.13.53.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 01 Sep 2020 13:53:20 -0700 (PDT) To: tyson andre , PHP internals References: Message-ID: Date: Tue, 1 Sep 2020 22:53:18 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [RFC] Global functions any() and all() on iterables From: dik.takken@gmail.com (Dik Takken) On 01-09-2020 01:56, tyson andre wrote: > Hi internals, > > I've created an RFC for https://wiki.php.net/rfc/any_all_on_iterable Hi Andre, I like the RFC, it is small and the added value is clear. The RFC mentions the possibility of adding a first() method in the future. I think it would be great to generalize this to the very thing that excites me most about this RFC: It could be the starting point for ultimately arriving at a better set of tools for working with iterables. There is some discussion about the need for functions that work on keys in stead of values or on both keys and values. It was suggested to introduce option flags. It was also suggested to change the function names to allow introducing multiple variants in the future: all_values() all_keys() However, as Larry already hinted at, we may be able to do better. If we introduce a keys() function that generates the keys from a given iterable then we can use any value oriented function to work with the keys in stead. There is also the case where both the keys and values are relevant. It may be interesting to look at how Python handles this (I'm sorry, I just happen to know that language well). Python has dictionaries. Dictionaries have an items() method. This method is one of the most commonly used methods in Python. It is a generator that yields key/value pairs, one pair for each item in the dictionary. Perhaps we could do the same thing by introducing an items() function that takes an iterable and yields one array for each item in the iterable. The arrays contain the key/value pairs. Quoting the example Tyson gave: any($itemMap, fn($enabled, $itemId) => $enabled && meetsAdditionalCondition($itemId), ARRAY_FILTER_USE_BOTH) Using items(), the above can also be written as: any(items($itemMap), fn($item) => $item[0] && meetsAdditionalCondition($item[1])) This way we can do with just one any() variant and without option flags. It makes the example slightly less descriptive though, trading variable names for array indices. In Python I would probably use a comprehension here. Maybe the following will be possible in PHP at some point: any(foreach $itemMap as $enabled => $itemId yield $enabled && meetsAdditionalCondition($itemId)) Regards, Dik Takken