Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120481 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 2877 invoked from network); 31 May 2023 00:09:52 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 31 May 2023 00:09:52 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6195318004D for ; Tue, 30 May 2023 17:09:51 -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, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,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-ed1-f43.google.com (mail-ed1-f43.google.com [209.85.208.43]) (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 ; Tue, 30 May 2023 17:09:51 -0700 (PDT) Received: by mail-ed1-f43.google.com with SMTP id 4fb4d7f45d1cf-5147f7d045bso7181422a12.2 for ; Tue, 30 May 2023 17:09:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685491789; x=1688083789; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=gWWKHqt05BSKwjjG4W/dwmTcBv9QJqtTkeVyfnpoIn0=; b=UggIZu2ATt4Vz+IMhQAlNGlzitZBR/ItmBCipihudKXjUfFMn5mNkmiN9Fpnztc8Ah HtAP2k2IJAFstPiW48j+s0e27uofAPAT9BLY5C50QWmPDAkbeGBYW/iTdPBCUBn3d4VA ZtSvuwjMMseQKLACm1Pxryh3ExJEniPIQ6PVFGMr9eYYySoSceZIHI2x4iSBaVAkpyHE 9XJLHgkWkYp/1PTLqiWblcxPrqaeu+JqSBM9Pjxcz8wMa6exfr56etiQAm5mMdKjjv5r qSxW89dvEOQShFVpv7aYxV09qO3Zd0y4v7s2CT2zJN2UMSdcz8EQ67b3nwcatmezVO6A lA0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685491789; x=1688083789; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gWWKHqt05BSKwjjG4W/dwmTcBv9QJqtTkeVyfnpoIn0=; b=fcJKKs/6idfz4OnU5MILxMxbBxP5KKOx6y3SKxGHzpY5xixeUBFzWGii3nyi/uthZl LiEveSOBf408jP3r1f5GCd4ZPi3r18nlHZKRqiwif4dFycJX29QUpDiJ9NTEnVTEuqmi Tphifho46iMXGwfqesXGuQEORA/L+voH+KyZrvHZK/hYPNm7vrlRViEjh5OOZy/YqMWL LfAnqLQCxNK7zICU+dG4HIhCCE/S7VCCE0xwGYEoUlxbQfP/ng3dHjHUUJUyQa4kQc0d ns9Sk5MQld75DjSsAXTnae1Ga5UhMk0/e9m9gOGPusmvAhycDhSeybU8Xv1OKRf461Uq hpsQ== X-Gm-Message-State: AC+VfDxW9n9PfHAHMHz0CNd5sDgaHPe9maVj0O+B/tkTxl9CA3saWK4q fPzHIbKma942o0HHs8jAX0RQqjXarqg= X-Google-Smtp-Source: ACHHUZ7jNjYfNjt9zRFv9KCBqzuuN6B8xFMaK9a4KIiYFW4MGz2ijcBaWNAbURjmpnZPuRLTWcOyEw== X-Received: by 2002:a17:907:c0c:b0:973:edba:df30 with SMTP id ga12-20020a1709070c0c00b00973edbadf30mr3991527ejc.61.1685491789209; Tue, 30 May 2023 17:09:49 -0700 (PDT) Received: from smtpclient.apple ([46.217.211.121]) by smtp.gmail.com with ESMTPSA id w22-20020a170906131600b00965e1be3002sm8011255ejb.166.2023.05.30.17.09.48 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 30 May 2023 17:09:48 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.600.7\)) In-Reply-To: Date: Wed, 31 May 2023 02:09:36 +0200 Cc: php internals Content-Transfer-Encoding: quoted-printable Message-ID: <5140E8C4-A27B-40C0-82F2-A73700A3D3A0@gmail.com> References: <289E585B-EF8B-4B17-89BE-BE8295FD9FE1@gmail.com> <4CA1668E-A342-452E-A994-5839C377CB27@gmail.com> <9D69B8D4-835A-47C0-A3BD-64285849BB02@gmail.com> To: Andreas Hennings 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) > On 30.5.2023, at 21:56, Andreas Hennings wrote: >=20 > On Tue, 30 May 2023 at 19:25, Boro Sitnikovski = wrote: >>=20 >> Here's some more examples: >>=20 >> 1. Use `array_group` to create list of singleton list: >> ``` >> $groups =3D array_group( $arr, function( $p1, $p2 ) { >> return false; >> } ); >> ``` >>=20 >> (This can also be achieved with `array_map` returning `[ $x ]`) >>=20 >> 2. Distinct groups for consecutive positive and negative elements >>=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); >> } ); >> ``` >>=20 >> This produces `[[-1],[2],[-3,-4],[2,1,2],[-3],[1,1,2]]`, so we can = easily capture the groups of highs/lows for example. >>=20 >> 3. Group sentences (similar to `explode`, but still different) >>=20 >> ``` >> $arr =3D "Hello, PHP. Good to see you."; >> $groups =3D array_group( str_split( $arr ), function( $p1, $p2 ) { >> return '.' !=3D=3D $p1; >> } ); >>=20 >> $groups =3D array_map( 'join', $groups ); >> ``` >>=20 >> Producing `[ "Hello, PHP.", " Good to see you." ]`. >>=20 >> 4. Grouping book sections >> ``` >> $book_sections =3D [ '1.0', '1.1', '1.2', '2.0', '2.1', '3.0', '3.1' = ]; >> $groups =3D array_group( $book_sections, function( $p1, $p2 ) { >> return $p1[0] =3D=3D=3D $p2[0]; >> } ); >> ``` >>=20 >> Producing `[ [ '1.0', '1.1', '1.2' ], [ '2.0', '2.1'], [ '3.0', '3.1' = ] ]` >>=20 >> and so on... >>=20 >> Basically, it's a very general utility :) >>=20 >> Best, >>=20 >> Boro >=20 > Only one of these examples actually uses a comparison function that > actually compares two adjacent values. > All the others look at a single value only, or they don't care about > the original order at all. >=20 > And that example (2) seems quite artificial and rare. >=20 >> so we can easily capture the groups of highs/lows for example >=20 > How often does this problem come up in practice? >=20 > I did actually have a look at the Haskell doc page you linked. > = https://hackage.haskell.org/package/groupBy-0.1.0.0/docs/Data-List-GroupBy= .html > I wonder where this is used in the Haskell world, and how often they > need this true comparison of adjacent values. > The examples from the doc page are quite artificial. That's ok for a > doc page, but does not make a good argument for why we need it. Thanks for digging deeper in trying to understand the value that this = would bring. For a more realistic example, check the following gist: = https://gist.github.com/bor0/b5f449bfe85440d96abd933b9f03b310#file-test_da= tetime_group-php. This was a very recent thing I worked on in = production. The events came ordered from the database. But also, this is = not the first time I was in need of a grouping functionality. Sorry for repeating, but I see it as a handy utility. We can bring the = same argument for any of the `array_*` functions because most of them = can be implemented with for loops. If the main concern is the consecutive processing of the elements, we = could add a third argument to `array_group` (flag) that would allow = users to alter the processing behaviour - at a price, consecutive taking = O(n) vs all elements taking something close to O(n^2)=