Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120462 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 58345 invoked from network); 30 May 2023 16:22:04 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 May 2023 16:22:04 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9301D1804DF for ; Tue, 30 May 2023 09:22:03 -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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE, 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-yb1-f175.google.com (mail-yb1-f175.google.com [209.85.219.175]) (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 09:22:03 -0700 (PDT) Received: by mail-yb1-f175.google.com with SMTP id 3f1490d57ef6-b9a7e639656so8548706276.0 for ; Tue, 30 May 2023 09:22:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20221208.gappssmtp.com; s=20221208; t=1685463722; x=1688055722; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=gD6X+1bCJuxj53j4+UlUzAddNzcPVeOsnVIP7yQk2LU=; b=FWKWP6qK1/1kjIv4zuCj+WbsrINg6eLqUaJJakRgTwt4VlpKzgJpbppJ0GzscI3a9u QVZAE6h99VXIKzKB78x/zZnxGAdI948GMsYTBuqOB+mhiuTfOndV4zoDC7c1T7WlrWK5 eI5CrgWTk2xlHbI7XSRlUWJgFkH1BSOAI6OX9tqonqnREvxlKvpfLEfD5woJ2uwASjRi yJbm2sfcyI+PhAUXmhPL8cRH48NjmdS/gS74/W4C6QBz9z+CXB6OSkgf6llCILEoXVAk oYI4SIPcT7xDtwtV01EPMMoPt1bjUWUKaq/b8eBB8w3u5Dz+dRGB99obugfubbuiWunV xn8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685463722; x=1688055722; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=gD6X+1bCJuxj53j4+UlUzAddNzcPVeOsnVIP7yQk2LU=; b=Fs13kRDgHSiyEdTHqjb2+L3bsfcS2MniWKg4APcU+7R5hbxkmScdb9vAX0NaYeA1wT xB1lUVxR4crStIHOLo6HMNDc3E94hoHyNbUAag9c/91CZijWFq82qwFdbLmXW3s3O3Vz h9r3JojgaCNmA6d3lPZv2df17J4fB1BCFwAgGrAPdSiGUm3GSte/rb3e2GknQpPephW7 HlVTvNfOXXGEIIDKGx4Bz+yOkwm8626yFIDPtqccgW777ecucHvVzZjLzLnMyxDaxBZv cgul1vHAeS5sHIZH0qYFIa8Z0wWGSus2R9ey0mW9NlKWkyX4e6hKVkpvxVQLnSewP9os zRUA== X-Gm-Message-State: AC+VfDyjM7r2NaA3uJ7PUY8RuoB4x3oqOYWpjlFC1yE3NglWKtJifyAd TVB90eHLqYP7iUsdZd/VpaMT3kS847R54H6NimzFgg== X-Google-Smtp-Source: ACHHUZ5vJNFrjCV1nDJBm1/h1ssvdQsmJO6dr3MyI8EAs0PhvmnCJs/QizN9U2rVdVBNFXWyQckduAGbQyloIkab/o4= X-Received: by 2002:a81:bf44:0:b0:561:b26c:40c4 with SMTP id s4-20020a81bf44000000b00561b26c40c4mr2318135ywk.51.1685463722437; Tue, 30 May 2023 09:22:02 -0700 (PDT) MIME-Version: 1.0 References: <289E585B-EF8B-4B17-89BE-BE8295FD9FE1@gmail.com> <4CA1668E-A342-452E-A994-5839C377CB27@gmail.com> In-Reply-To: <4CA1668E-A342-452E-A994-5839C377CB27@gmail.com> Date: Tue, 30 May 2023 18:21:51 +0200 Message-ID: To: Boro Sitnikovski Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC] [Discussion] Add new function `array_group` From: andreas@dqxtech.net (Andreas Hennings) Thank you, this clarifies and it confirms my initial assumption of what you are proposing. So you want to slice an array by comparing adjacent values. My personal feedback: I think the need for the grouping behavior you describe is not common enough that it needs its own native function. I would say the more common desired behavior is the one in your first example. And even for that we don't have a native function. Your behavior can be implemented in userland like so: https://3v4l.org/epvHm $arr1 = array(1,2,2,3,1,2,0,4,5,2); $groups = []; $group = []; $prev = NULL; foreach ($arr1 as $value) { if ($group && $prev > $value) { $groups[] = $group; $group = []; } $group[] = $value; $prev = $value; } if ($group) { $groups[] = $group; } print_r($groups); If needed, the comparison function can be separated out and passed as a parameter. So the array_group() function with a comparison callback parameter can be implemented in userland. I think you need to make a case as to why the behavior you describe justifies a native function. E.g. if you find a lot of public php code that does this kind of grouping. I personally suspect it is not that common. Cheers Andreas On Tue, 30 May 2023 at 17:08, Boro Sitnikovski wrote: > > Hey, > > Thanks for the suggestion. > > For the previous case in the code, I added these in a Gist to not clutter here too much: > > 1. The first example corresponds to https://gist.github.com/bor0/b5f449bfe85440d96abd933b9f03b310#file-test_manual_group-php > 2. The second example corresponds to https://gist.github.com/bor0/b5f449bfe85440d96abd933b9f03b310#file-test_array_group-php > 3. Another example, addressing the problem of increasing subsequences is very simple with `array_group`: https://gist.github.com/bor0/b5f449bfe85440d96abd933b9f03b310#file-test_array_incr_subseqs-php > > Best, > > Boro > > > On 30.5.2023, at 16:57, Andreas Hennings wrote: > > > > Hello Boro, > > I think you should include the "expected result" in your code examples. > > Maybe this is in your patch file, but I don't think we want to look at > > that for discussion. > > > > Cheers > > Andreas > > > > On Tue, 30 May 2023 at 13:35, Boro Sitnikovski wrote: > >> > >> Hello all, > >> > >> As per the How To Create an RFC instructions, I am sending this e-mail in order to get your feedback on my proposal. > >> > >> I propose introducing a function to PHP core named `array_group`. This function takes an array and a function and returns an array that contains arrays - groups of consecutive elements. This is very similar to Haskell's `groupBy` function. > >> > >> For some background as to why - usually, when people want to do grouping in PHP, they use hash maps, so something like: > >> > >> ``` > >> >> $array = [ > >> [ 'id' => 1, 'value' => 'foo' ], > >> [ 'id' => 1, 'value' => 'bar' ], > >> [ 'id' => 2, 'value' => 'baz' ], > >> ]; > >> > >> $groups = []; > >> foreach ( $array as $element ) { > >> $groups[ $element['id'] ][] = $element; > >> } > >> > >> var_dump( $groups ); > >> ``` > >> > >> This can now be achieved as follows (not preserving keys): > >> > >> ``` > >> >> $array = [ > >> [ 'id' => 1, 'value' => 'foo' ], > >> [ 'id' => 1, 'value' => 'bar' ], > >> [ 'id' => 2, 'value' => 'baz' ], > >> ]; > >> > >> $groups = array_group( $array, function( $a, $b ) { > >> return $a['id'] == $b['id']; > >> } ); > >> ``` > >> > >> The disadvantage of the first approach is that we are only limited to using equality check, and we cannot group by, say, `<` or other functions. > >> Similarly, the advantage of the first approach is that the keys are preserved, and elements needn't be consecutive. > >> > >> In any case, I think a utility function such as `array_group` will be widely useful. > >> > >> Please find attached a patch with a proposed implementation. Curious about your feedback. > >> > >> Best, > >> > >> Boro Sitnikovski > >> >