Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120463 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 59714 invoked from network); 30 May 2023 16:25:33 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 May 2023 16:25:33 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 54498180544 for ; Tue, 30 May 2023 09:25:33 -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-yw1-f178.google.com (mail-yw1-f178.google.com [209.85.128.178]) (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:25:32 -0700 (PDT) Received: by mail-yw1-f178.google.com with SMTP id 00721157ae682-565de553de1so45429517b3.0 for ; Tue, 30 May 2023 09:25:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20221208.gappssmtp.com; s=20221208; t=1685463932; x=1688055932; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=WEqK1KK48gsFJVe7f8NuY3KhKR7Jgb9SglYSNOxaLSE=; b=CHUNwWcIobK6dsziKkG175bD1KEk3lV6HdkmSkAz44fqCvtPvWjT6TYdwqef0qY0Sd U95lgtkA1V9cXwkY1F3/f7KOh2Wkx3tnx6tZ42/RNQwyLeLLJYFZ4aJ0wLWtkuxsB5+l XQ9aSIplkiSWkXOu/RDy09pSJIHN/6K2AyUgbqGVJg9jPN+0K6mqsp5wE2sHKoeaWVLN 0mpxlYIErmzy5nk2H1SKoBORsbHkAauNbsbObXKQcyCTlpTdCdReW4xNINa2c3f5y7+o SfoAhAxkO9KuKvvsT0LXKaHmqcywRPtMxqte5NGLnexnc70WF2iLjbp0/iRgNjEfZdqB MOhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685463932; x=1688055932; 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=WEqK1KK48gsFJVe7f8NuY3KhKR7Jgb9SglYSNOxaLSE=; b=OCa8vnsufxw7h8fZFNrOP83uuyr2rFCfco7a5+RudrNxpzDpJJfF+Xljb66c0WpTii YghqDEzsNeCkvouwMHINH0dWqiiCyegdFd+RJTzqsYcXSe8j/Jm7s0+jXKMwrD2nbdFk /l37+zYWvUGQTpNv+shAwOmx3SbTsIaz19co3ExOTMhbnf5BJZRWBoX6du0CWZ9AIezV p8tJLE7AFw6Qph4iE+RKRhngHDcvttqA9plCSfULrrh8WjBH/9tZcQ1298A6ENz77tXR 2g0MPt8Q9ho3VmUZdbQM6JcXkntlSXXYpGk4PeJBlbof4W66HEN+Hbz9+JOqphSiE40z Pugw== X-Gm-Message-State: AC+VfDwDA+Ps4Wt59xr4GloLD6Ge80m6svc6AFVRitnzQMMwneQzPbza BUqv0rzY9lgBqssKKKPvCuWJuU/ngfD+kjH53eXsYQ== X-Google-Smtp-Source: ACHHUZ5FsLjY5gyxZw+ZyA419lv4/luSKJU4kjEsoA7izH9otYXVVWnpWxmaSKIpVfpq18Jf5C3uhDsOvX2VoZbu8jc= X-Received: by 2002:a81:4e06:0:b0:561:bbb8:2dc3 with SMTP id c6-20020a814e06000000b00561bbb82dc3mr2691553ywb.21.1685463932032; Tue, 30 May 2023 09:25:32 -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:25:21 +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) Here we go, https://3v4l.org/KsL3o function array_group(array $arr1, callable $compare): array { $groups = []; $group = []; $prev = NULL; foreach ($arr1 as $value) { if ($group && !$compare($prev, $value)) { $groups[] = $group; $group = []; } $group[] = $value; $prev = $value; } if ($group) { $groups[] = $group; } return $groups; } On Tue, 30 May 2023 at 18:21, Andreas Hennings wrote: > > 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 > > >> > >