Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120494 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 88005 invoked from network); 31 May 2023 23:00:14 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 31 May 2023 23:00:14 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id EB5491804F8 for ; Wed, 31 May 2023 16:00:12 -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.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS19151 66.111.4.0/24 X-Spam-Virus: No X-Envelope-From: Received: from out4-smtp.messagingengine.com (out4-smtp.messagingengine.com [66.111.4.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 31 May 2023 16:00:12 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id EE7695C01BC for ; Wed, 31 May 2023 19:00:11 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute4.internal (MEProxy); Wed, 31 May 2023 19:00:11 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-type:content-type:date:date:from :from:in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm1; t=1685574011; x= 1685660411; bh=VJ3CnLw6cK/T87Vc8ZOiEyO0SWMsXGbeL4REdQ3OJGM=; b=S FzzytZwFRNyXA2JJhPnA0LlDXYGpha/RnCOOzyXJKx3znrrf77rGu7ZqGRaS4BTT zvQrpjhtJJUXowZihr1xeTGT3zgvn+HXS60cQafXz+qsOuvqsEd9tTXQjiwUoGPo OBe9EK6XQDIZbjoy4KwShmyRG4KDq9Y301Ml+bn85egEifw/Q6r6tzl5Vy9DLc6U tX8Z6WEUK5iPrjg2sAd+jkbqfnRq2msK2rciYIJrVPqVzFoiOm4R+i47OTSuESGw NTPQAuUtBucS0kRdtmk+1XJK06X2ZYlsyMZHI7iVJErkc7oIdS3gp+c8epeVau/b D5dJOl65r1obPqOWwf8GA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; t=1685574011; x=1685660411; bh=VJ3CnLw6cK/T8 7Vc8ZOiEyO0SWMsXGbeL4REdQ3OJGM=; b=L2JaeJuk7qBFhcGq0NhoJ3TKlwzP+ 0leB+nntmZGLaee02GstNGSTBlTPHYDT5jZsWHJDFh0q7rwbntDt+WcUjx27tpRa 74VPNzt8G0OJyQbtW81z/5FCis5r7LdKnCn3tZmdxwtHRmVH8YVsYYXQxPdz9lx2 PcpsTVkAf3g9bNITqwAJ+OlR1lFAB3NLGQMN0R7z2vLaAT3DtsalMAVb9f62cyss NAvNs4gCVZTIxxNNa7K30vY12dY139qWi+VC3/YrgwHkHlYWg1gFfMTY6KDfD1WC PZFGgMOWGo6wzgKR881iUq7esgfeKL0Kc7QiUh3ujRa4D/2Povt/WI4PQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrfeeltddgudejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesthdtredtreertdenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucggtffrrghtthgvrhhnpeelgeeigefgfeejueefgeeffefhgfffgfetledvleff leehkedtfffgfeehffetieenucffohhmrghinhepjhgrvhgrshgtrhhiphhtrdhnvghtne cuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheplhgrrhhr hiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 8693F1700090; Wed, 31 May 2023 19:00:11 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-447-ge2460e13b3-fm-20230525.001-ge2460e13 Mime-Version: 1.0 Message-ID: <1fcd4791-38a6-4467-8033-b0686f505af7@app.fastmail.com> In-Reply-To: <155203BE-9037-4F24-9EBC-A46EAE2ACBF5@gmail.com> References: <289E585B-EF8B-4B17-89BE-BE8295FD9FE1@gmail.com> <831B6DDD-B017-482A-9288-E31D44FC0298@gmail.com> <155203BE-9037-4F24-9EBC-A46EAE2ACBF5@gmail.com> Date: Wed, 31 May 2023 22:59:51 +0000 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] Re: [RFC] [Discussion] Add new function `array_group` From: larry@garfieldtech.com ("Larry Garfield") On Wed, May 31, 2023, at 10:47 PM, Boro Sitnikovski wrote: > 1. The grouping that JavaScript/.NET/Lodash/Scala/etc. do (this should > be the default of `array_group`) > 2. The grouping that Haskell does, the one I proposed earlier (this can > be altered in a flag within `array_group`) > > Based on this, I'd like to adjust my initial proposal, where we would > have the following function: `function array_group(array $array, > callable $callback, bool $consecutive_pairs = false): array {}` > > If the argument `consecutive_pairs` is false, it will use the > function's return value to do the grouping ($callback accepting single > element in this case) > Otherwise, it will use the function's boolean return value to check if > two consecutive elements need to be grouped ($callback accepting two > elements in this case) > > (This approach seems to be consistent with `array_filter` in the sense > the callback accepts one or two arguments) > > With a few example usages: > > ``` > var_dump( array_group($arr1, function( $x ) { > return (string) strlen( $x ); > } ) ); > // Producing ['3' => ['one', 'two'], '5' => ['three']] > ``` > > Another one: > > ``` > $arr = [-1,2,-3,-4,2,1,2,-3,1,1,2]; > > $groups = array_group( $arr, function( $p1, $p2 ) { > return ($p1 > 0) == ($p2 > 0); > } ); > // Producing [[-1],[2],[-3,-4],[2,1,2],[-3],[1,1,2]] > ``` This sounds like two separate functions in a trenchcoat. It should be two separate functions. > I believe this proposal captures many use cases, beyond the examples we > discussed. Curious about any other thoughts. > > I'm also attaching a PoC patch that implements this. > > Attachments: > * array_group.patch Side note: I don't think anyone reads patches sent to the list. I didn't even realize it allowed attachments. :-) If you want someone to review code, a GitHub PR is the way to go. You can also include benchmarks there. From experience, if you don't have a compelling reason why this *needs* to be in C rather than PHP (which in this case boils down to performance exclusively), you're not going to be able to convince people to add another random utility function. That's just the reality these days. --Larry Garfield