Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111712 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 6236 invoked from network); 29 Aug 2020 21:35:24 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Aug 2020 21:35:24 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5C39F1804AA for ; Sat, 29 Aug 2020 13:39:22 -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-f173.google.com (mail-lj1-f173.google.com [209.85.208.173]) (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 ; Sat, 29 Aug 2020 13:39:21 -0700 (PDT) Received: by mail-lj1-f173.google.com with SMTP id e11so2564384ljn.6 for ; Sat, 29 Aug 2020 13:39:21 -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=56BbZt2td7PZneDjuEJLADGodOunvpPRzemL+6EqRe8=; b=GmZc/d2RSCgsfeqUPwy7zsU9iDfGH6q+uCZdBkHgPAFPbmaHkrg8HXSX1rTdAI4HMG M2EqmHubaGI4KvauW4GWnY7y19wc5FE1CxLrzdvJSwVrUJgB+SqJ6fd8yiHn2FsS9JCn oJJmY4OFqEph8qkgXvX7PFMFkzYzQl9wibH7K2tBMohQlIGwBAaxnwnbNc0zINfF1UDU 2pwwyQDGfoewCEwD58qDo1NDou++j3kY0B/b2lF7cUgzGIbTfM4YWvHkgd8GUa/j5AmJ fotytq5F00v/UGdNH4oRoiLN5NlIiCMhqtiHjV4zxJwAioLDcDYsp67NwOWw1uj/XGjW T/eg== 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=56BbZt2td7PZneDjuEJLADGodOunvpPRzemL+6EqRe8=; b=eoBdRSWPyGkDJM70X/BikQfUqo/TBQ+8AJzYsWUH/2wmTDmfX7NPp4WXKagcfe5c5w HXLVq2/A8htEitjYzjnhKXnWO6e8n8AFzXt1B4T+PbumY+55NyhAewCwUDK5dOnOLG7t RtboA9Cc4Yh+WozQgwKkAf4A2SOf0ZmCtxrCyc5oF4yKRV2NwoeRls3wtV1qywXzHzSf PGGTnSTuKb9QX5uZx1HX/jP4YzBUK6nZrrWaMPWpJaAzp2876qLMELLZiOV7E5gFF+01 DdZ3F9vQE+wKvEA+Bsg2nY71ASeV1/aX3YXJMU6hX5e28frwp/s3QALq4TAIN/hY7Iq1 lRhg== X-Gm-Message-State: AOAM533w1jnAxw9B/M27rJ99PMHFvsnr5RxKy/S7KEgN9/Nd+zEVRYqT uhUSNrS8zIpRV3wt2UkVJI9b009y62oVi2iGiyA= X-Google-Smtp-Source: ABdhPJw1Z1/AL2u4wNM5gksej12lKC0PGjmgdFEr3LCzTk+JpdVhPPCaiaanMsErpto00LRzqA+jLoDSWCNAryovNh4= X-Received: by 2002:a05:651c:c6:: with SMTP id 6mr2149113ljr.237.1598733559850; Sat, 29 Aug 2020 13:39:19 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sat, 29 Aug 2020 22:39:03 +0200 Message-ID: To: tyson andre Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000ecea6805ae0a290f" Subject: Re: [PHP-DEV] Proposal: Adding functions any(iterable $input, ?callable $cb = null, int $use_flags=0) and all(...) From: alexinbeijing@gmail.com (Alex) --000000000000ecea6805ae0a290f Content-Type: text/plain; charset="UTF-8" I like it! What is the $use_flags parameter for? On Sat, Aug 29, 2020 at 10:24 PM tyson andre wrote: > Hi internals, > > 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 ($items 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($items, fn($x) => API::satisfiesCondition($x))) { > 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(). > > Previous discussion was in https://externals.io/message/103357#103373 > > - New contributors to projects wouldn't know about any() and all() if it > was reimplemented with different semantics and only occasionally used > (e.g. MyArrayUtil::any()) in various projects) > - If this was provided only in userland, there'd be low adoption and code > such as the first example would remain common. > If the standard library provided it, then polyfills would as well, > making cleaner code easier to write. > > Thanks, > - Tyson > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > --000000000000ecea6805ae0a290f--