Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119735 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 93585 invoked from network); 17 Mar 2023 09:52:51 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Mar 2023 09:52:51 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1FD0D180381 for ; Fri, 17 Mar 2023 02:52:50 -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_H2,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-yw1-f182.google.com (mail-yw1-f182.google.com [209.85.128.182]) (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 ; Fri, 17 Mar 2023 02:52:49 -0700 (PDT) Received: by mail-yw1-f182.google.com with SMTP id 00721157ae682-5418d54d77bso83628567b3.12 for ; Fri, 17 Mar 2023 02:52:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679046769; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=V8sWnfZOBdGR7lVtlK60qUAEF5eus8AScPWcNss5g1Q=; b=CS+zqPDERDfCLfexLcZISDrmM5t0MChFTirXdVEbOeABnD6SwBvy0B2Iga7zyZ61Y8 rVIav5Kk7UfzhlHcDEU0hR2coU2xKfSVgqFI3pPXqKLlbfaeN44fK/hgzf09Tb2qQATi kYyxgD9m8k73F0yCFzgGLcvz3TPs1d+F2DW8gyoBaN6DqfB4zOqGNpkzEE/2lGo1YcNm gd1b17YiXRM4edN1LstSIT8RgmvcwMEgodypkzK0dwD1Mfddm3c4A0v+k2lhf9P02gMM FLMT95MYwFuX4Lc4jiHYjHBt8E9eWQzYvFDUYXh9daEHd9Mpy7Sfs0PemF+WhfWbpswa FqvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679046769; h=content-transfer-encoding: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=V8sWnfZOBdGR7lVtlK60qUAEF5eus8AScPWcNss5g1Q=; b=rm9G++kMdd9OV+HjgnXvteqzJ4Ato8KkRvcPiGhN+dGP5SaiYcDIzPG2nagmIobtx2 zHEWavYbX6XIulPNz8MVL0Mo+VQKY1csMV0yUclNNsHZFnZ2pcpbgthC+z9JmlW/anLe u3U0jKF9r96Q2BMrsGtkLbOTjnQt3qOrox7ndokOEpi0WVOFO7BJ/CPe1edBbtSfsmdU 8/x8ZRx2+vPbKhB9P+p+JYMAwfbwXssKVT513lqYURzRP9gC4AkFrqmcJXDtuQowog25 +kbI7kbDrcpwULhbCNaHa/p5uyCRY/QecjANY783JeJyUp0O4mefUcd5JP4EjsYzEEPC WHWg== X-Gm-Message-State: AO0yUKURdQ4EX0yL9Jp0YEsmToS6J6NhIE60WK0cqBzEK5co9zusCZZB LDG4cEpnH8FYbUz+cvSd5aqP32GL2q/LCIaAD4hbNS03z7mDHIkI X-Google-Smtp-Source: AK7set8fE7e90z0XSGREP0n63oPhbezVGwxKBzgbXj9ou2ggkw+jPNrZd1DJprX9fjBY7b1GOvkROhFqaYJVI8AE24I= X-Received: by 2002:a81:af14:0:b0:541:8291:5237 with SMTP id n20-20020a81af14000000b0054182915237mr1612383ywh.0.1679046768745; Fri, 17 Mar 2023 02:52:48 -0700 (PDT) MIME-Version: 1.0 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> <73033e11-a521-40a3-a29e-14d575332aea@app.fastmail.com> In-Reply-To: <73033e11-a521-40a3-a29e-14d575332aea@app.fastmail.com> Date: Fri, 17 Mar 2023 10:52:37 +0100 Message-ID: To: Larry Garfield Cc: php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] First-class callable partial application From: landers.robert@gmail.com (Robert Landers) On Thu, Mar 16, 2023 at 11:26=E2=80=AFPM Larry Garfield wrote: > > 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 instanc= e > > method on Closure would allow this: > > > > $mapFoo =3D array_map(...)->partial([$foo]); > > $filterFoo =3D array_filter(...)->partial([1 =3D> $foo]); > > > > Which could copy over the full signature, so be equivalent to this: > > > > $mapFoo =3D static fn(array ...$arrays): array =3D> array_map($foo, ...= $arrays); > > $filterFoo =3D static fn(array $array, int $mode =3D 0): array =3D> > > array_filter($array, $foo, $mode); > > > > While being a similar length to a much less rich version: > > > > $mapFoo =3D fn($array) =3D> array_map($foo, $array); > > $filterFoo =3D fn($array) =3D> 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 th= e advantage of the implementation almost certainly being rather pedestrian = in comparison. That approach would favor left-to-right application, but no= t force it, which is probably sufficient. > > As a thought experiment, if we had that syntax and functions that were de= signed 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 =3D [1, 2, 3]; > > $a2 =3D amap(...)->partial(chr(...))($arr); > > $str =3D implode(...)->partial(',')($a2); > > Or, if combined with pipes: > > $size =3D $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 onl= y 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 object= s? > > --Larry Garfield > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > Hey Larry, > We would likely want partial() to accept positional args (left to right) = and named args. I think this is already supported-ish. For example, here's a partial application in user space that seems to follow all the rules. Or at least the rules that are followed make sense. function partial(Closure $callable, ...$args): Closure { return static fn(...$fullArgs) =3D> $callable(...[...$args, ...$fullArgs= ]); } https://onlinephp.io/c/9195a It only needs a bit of edge-condition handling (like when a parameter is filled, you probably shouldn't be able to override arbitrary parameters).