Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120495 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 89403 invoked from network); 31 May 2023 23:02:45 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 31 May 2023 23:02:45 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E69B41804F8 for ; Wed, 31 May 2023 16:02:44 -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_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) (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:02:44 -0700 (PDT) Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-96fe2a1db26so11372266b.0 for ; Wed, 31 May 2023 16:02:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685574163; x=1688166163; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=+vKKCuAANtuQiUkFWYQ0LyCljHHDRnVUQCgGnBDUnGk=; b=gYZpJQsOh0tfDafm1aC1NDjZwxSuO7rJpkPN9vgsxENPVlXl236d6Y80OMMpMzvsa0 oVaEcYAiSFAM2+Mi8X55j1L7AJK3UEaPamULyMR4JNgfz+61l7lfIZRYRFsehKj4cCPV dPfr2BrXHgeFzhxwTAUoh40d8TRRTw6e5JpEt8vedHtN3rfvEKdYZQ+QkVrf9Z8X9SXA gwFtRHcAuW1dhxEAFpIJhQJA33Hx7CfTxWXHj+efF8LfpCfFdxEVA5D3PnCnAZtlg6B1 nDpcC9iLI5Y20Ya8yoqK2Cdk5oQEeUCgLIxyxacfSBGerGSO+DLoWOUg8zWROoorBxpE YQtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685574163; x=1688166163; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+vKKCuAANtuQiUkFWYQ0LyCljHHDRnVUQCgGnBDUnGk=; b=ki3mUdCzehpSqJmPPAT0MHNETGSYfPza0YsLWbzM5VbNLcG1dgcssZa30F7yDj2dau HjhaLAYce2VGh6CzoWtPW4oAcpFeS0YwvCA5OQrzJTEmAgCjr4OkArtWQyyCaaouYnKN FFjMN8rExnTbxWU3aLy2SKf+w0k9HF3BxV1F6tlybrqecQvtAu4BuaSqBuyA2O4I/xYG xQNbgGFlqjZMEvwgVM9HxMkmsjDjqWJ+g+nr8edoC6zFN2fKvxO72Ux+Rptnx1CGae1p UoFupGMbXJhsYVtQM39JV2JFKxYDGENOImzHfX4a+KpaoPFVH+SrD7Oi0iaGeRTPJKw2 66uQ== X-Gm-Message-State: AC+VfDzxhrLAqyk3Dn4/By89/OjQOoKb0+Ed5blT6Suu98F9srzwg+UW 8wVdXMaQIc/KMuoELKqWj1Ml7mDO1SE= X-Google-Smtp-Source: ACHHUZ7LNriTDH353VsFHiEq9hrH/4X1ho3Iw4odxVDXlmDAGQlp3MChz4f3rWOt29K+bjb7s7vnTQ== X-Received: by 2002:a17:907:3f05:b0:974:1d8b:cd52 with SMTP id hq5-20020a1709073f0500b009741d8bcd52mr7020102ejc.9.1685574163000; Wed, 31 May 2023 16:02:43 -0700 (PDT) Received: from smtpclient.apple ([46.217.49.136]) by smtp.gmail.com with ESMTPSA id i16-20020a170906115000b009534211cc97sm9675076eja.159.2023.05.31.16.02.42 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 May 2023 16:02:42 -0700 (PDT) Message-ID: Content-Type: multipart/alternative; boundary="Apple-Mail=_AFE1DAEF-6790-41BC-9E4C-98E4F8163A5F" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.600.7\)) Date: Thu, 1 Jun 2023 01:02:31 +0200 In-Reply-To: <1fcd4791-38a6-4467-8033-b0686f505af7@app.fastmail.com> Cc: php internals To: Larry Garfield References: <289E585B-EF8B-4B17-89BE-BE8295FD9FE1@gmail.com> <831B6DDD-B017-482A-9288-E31D44FC0298@gmail.com> <155203BE-9037-4F24-9EBC-A46EAE2ACBF5@gmail.com> <1fcd4791-38a6-4467-8033-b0686f505af7@app.fastmail.com> X-Mailer: Apple Mail (2.3731.600.7) Subject: Re: [PHP-DEV] [RFC] [Discussion] Add new function `array_group` From: buritomath@gmail.com (Boro Sitnikovski) --Apple-Mail=_AFE1DAEF-6790-41BC-9E4C-98E4F8163A5F Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii > On 1.6.2023, at 00:59, Larry Garfield wrote: >=20 > On Wed, May 31, 2023, at 10:47 PM, Boro Sitnikovski wrote: >=20 >> 1. The grouping that JavaScript/.NET/Lodash/Scala/etc. do (this = should=20 >> be the default of `array_group`) >> 2. The grouping that Haskell does, the one I proposed earlier (this = can=20 >> be altered in a flag within `array_group`) >>=20 >> Based on this, I'd like to adjust my initial proposal, where we would=20= >> have the following function: `function array_group(array $array,=20 >> callable $callback, bool $consecutive_pairs =3D false): array {}` >>=20 >> If the argument `consecutive_pairs` is false, it will use the=20 >> function's return value to do the grouping ($callback accepting = single=20 >> element in this case) >> Otherwise, it will use the function's boolean return value to check = if=20 >> two consecutive elements need to be grouped ($callback accepting two=20= >> elements in this case) >>=20 >> (This approach seems to be consistent with `array_filter` in the = sense=20 >> the callback accepts one or two arguments) >>=20 >> With a few example usages: >>=20 >> ``` >> var_dump( array_group($arr1, function( $x ) { >> return (string) strlen( $x ); >> } ) ); >> // Producing ['3' =3D> ['one', 'two'], '5' =3D> ['three']] >> ``` >>=20 >> Another one: >>=20 >> ``` >> $arr =3D [-1,2,-3,-4,2,1,2,-3,1,1,2]; >>=20 >> $groups =3D array_group( $arr, function( $p1, $p2 ) { >> return ($p1 > 0) =3D=3D ($p2 > 0); >> } ); >> // Producing [[-1],[2],[-3,-4],[2,1,2],[-3],[1,1,2]] >> ``` >=20 > This sounds like two separate functions in a trenchcoat. It should be = two separate functions. >=20 >> I believe this proposal captures many use cases, beyond the examples = we=20 >> discussed. Curious about any other thoughts. >>=20 >> I'm also attaching a PoC patch that implements this. >>=20 >> Attachments: >> * array_group.patch >=20 > 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. >=20 > You can also include benchmarks there. =46rom 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. >=20 > --Larry Garfield >=20 > --=20 > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php Thank you for your thoughts. We can definitely separate them in two = functions. Also thanks on the note for the patches - will keep it in = mind :) Good point on the benchmarks, I will keep those in mind. Beyond performance, I think this has to do with developer experience = pretty much, too. Almost all mainstream languages have a built-in = grouping function, and in my honest opinion, it's time PHP has one too = :) I think we can all agree grouping is a very common/basic = functionality that we all meet with regularly.= --Apple-Mail=_AFE1DAEF-6790-41BC-9E4C-98E4F8163A5F--