Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119731 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 53800 invoked from network); 16 Mar 2023 22:26:35 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 16 Mar 2023 22:26:35 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9179B180211 for ; Thu, 16 Mar 2023 15:26:34 -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.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS29838 64.147.123.0/24 X-Spam-Virus: No X-Envelope-From: Received: from wout3-smtp.messagingengine.com (wout3-smtp.messagingengine.com [64.147.123.19]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 16 Mar 2023 15:26:34 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id EFC2E320090D for ; Thu, 16 Mar 2023 18:26:32 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute4.internal (MEProxy); Thu, 16 Mar 2023 18:26:33 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-type:content-type:date:date:from :from:in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm2; t=1679005592; x= 1679091992; bh=wvxLfI2WBFD8Dn4ebCMGjPb5K534Gmmb7rv/1QeyfrA=; b=x TJqTWod8EsCRCU94J1nLdtF8CFbEvu3ZKBoGcOUwS53xAkZznRtWdyNy4DbGEnte YNhBDxObw+0Q6mD06yfHw/5HbyrblIF0n2TYl1361lu7bu+zQiA66a6MSCLoHSbo 7wKQR6zCN/Z+p3w3vrYX4+XYZrCWrux0nV1WMJc5Fgm7dZl8/wSg6x/Yssi4GjR+ 5CNO+sn1fGXSAUS9D5+oIXKz7UOVzlaUkTwYX9stXKUYcsah/BCiOWMC8syaiNIq MAANohgtSIzUvA/Jvq5x76uKOPU2z0vMntHl+kr5gRUcAmv+jbylOvkH2tb7IXdK ikozuZpo29NX+SnY+Kmtg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; t=1679005592; x=1679091992; bh=wvxLfI2WBFD8D n4ebCMGjPb5K534Gmmb7rv/1QeyfrA=; b=gNaGBzk0sLVhhQTeA9LmlxBvqXfFV GnY/kPa7QnAltaE8ra/ddBRfzKuXbFyQ56mGr9wAL5fT+83PUZnFcD0QE1Lc7SmP yAiVg3uJcpAci6dGjhnbnsJkGg4yKEO+pTG9YWrwR4xrST8hnuFcZjSx6+ztoEGe /oD1DxlluclVFNNBDOMXjyGj6ezoGxmOq0pFdY7V7ZReigegX9z6cJCytFHlOG6s wtxMxOm5LSyfjSV1WF3bqmlyhc1eQUNUym7AWY5vMJ3waqAUB1V+xIrmssmB76Z8 SjEV3m9py8BMdcccmjysQxtx+9IdkYeNziUzwsQd2YMxfbHr2vjVjpujw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvdeftddgudeifecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefofgggkfgjfhffhffvufgtsehttdertderredtnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecuggftrfgrthhtvghrnhepgeelgfekudeivddvteffueejffdthfejieevhefg ffekudevkedtvdelvddvffefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpe hmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 5BC311700089; Thu, 16 Mar 2023 18:26:32 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-221-gec32977366-fm-20230306.001-gec329773 Mime-Version: 1.0 Message-ID: <73033e11-a521-40a3-a29e-14d575332aea@app.fastmail.com> In-Reply-To: References: <9975B833-EE24-4ED7-B28E-841B92988BA0@cschneid.com> <1A2CE63B-ECCA-403D-83AC-B1E26279323C@gmail.com> <9a2140b4-97bb-4a9c-90c5-809274c83f75@app.fastmail.com> <88c4a63c-859b-94d5-e314-3399fb2c3fb0@gmail.com> <1a4d4434-7318-4831-9fc0-8b48a6400a62@app.fastmail.com> Date: Thu, 16 Mar 2023 17:26:00 -0500 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] First-class callable partial application From: larry@garfieldtech.com ("Larry Garfield") On Thu, Mar 16, 2023, at 4:14 AM, Rowan Tommins wrote: > On 15/03/2023 21:12, Dan Ackroyd wrote: >> Would it be desirable to split those two things into two separate >> RFCs, by having the first RFC not have native syntax support, but >> instead another static method on Closure? e.g. something like: >> >> Closure::partial($callable, array $position_params, array >> $named_params): Closure {} > > > Hm... now we have the first-class callable syntax, making it an instance > method on Closure would allow this: > > $mapFoo = array_map(...)->partial([$foo]); > $filterFoo = array_filter(...)->partial([1 => $foo]); > > Which could copy over the full signature, so be equivalent to this: > > $mapFoo = static fn(array ...$arrays): array => array_map($foo, ...$arrays); > $filterFoo = static fn(array $array, int $mode = 0): array => > array_filter($array, $foo, $mode); > > While being a similar length to a much less rich version: > > $mapFoo = fn($array) => array_map($foo, $array); > $filterFoo = fn($array) => array_filter($array, $foo); Fascinating! I... don't know if we considered something like that or not 3 years ago. It's been a while. It's definitely not as nice as the integrated syntax, but it does have the advantage of the implementation almost certainly being rather pedestrian in comparison. That approach would favor left-to-right application, but not force it, which is probably sufficient. As a thought experiment, if we had that syntax and functions that were designed to be used with them, it would look like so: function amap(callable $c, iterable $it) { ... } function implode(string $sep, iterable $it) { ... } function length(string $s) { ... } $arr = [1, 2, 3]; $a2 = amap(...)->partial(chr(...))($arr); $str = implode(...)->partial(',')($a2); Or, if combined with pipes: $size = $arr |> amap(...)->partial(chr(...)) |> implode(...)->partial(',') |> length(...); Which... is not terrible, especially as it doesn't preclude using higher order functions for more control. We would likely want partial() to accept positional args (left to right) and named args. And I'm more than happy to say now that the capture is only by value, ever. I wonder if there's a shorter name we could use than "partial" to make it more readable? Or perhaps an operator that applied only to closure objects? --Larry Garfield