Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111721 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 68946 invoked from network); 30 Aug 2020 15:45:36 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Aug 2020 15:45:36 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C18621804D9 for ; Sun, 30 Aug 2020 07:49:46 -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.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from out1-smtp.messagingengine.com (out1-smtp.messagingengine.com [66.111.4.25]) (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 ; Sun, 30 Aug 2020 07:49:46 -0700 (PDT) Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id B09A75C0180 for ; Sun, 30 Aug 2020 10:49:44 -0400 (EDT) Received: from imap26 ([10.202.2.76]) by compute7.internal (MEProxy); Sun, 30 Aug 2020 10:49:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=v5/mq8 UJXTeH0BKrS33ITq/By9DJGMbJS1Rf3F1XiyA=; b=eMQxWIBsw3XM1GQtDNhL9h VeVODdMO6WixTk+f0mvzYH6gO2I1KbT1wk+5SBY1ZInYlR9bpf2DMLG+eBE92Sg5 g1f7tKdrV1yw4xSKKaxKykwnh98hBLT5hTmvzOgUDZHxttAhHMJASE8A9LzSkznA ZA9jELHGDM0/NdxlgzQWyd3/jpdL+EoaQ7QIxUBA9yzHFji+xz3HE5a9t4xurcCb CYFHQhlgCR1qXVodTKk4fzjaM0wtFwb8O6pOa6JUYhUGe4DSOJao2/9CkW2QRwkV /XUpLbqa0BXWsuN22H+xOh53pNlfmuNzIrbVxm/5sfK5vK/sEm3WmTqWiY6yVPHQ == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduiedrudeffedgkedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesthdtredtreerjeenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucggtffrrghtthgvrhhnpeelkeehlefftefhheefgeelieehleeuhfehvddtvdfg veetteekiedukeeileeukeenucffohhmrghinhephhgrshhkvghllhdrohhrghdpmhhoii hilhhlrgdrohhrghdpphihthhhohhnrdhorhhgpdhorhgrtghlvgdrtghomhdpghhithhh uhgsrdgtohhmpdgvgihtvghrnhgrlhhsrdhiohenucevlhhushhtvghrufhiiigvpedtne curfgrrhgrmhepmhgrihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdr tghomh X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 478D214200A2; Sun, 30 Aug 2020 10:49:44 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.3.0-232-g4bdb081-fm-20200825.002-g4bdb081a Mime-Version: 1.0 Message-ID: <7d91bd8f-9ca0-4e62-886b-f89bc8edd3e9@www.fastmail.com> In-Reply-To: References: Date: Sun, 30 Aug 2020 09:49:23 -0500 To: "php internals" Content-Type: text/plain Subject: =?UTF-8?Q?Re:_[PHP-DEV]_Proposal:_Adding_functions_any(iterable_$input,_?= =?UTF-8?Q?=3Fcallable_$cb_=3D_null,_int_$use=5Fflags=3D0)_and_all(...)?= From: larry@garfieldtech.com ("Larry Garfield") On Sat, Aug 29, 2020, at 3: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 I like this, but I do not like the flags. I don't think they're at all useful. A lot of the other discussion in the thread seems to be needlessly complicating it, too. all() and any() only need return booleans. Their callbacks only need return booleans. That's the point. first() makes sense to add, and it would return the first value that matches. For the callback itself, there is work to, hopefully, add partial function application to 8.1. (No idea if it will be successful, but the effort is in progress.) If so, the upshot is that turning an arbitrary function into a single-parameter function becomes silly easy, which means functions like this can just expect a single parameter callback and be done with it. No need for extra-args or flags or whatnot. If you want to check the keys of an array, call array_keys() first and use that. if (any(array_keys($foo), fn($k) => $k %2)) { ... } all(), any(), and first() all sound like good things to include, but let's not over-complicate them. We can do better today than we could in 1999... --Larry Garfield