Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120466 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 63922 invoked from network); 30 May 2023 16:34:03 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 May 2023 16:34:03 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BE071180551 for ; Tue, 30 May 2023 09:34:02 -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:34:02 -0700 (PDT) Received: by mail-yb1-f175.google.com with SMTP id 3f1490d57ef6-ba82059ef0bso3799553276.1 for ; Tue, 30 May 2023 09:34:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20221208.gappssmtp.com; s=20221208; t=1685464441; x=1688056441; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=US8eidKNJL+XD8KtvLTz3yKSZaVNcD7RUNxUFnLDM40=; b=BIowYZFYMiwTPQycJ4X4KeMKyvQCmP70nTNtrAuSEXP6VYIm//PQMnWNa1GPIj7GT8 x53ImoicyBkbFsglo1vZfhQR9iT9Z7KmHoJugkTrGsz6uviGuAWNNgCceMX8myBTj6A6 +D89rs6F9sqhcILSrCfT1Ph+iuZuFuo7GseA+HIq6dPTGcs+Jk0cWltJ7BLxmROGO+De hqH/UoMP8sbgosXlXGnagRbjPcXg3D/iQcoaKq7jmniHhza1yC1MRWHoVhxNQ87nrf8X xnbyKHU4fps2a7vAKCcUShuEAmiLTyhAqx/fKSRGWCrzNqhSzq8wJRwr+c+etYlVwZ2L HpxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685464441; x=1688056441; 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=US8eidKNJL+XD8KtvLTz3yKSZaVNcD7RUNxUFnLDM40=; b=HbtzHgt43gBdJuSRxeWCoppnFYjykZzsmpKOofBtVCOiDabED2G322H3U4MO6a35cJ A0gEGPVp6sFbjm6uogUetarDtJsTA4aWP+n/7z9Y2eHbs9+AAJDW8mtdLFHwcwpfFNob vkQTcAaf2Z+33jSiJs12/EtjiBpH9UwiuxFQF9k2k9KinVYrItT7mQ/uBa9rOpKwIkHY AdQE8a7/HTm+2yexYVVN5IbLmC7Mn9ugExtrE5NVd0nKw99v2c6t3VZTGY4lknF4UBM8 lpItRITVRlayRpT6RQOS7WMNx59y+RA8X/khqCZEwuZ5tu9mF6pFsRWmxdzNUZRnqr+B EfLg== X-Gm-Message-State: AC+VfDz/f2cXLAFUTDKRnAw1vqoh8Ip9FqaI1Mig03Cej2aMbVUO0vOP SxlNKeWGl7m5HNmAfBRG+KaGk4AtSlDqk1UiM3Leig== X-Google-Smtp-Source: ACHHUZ7HUFqW9nuGksN3M2s134tOxog7QtwRduF3tq3SG1+BwRnZxYhYv/YW6C0c6/u5cPbRBeoi6ioPy1RzWIeu2mA= X-Received: by 2002:a25:7e87:0:b0:b3b:a48c:b241 with SMTP id z129-20020a257e87000000b00b3ba48cb241mr2929232ybc.31.1685464441692; Tue, 30 May 2023 09:34:01 -0700 (PDT) MIME-Version: 1.0 References: <289E585B-EF8B-4B17-89BE-BE8295FD9FE1@gmail.com> <4CA1668E-A342-452E-A994-5839C377CB27@gmail.com> In-Reply-To: Date: Tue, 30 May 2023 18:33:50 +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) On Tue, 30 May 2023 at 18:27, Boro Sitnikovski wrote: > > 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=php+group+elements+site:stackoverflow.com All of the examples I looked at are asking for the first kind of grouping, that can be implemented as in your first example. In all the examples, if two items are equal, they end up in the same group. In your proposed behavior, equal items can end up in distinct groups depending on their original position in the source array. I don't see any questions or examples that ask for this. -- Andreas > > > 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. > > > > 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 > >>>> > >> >