Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120459 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 50012 invoked from network); 30 May 2023 15:09:03 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 May 2023 15:09:03 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3DDE0180545 for ; Tue, 30 May 2023 08:09:00 -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_H3,RCVD_IN_MSPIKE_WL,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-ej1-f54.google.com (mail-ej1-f54.google.com [209.85.218.54]) (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 08:08:59 -0700 (PDT) Received: by mail-ej1-f54.google.com with SMTP id a640c23a62f3a-96f5d651170so1123213166b.1 for ; Tue, 30 May 2023 08:08:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685459338; x=1688051338; 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=MVsTwDTuUC7k2mLC/+kE7cQ78nyCcsMcM17Ge8Sj3DA=; b=MxnnHSbzgU2i0wNXzf3HSa7jJdq9bbyZfkod8WtLmTdtAqMAlgtxQRl954Pw4KCbPH ya0X7imQU/Ekxz+Mzobv7F80Hdkzo7bcdwpq9CK2gW2oUq6rBQMf/Fo0ORe0c0XYRztj eYSgOxYDpmlM6pcTCQiHuKWolFjvWtFMvVd2p+HGvI4K/kkcL2nkjFe4gM/AvTtfx+GA U1QGZVopjg0Ur8jXu2CBSTODHgvtWnDUMT5jrfjKGem7vMyr9yKUjMGAh48IQxgeEZFn xtjPctqeWxtImyCxrWiDzURQcLCfA8Qg/Fz70JShfBGbCkl9k5Tp39RViTBFv58fGFes Wsew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685459338; x=1688051338; 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=MVsTwDTuUC7k2mLC/+kE7cQ78nyCcsMcM17Ge8Sj3DA=; b=G49potdrnfQ4PfU1j7AjwuAVForhDBcpiKdEDyBzwrUW3Yf0RvVx0BsZEi2hxcZdJB uHVpWFrg5tdwiheuVn2BfYx/qg3INgUec6tBaVhPq+f3dJKi8bQpQq2+hjzvPZbRwAHv akeGpDXSgG67kf4zOJJpSq0s5naVm79uKUEzdRIj4EUzCO+glC1KL9mDJAu6MgecluR+ JvulkXS1CyjJEp0oMVt94RRqW6Xk5wZPL9KVioouS0vHEIlplho3FwMFJSB0XFIQHcr+ N3HXuDxMMQWbeeaSCWyV9LQ4KSvXTPdny0Jv44ULWyykyOhEtovZRx8M/jrMKbKtSts1 pApQ== X-Gm-Message-State: AC+VfDydR7O9te8sqQKQgQi++9sVjk/Ad1H9oeKp48ubrS43Jy4JCiLi EHU6JyPF0WBaQWaYWhOXhHEVH+f4upk= X-Google-Smtp-Source: ACHHUZ7c7v+KYiFezrf3HQFNJNwtnv7uadJoqIcmWSXhL2rWeQ1UXvinbNmBpX/dLZkDvBNNovPHtA== X-Received: by 2002:a17:907:d28:b0:973:ddfe:e074 with SMTP id gn40-20020a1709070d2800b00973ddfee074mr3128908ejc.2.1685459338290; Tue, 30 May 2023 08:08:58 -0700 (PDT) Received: from smtpclient.apple ([46.217.211.121]) by smtp.gmail.com with ESMTPSA id b12-20020a170906150c00b0097381fe7aaasm7458311ejd.180.2023.05.30.08.08.57 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 30 May 2023 08:08:57 -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 17:08:46 +0200 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: <4CA1668E-A342-452E-A994-5839C377CB27@gmail.com> References: <289E585B-EF8B-4B17-89BE-BE8295FD9FE1@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) 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_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 Best, Boro > 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