Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111760 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 79359 invoked from network); 1 Sep 2020 08:26:31 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 1 Sep 2020 08:26:31 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9DDA4180538 for ; Tue, 1 Sep 2020 00:31:03 -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,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lj1-f177.google.com (mail-lj1-f177.google.com [209.85.208.177]) (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 00:31:03 -0700 (PDT) Received: by mail-lj1-f177.google.com with SMTP id w3so300132ljo.5 for ; Tue, 01 Sep 2020 00:31:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=ssRjwg+gtXbnq0hvfwFezdkSK5FoAqzkQBRrxB0/PUk=; b=cM5P6AJMGnAZTuNqkJR+uXIdLovHtd/Csp0KH8VKqmoWj/dQ1GWYka+0LTIWm8uugx /XSa6CWDZvqr6O5QtQ0Wg9k5MQNP8E0/AUPAl1UgUihly5Dboc9eA9efm7OOz24QSXQw uIvhRvCqV5GEtq2yYclZ2zi71u0TnYm16fc25XooILw46YrjqemRn4DRBaoNRFNHza6w LAQBDLU9d71UqhjGsyr1gIUfE4N0FLaxvyMj4qwqR0q9yWeU3nv9N/cGOSMeCT2R993q k0FoVZl/QZnG7m4l0VCyZVO+sDkf/uWt2yDof36oHwTA7dJRxo5LNO2CUtEeBu0VFB3j tbEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=ssRjwg+gtXbnq0hvfwFezdkSK5FoAqzkQBRrxB0/PUk=; b=hfUOC8EWJZ0bMfzEpE6PSod6NLREKMXrXd/rQJnW8NQ8xcOJoqH6RnbqDoN0hv0DWv IE7EllKrUn5ufaA43wbTJcPrHuNvn5p7I18LImTms2eCAS0S6BqZgzE1jYRL7H/HCx7D m1qa/49oLPaeZpBxpTdiMTRw32wKe33E7rHWFfLTaDPQNf+chAfBcrMUYxEb5Umma3e3 3xZGyrll1v9Db4i6h4jXPWU4PuEHqOgt2Wtta6Yui73spdLP3GnqHWRdq1NCDNiXVVwt Gc6G1trlYO+hW35DMXEFUEXdNU9XLYOPt12XQG7uKeZK1Hl+0PF8UB+9QbstpgCUrNf+ m/KQ== X-Gm-Message-State: AOAM530nQYVGl5T/HWxwn93OjoxvIvjT2zRl6lML4DFjStfwoqsqsLyb i/bseNwTIvv/apioWwie92ZxrIWMRPqTi6GHWYU= X-Google-Smtp-Source: ABdhPJyCxzhqzMzdwenawRn+GBM0dtcQZySEwYzEhNwqVdi7ZljeHBRxHwWv45ATgVR61IshPBEfgF/7SGrGSa656s0= X-Received: by 2002:a05:651c:290:: with SMTP id b16mr279591ljo.307.1598945461406; Tue, 01 Sep 2020 00:31:01 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 1 Sep 2020 09:30:45 +0200 Message-ID: To: tyson andre Cc: PHP internals Content-Type: multipart/alternative; boundary="0000000000003e0e4205ae3b809d" Subject: Re: [PHP-DEV] [RFC] Global functions any() and all() on iterables From: nikita.ppv@gmail.com (Nikita Popov) --0000000000003e0e4205ae3b809d Content-Type: text/plain; charset="UTF-8" On Tue, Sep 1, 2020 at 1:56 AM tyson andre wrote: > Hi internals, > > I've created an RFC for https://wiki.php.net/rfc/any_all_on_iterable > > This was proposed 2 days ago in https://externals.io/message/111711 with > some interest > ("Proposal: Adding functions any(iterable $input, ?callable $cb = null, > int $use_flags=0) and all(...)") > > - The $use_flags parameter was removed > > The primitives any() and all() are a common part of many programming > languages and help in avoiding verbosity or unnecessary abstractions. > > - > https://hackage.haskell.org/package/base-4.14.0.0/docs/Prelude.html#v:any > - > https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some > - https://docs.python.org/3/library/functions.html#all > - > https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#allMatch-java.util.function.Predicate- > > For example, the following code could be shortened significantly > > ``` > // Old version > $satisifes_predicate = false; > foreach ($item_list as $item) { > if (API::satisfiesCondition($item)) { > $satisfies_predicate = true; > break; > } > } > if (!$satisfies_predicate) { > throw new APIException("No matches found"); > } > > // New version is much shorter and readable > if (!any($item_list, fn($item) => API::satisfiesCondition($item))) { > throw new APIException("No matches found"); > } > ``` > > That example doesn't have any suitable helpers already in the standard > library. > Using array_filter would unnecessarily call satisfiesCondition even after > the first item was found, > and array_search doesn't take a callback. > > A proposed implementation is https://github.com/php/php-src/pull/6053 - > it takes similar flags and param orders to array_filter(). > > Thanks, > - Tyson > To be in line with naming conventions, I would suggest calling these iter_any() and iter_all(), using iter_* as the prefix for our future additions to the "functions that work on arbitrary iterables" space. iterable_any() and iterable_all() would work as well, though are unnecessarily verbose. Regards, Nikita --0000000000003e0e4205ae3b809d--