Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111731 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 4115 invoked from network); 30 Aug 2020 21:38:52 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Aug 2020 21:38:52 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CE4001804C0 for ; Sun, 30 Aug 2020 13:43:05 -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,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from NAM04-SN1-obe.outbound.protection.outlook.com (mail-oln040092011048.outbound.protection.outlook.com [40.92.11.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 30 Aug 2020 13:43:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ex2JwK+JauSZWyD2TbWRnV3a0KfU7FqlkuIOEwCJosU3p6u8HLAybb0/TdFmA25mjm7hKgFSM60PU25aV11UV+uB+A7XyWqM7wFQIQT5vgROKuBnZFu9P7bcB9QGAW2ENlkEaKq/+TLnCZoRUDX+mkdYXTlKb7EoIITZixRkY7/63IlwgFvxQ8Hn379eQbAb2FgUDfbG3GA8w6wT6cwNPi64NIwK0cN4kT+Z8E50eU++S6MWxweRVs25btA5CGUjKDFrQJ320T3wmlgaz9teczDfZPnGEOoxxvH3UQrdlWP7FwWPRAam7DS41KzMT6OyuaSjvUaBb+AQXbBsXszSAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=tisedw0kMVsUUfsF8Tap3vrnr9RoiBYozd35AU5eBkI=; b=Hg/cwDu7kKwZEHWvFnIvmiWhxPCysC/cbw4XjCO/8pzKBYgSl9tFr6TLGH45suv5PEgeYIhLwPTEj4yRpng//+J4fCt2foxrqhKv/PclR+n7ZH3v8+sAImui95nyQaX35L1UiMzh9whgWw+b+SdDDzZENhxd+2OeKaf6lNI6Ngl+vA6P67jE9gZ7eASdSD1nzwD00RzQJPxqXzvDRbIwO3EjEVdnzXEtSy2kXbsXw296s3zKhQzA9923Nlw4pQHUpkqQr3qCNXQ93Jl5Xu4cFOrO46DWvMy55yGKjI6qmhxiaAeb29P1pPksLghRd+fxatlNswU8e+X/T85W3QuUjQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hotmail.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=tisedw0kMVsUUfsF8Tap3vrnr9RoiBYozd35AU5eBkI=; b=ToVEOarXP5EMFUPXmRCp84PJ5e4b2Cf+Hz7GR76QSc0S97SdCBM7tPdIfiRGV3W9Eqfhd0l3GUHSLum3iqArx20cWwXSqsblT5UsRCWXSlDG6NKlBdABMmLsVXZpKsrUoNWm+aAQg7xAyFLXp/Wl1YpRwkXMbvCJxpF75O/tjU5YsyiJXr+f/pOL9lOvDp8sIPUGry4SyjhekoQr52//izwIleUcQ+efuzR7p6iB2d1h27CJt27F3ncAItJjZ8ROtvAQoeLSOIu2exFZq0huF8rJY28fg4JLVecaoh6DHkOysBqh0SFgFB3TvpnLvDBP3EGDPpZNfik1vlJbT9jMcQ== Received: from BN3NAM04FT014.eop-NAM04.prod.protection.outlook.com (10.152.92.54) by BN3NAM04HT102.eop-NAM04.prod.protection.outlook.com (10.152.93.31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3326.21; Sun, 30 Aug 2020 20:43:03 +0000 Received: from DM6PR07MB6618.namprd07.prod.outlook.com (2a01:111:e400:7e4e::46) by BN3NAM04FT014.mail.protection.outlook.com (2a01:111:e400:7e4e::179) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3326.19 via Frontend Transport; Sun, 30 Aug 2020 20:43:03 +0000 Received: from DM6PR07MB6618.namprd07.prod.outlook.com ([fe80::f9d1:ed5b:8625:bfb4]) by DM6PR07MB6618.namprd07.prod.outlook.com ([fe80::f9d1:ed5b:8625:bfb4%7]) with mapi id 15.20.3326.025; Sun, 30 Aug 2020 20:43:03 +0000 To: Dik Takken , php internals Thread-Topic: [PHP-DEV] Proposal: Adding functions any(iterable $input, ?callable $cb = null, int $use_flags=0) and all(...) Thread-Index: AQHWfj2/bRooGr39CkeRSgZBGY4s+alRB8IAgAAT9Mw= Date: Sun, 30 Aug 2020 20:43:03 +0000 Message-ID: References: ,<89d77c3c-593b-b5dd-f39c-009575e295a5@gmail.com> In-Reply-To: <89d77c3c-593b-b5dd-f39c-009575e295a5@gmail.com> Accept-Language: en-CA, en-US Content-Language: en-CA X-MS-Has-Attach: X-MS-TNEF-Correlator: x-incomingtopheadermarker: OriginalChecksum:573AAA6AF6370D19EF4D1E1E918D024A3A3897ACCA5981F3C24A139EC9EB6858;UpperCasedChecksum:0951454D9F3B9361248A63BD37EB8A80B815F5006EAE1EC426E3DFC52CAD2E23;SizeAsReceived:7251;Count:44 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [OPbw1OEy8wzh8rfRz58+1aQbCH4qsrvo+Mqh0yY4LSrEKB97LmB7zTAegOE1IJfa] x-ms-publictraffictype: Email x-incomingheadercount: 44 x-eopattributedmessage: 0 x-ms-office365-filtering-correlation-id: 8cd3bd63-d2c7-4153-7a8e-08d84d254acb x-ms-traffictypediagnostic: BN3NAM04HT102: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: VpthplQQjnzWlJEEgDaCoYaz3jI7EnoUfHxD4i/ENzlTnzBMjUiuR4BI+nuQLDMmJfpK+3Ed3C2Kn7oEKtJLonjXKBroFzE7IbN8KzBxPkhwD6YrYIFpZj2bJDg17L4uMyaeOHa3bs065veIsKtHjBVy3dHdr7tBPJo1DLPpPuD7M/N76iqHdYp7ak7PZq212aR8BfB4QLwNv5+daKBzUXVG2NZRkXS7nJgm71/PlXOuvll4KHt7pU6d2MMXHVqN x-ms-exchange-antispam-messagedata: NiuGb5fc4q2fV/eB0AG/rHjepcBJMJyXldhU2/6oFxfKSOYUNFrLIQnXrpRIS++Rj6BysguSk+wv1Lskekk5KZ2XGqoR09qOJCOQ4OxrNFh7MLCndT9YNpsqdBGsydNhgoeRw28Ho4YF5PUcYfjI3Tb4b73sVqpu74MPcFrXTaXnpqdYfTlE5dJaawbx0F3GFLwB3J+PVJLZ7+tCkhny3g== x-ms-exchange-transport-forked: True Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: hotmail.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-AuthSource: BN3NAM04FT014.eop-NAM04.prod.protection.outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 8cd3bd63-d2c7-4153-7a8e-08d84d254acb X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 30 Aug 2020 20:43:03.6303 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN3NAM04HT102 Subject: Re: [PHP-DEV] Proposal: Adding functions any(iterable $input, ?callable $cb = null, int $use_flags=0) and all(...) From: tysonandre775@hotmail.com (tyson andre) Hi Dik Takken,=0A= =0A= > I would love to see this come to PHP. I also do a lot of Python=0A= > development and I really like its operators module, which provides=0A= > function equivalents to the intrinsic Python operators. Have a look:=0A= > =0A= > https://docs.python.org/3/library/operator.html=0A= > =0A= > Although the any() and all() functions are my favorites, having the full= =0A= > range of operator functions would be great. That could ultimately yield= =0A= > a proper counterpart to the range of array_* functions which support any= =0A= > iterable in stead of just arrays. Think of a keys() function that is a=0A= > generalization of array_keys().=0A= =0A= Operator overloading was the subject of https://wiki.php.net/rfc/userspace_= operator_overloading#vote=0A= which had a 38-28 vote, which didn't meet the 2/3 voting threshold.=0A= =0A= > This reminds me of the iter library that Nikita created:=0A= > =0A= > https://github.com/nikic/iter=0A= > =0A= > So yes, this can also be done in user space. Having it built into the=0A= language has advantages though:=0A= > =0A= > * High adoption rates, making lots of existing PHP code more concise=0A= > * Possibly better performance=0A= =0A= Strangely, if I remember correctly, I've had better performance looping inl= ine and calling closures than calling array_map=0A= when there were a large number of elements. I think opcache's inferences an= d the fact there's no switch=0A= from internals back to the php vm was part of the reason for it.=0A= (and the tracking of stack frames?)=0A= =0A= The readability's my main reason for making it native. I'd wondered if ther= e'd be a benefit to preloading where we replace C functions with native php= functions (optionally JITted) if there's an expected benefit,=0A= like HHVM does for parts of its standard library, but again out of scope.= =0A= =0A= > Regarding performance: Since we already have opcodes for handling=0A= > operators, it may be possible to extend these to consume arbitrary=0A= > numbers of operands from iterables. Then, an operator function like=0A= > any() would compile into its equivalent operator opcode. Finally, that=0A= > opcode can probably be JIT compiled into a tight loop in machine code.=0A= =0A= That's definitely something I'd want to see for array_map/array_filter.=0A= There's the question of whether it'd be reasonable to omit stack frames or = whether it's practical to create fake frames with parameter values=0A= for debug_backtrace().=0A= =0A= Larry Garfield also did some work on python-like list comprehensions,=0A= though it's still in draft.=0A= Automatically rewriting array_map(), array_filter(), all(), etc. to list co= mprehensions and then to JITted code=0A= could see a nice performance benefit in benefit, but this is out of scope o= f the RFC I'm working on=0A= See https://externals.io/message/104637=0A= =0A= > So a +1 from me for adding any() and all() but let us also consider the= =0A= > general problem of lacking iterable support in PHP. Perhaps that could=0A= > be the subject of a followup RFC.=0A= =0A= Part of the issue is what the return type would be.=0A= For example, reduce(iterable, callable, $initial =3D null) could work well,= but what should map() do=0A= =0A= `map(callable, iterable $values): iterable` - should it preserve keys, shou= ld it return an array|Generator, array|CustomTraversable, something dependi= ng on the object type, etc.=0A= (Generator performance seems like it'd be worse than eager evaluation)=0A= =0A= Regards,=0A= Tyson=