Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120465 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 62376 invoked from network); 30 May 2023 16:27:26 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 May 2023 16:27:26 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D04C6180538 for ; Tue, 30 May 2023 09:27:25 -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-f42.google.com (mail-ed1-f42.google.com [209.85.208.42]) (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:27:25 -0700 (PDT) Received: by mail-ed1-f42.google.com with SMTP id 4fb4d7f45d1cf-51458187be1so8203814a12.2 for ; Tue, 30 May 2023 09:27:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685464044; x=1688056044; 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=x786QLh0ISb0v6TosNZwmndTqTztpSW8oQMw9JOcP20=; b=muO5p1ZwY2xjQWI2lSn99UoYM2zRrK+/Pq8TBnrHWVjyZcXmZIKroX7C3eOCGR8oIV DHFznOryExQpQCNBVaheGzsMT/JgBIHOhbfwjAVi4vDPNYI5yoe+jW2xxmg5N3o/2bpy JaORcz0U9vjV5xPc9qcKKC7C11M7QYPMdVdD/K+vodGG2Cs+mA+/1//4QG2hiRyjdFYG qz82Gw/uw6ROZueBZmLYZTngU4+mvH0E9sODsLpCiczZ3iPjmRGRkYtwAxf0UmKoNlX+ GTanoois5ykWqfnvZ5M1rUJ/40RyENpn75vR2mntKg35MtNuT4ElBvm+v2RIDaur3ERN IxbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685464044; x=1688056044; 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=x786QLh0ISb0v6TosNZwmndTqTztpSW8oQMw9JOcP20=; b=HX+4Lj9fhy0TvcWHqeYt8a5MaOKzOXiLfqfYIcNukxYpKrFpTnIkoFRMjxa4L6SQCP gWEIda5i7dmtHrxUuwYFU8yJClOHHrJSWRFuv5kbdeSRglYXUCNBzdsRDgFifrq62f3q HVBp+iZJeJqolKoEq/3gFmLUXVVPiCJv8sYAtnSjQEDtREUyIwVVvdoWpl5ql1k4XCZG AMp3vl6zoS6VTjKIsHlYleSL/Bveg3LfF9sTU7xXou7OcwFcyVDUJjQpyrhmOU6Vlpv9 w82l/LayWDtd39sWBBQCIYa05Aau6DMLvwM3quUOro/8Es+nA7SiJvjUvkPTRcbD8HiM xxbw== X-Gm-Message-State: AC+VfDwXO9tjIMmhBxuqJ2Cv5Cz/Sc2W86d5fs0k/jKUBa/5ZdZ+d4t/ e2+dESW+ChZJMOMzvwW+bxM1BpOVJPo= X-Google-Smtp-Source: ACHHUZ52hg9hdSZis9KQ6Sf3AIL6YQ1O1dMcyzrxu/TV7KKTXNuQGn7wtoQ//HWs3w9eVMsWkKyV2g== X-Received: by 2002:a17:907:97d3:b0:94e:5679:d950 with SMTP id js19-20020a17090797d300b0094e5679d950mr2866739ejc.72.1685464043816; Tue, 30 May 2023 09:27:23 -0700 (PDT) Received: from smtpclient.apple ([46.217.211.121]) by smtp.gmail.com with ESMTPSA id va15-20020a17090711cf00b0094ee99eeb01sm7571773ejb.150.2023.05.30.09.27.23 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 30 May 2023 09:27:23 -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: Tue, 30 May 2023 18:27:12 +0200 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: References: <289E585B-EF8B-4B17-89BE-BE8295FD9FE1@gmail.com> <4CA1668E-A342-452E-A994-5839C377CB27@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) Hi, Thank you for your thoughts. > 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. This Google search might give more insight into the number of = discussions about a grouping functionality: = https://www.google.com/search?q=3Dphp+group+elements+site:stackoverflow.co= m > Your behavior can be implemented in userland like so: > https://3v4l.org/epvHm Correct, but then again, we can also implement = `array_map`/`array_filter`/etc. in userland :) > I think you need to make a case as to why the behavior you describe > justifies a native function. Similar to my previous answer, but also in general - ease of access and = also performance. > E.g. if you find a lot of public php code that does this kind of = grouping. >=20 > I personally suspect it is not that common. >=20 > Cheers > Andreas >=20 >=20 > On Tue, 30 May 2023 at 17:08, Boro Sitnikovski = wrote: >>=20 >> Hey, >>=20 >> Thanks for the suggestion. >>=20 >> For the previous case in the code, I added these in a Gist to not = clutter here too much: >>=20 >> 1. The first example corresponds to = https://gist.github.com/bor0/b5f449bfe85440d96abd933b9f03b310#file-test_ma= nual_group-php >> 2. The second example corresponds to = https://gist.github.com/bor0/b5f449bfe85440d96abd933b9f03b310#file-test_ar= ray_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_ar= ray_incr_subseqs-php >>=20 >> Best, >>=20 >> Boro >>=20 >>> On 30.5.2023, at 16:57, Andreas Hennings = wrote: >>>=20 >>> 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. >>>=20 >>> Cheers >>> Andreas >>>=20 >>> On Tue, 30 May 2023 at 13:35, Boro Sitnikovski = wrote: >>>>=20 >>>> Hello all, >>>>=20 >>>> As per the How To Create an RFC instructions, I am sending this = e-mail in order to get your feedback on my proposal. >>>>=20 >>>> 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. >>>>=20 >>>> For some background as to why - usually, when people want to do = grouping in PHP, they use hash maps, so something like: >>>>=20 >>>> ``` >>>> >>> $array =3D [ >>>> [ 'id' =3D> 1, 'value' =3D> 'foo' ], >>>> [ 'id' =3D> 1, 'value' =3D> 'bar' ], >>>> [ 'id' =3D> 2, 'value' =3D> 'baz' ], >>>> ]; >>>>=20 >>>> $groups =3D []; >>>> foreach ( $array as $element ) { >>>> $groups[ $element['id'] ][] =3D $element; >>>> } >>>>=20 >>>> var_dump( $groups ); >>>> ``` >>>>=20 >>>> This can now be achieved as follows (not preserving keys): >>>>=20 >>>> ``` >>>> >>> $array =3D [ >>>> [ 'id' =3D> 1, 'value' =3D> 'foo' ], >>>> [ 'id' =3D> 1, 'value' =3D> 'bar' ], >>>> [ 'id' =3D> 2, 'value' =3D> 'baz' ], >>>> ]; >>>>=20 >>>> $groups =3D array_group( $array, function( $a, $b ) { >>>> return $a['id'] =3D=3D $b['id']; >>>> } ); >>>> ``` >>>>=20 >>>> 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. >>>>=20 >>>> In any case, I think a utility function such as `array_group` will = be widely useful. >>>>=20 >>>> Please find attached a patch with a proposed implementation. = Curious about your feedback. >>>>=20 >>>> Best, >>>>=20 >>>> Boro Sitnikovski >>>>=20 >>=20