Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111758 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 57424 invoked from network); 1 Sep 2020 02:50:37 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 1 Sep 2020 02:50:37 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1AC7D1804D9 for ; Mon, 31 Aug 2020 18:55:10 -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-vs1-f43.google.com (mail-vs1-f43.google.com [209.85.217.43]) (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 ; Mon, 31 Aug 2020 18:55:09 -0700 (PDT) Received: by mail-vs1-f43.google.com with SMTP id x203so3355199vsc.11 for ; Mon, 31 Aug 2020 18:55:09 -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=NjSIhXNgT655U7xQcKDTfLTdkxm/UHY6l/DLiixo/no=; b=aqkBjMTde8wYS4axo1vOF0Qz+hnyfUK/6SUp4f29dmkuY0ZtUvKWwOjboJjdAceaEv KoWA2lzUIibc+/vi3uPg+FQHJUpHyUANICzHLRV5Mm8ijJiMJkGSb+YvnRTPxOmntOED mU6JcJvXoc12ORdeBPv3u/RGwqIEXB/+XRIbKOTv/LGQ3F1zmpQGuAzniQ83zUe/OKZf CidDV612ACDeQ8ONDphHBXc1DfoS7WBluV4i7r0Km+pkIW+zgHkINu4DRilLrp6mSNRY HEybrJGB2crHR0f2Rxqh7PzSwfr/yFaNd8tSZ3WuORheRxwlL1FMRUcZNQnP9+vF3/gb 3vrw== 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=NjSIhXNgT655U7xQcKDTfLTdkxm/UHY6l/DLiixo/no=; b=dvBfm5rrHv7e97zzigZ76j6BSqW9tyKCGC1/jfaxA+lQdLnJPhLtv/5CXH18pCRcg3 UqfQXUdM7AORDIYl62BpEL8YJt+rW3YgvtxNMFmPSBK6I2ELwH1TFXNCkmKeT8PY3feJ +ZOqwzpos+SR1kiDYacOkj5oI8AZr+oS52vtqIYWwfC/Vx4GvnbO4HvTgWq90Z487qv7 drCp4wxonFDnWr3Yr9TuAl9ix1cU5nSfb3XLBjSqDudHXjsRqlElm28ClNdLAWhHcDJk /5Ar+iyZTGlI/z/PefU4P1xBZrNKoCiHjO7jAxGv1vblJBMwwTMMsU/DCtralZBIbwoE pzCA== X-Gm-Message-State: AOAM531E0ytGQkIeyOC0CwqtfcMbL9gWWYOPlZ0NF53dAV8pW1yVeF/r fS3SLY/dmHiS8NbrwfqAD/ZWxQh+fbeavVPFNUE= X-Google-Smtp-Source: ABdhPJz1uboTE7ZNhga2fG0ct40eI3HIU2NdevhGWxWAxFiuCtF7Finf+NZV5O32+P5XMIE3h0SJmyAQEpc3XWBHXZA= X-Received: by 2002:a67:7c4e:: with SMTP id x75mr3544399vsc.60.1598925307863; Mon, 31 Aug 2020 18:55:07 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 31 Aug 2020 21:54:56 -0400 Message-ID: To: tyson andre Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000ff65ef05ae36ceb4" Subject: Re: [PHP-DEV] [RFC] Global functions any() and all() on iterables From: matthewmatthew@gmail.com (Matthew Brown) --000000000000ff65ef05ae36ceb4 Content-Type: text/plain; charset="UTF-8" This would be a fantastic addition, and it would also alleviate an issue in static analysis land where it's very tricky (in the general case) to verify exactly what implications successfully completing a given foreach loop has on the array being iterated over (e.g. https://github.com/vimeo/psalm/issues/649). Using this function would make user intent much clearer to static analysis tools, and also (as RFC describes) increase code legibility. On Mon, 31 Aug 2020 at 19:56, 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 > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > --000000000000ff65ef05ae36ceb4--