Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114436 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 8807 invoked from network); 12 May 2021 09:43:14 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 12 May 2021 09:43:14 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id F0D971804B5 for ; Wed, 12 May 2021 02:51:13 -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,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 12 May 2021 02:51:13 -0700 (PDT) Received: by mail-ej1-f46.google.com with SMTP id k10so2843500ejj.8 for ; Wed, 12 May 2021 02:51:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=qNM/EAcEGDPln1BQstYCn4lBbbdz8Ckep4yZLbf2X3c=; b=jSC0TTWn9K5Ma+KIyYOxUqs5cXHckVHnMKxP0ZuRGUPiHx6RlH+vOe4Om0VM/j6YQH oVnOclc7CilSsa3UdMTFDz/gh96pGJsu/SpRzR6jVaOXVLIoh8Y4PiSjRSIV9yX4ai24 yB3GeoFHp2zhb1CX6NMSS95qx1CTJ1Co1zfS1hJwu9/c7uvDwmAIxR6IVtTK+7lgquQI 43QOw4y8FBG7Q/AfzULVzCmTGPf8bWfFoOheAcAriIwLTSu9xe9rM/5wwpwN25TI+e+k elWpmIUdbj98KMiX4QVNHHv0OmotW25a+4i29J5GBIiMhzmflnFTyCnZqddZkIpAzp+o 5rZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=qNM/EAcEGDPln1BQstYCn4lBbbdz8Ckep4yZLbf2X3c=; b=NsebsoRc0HR5B4UY62N3DC4kYjqUXWr/lKhYCz0epCy/JBrNJKWafudCf18pWs+8NQ jp+KuEJWhQ2+ZoqJ+UOI4kPjw2pf26Q4e39rmcLGrybUFLqkeQ1QGcUbdR0y05cOvJv3 u6tl7h9UhOYB5ecXWipHoHCqIYqvPqkJ8POl+pTJglMLSpXoHwshuk+Or+bOQsRMydUw ojMfPdr7Bra4j161PubiTGm67BwwYbClRzErSV2GrMqiarDtFXyJ+BXcn4NHHJgbAB7g OaHKe1N/vubiYMU7lEPSgTYq0Op4/usMnyS4iZ98LUxEZg9f0RxgUrx4dZyIarySumYV +Jkw== X-Gm-Message-State: AOAM531gyr0qWyowVMlIjS4LrTUtaMbp7S6zro0YUaf65LCuOygDHa9a C5EqRkGhTUFVfQTE/VCX9JYHsDouNGqSLhDy+nzO4bxWU5I= X-Google-Smtp-Source: ABdhPJyBNQ7PmEFMsX81EERXMmH+KiIOflRqxlRC4mqDXUdhmjql5k9Hb7tfgr9rabMCK6EPb+g6wHjNyTTaOyqRZYM= X-Received: by 2002:a17:906:3042:: with SMTP id d2mr37094845ejd.234.1620813072539; Wed, 12 May 2021 02:51:12 -0700 (PDT) MIME-Version: 1.0 References: <5650fab5-6006-4af2-b337-9e4aba67bf04@www.fastmail.com> <13142d57-f901-462c-a38f-b146ab36497a@www.fastmail.com> In-Reply-To: <13142d57-f901-462c-a38f-b146ab36497a@www.fastmail.com> Date: Wed, 12 May 2021 11:51:00 +0200 Message-ID: To: Larry Garfield Cc: php internals Content-Type: multipart/alternative; boundary="0000000000006fa1c905c21ef322" Subject: Re: [PHP-DEV] [RFC] Partial function application From: nicolas.grekas+php@gmail.com (Nicolas Grekas) --0000000000006fa1c905c21ef322 Content-Type: text/plain; charset="UTF-8" > > > > BTW, ideally, partial functions should not increase the depth of the > > > > stacktrace at all. Do they? > > > > > > > > Nicolas > > > > > > They currently do, since they work by creating a Closure-esque object > > > called Partial with an __invoke() method. However, if you partial the > same > > > thing multiple times then only one stack level gets added. > > > > > > > Nice. Would it be possible to optimize this and remove the extra frame? > At > > least maybe for the (?) case? > > I'd have to defer to Joe (he wrote the implementation), but I suspect > not. The partial has to be there to carry around the information of what > callable to actually call and what the bound parameters are. At that > point, it's likely more work to decompose the Partial internally before > calling it than to just call the Partial itself. > Then I suppose that removing the extra frame would mean embedding that state into Closure objects. Would it make sense to extend Closure this way? Isn't the new Partial class an implementation artefact that should be removed? If not, what are the userland-side reasons to keep it? If the Partial class has to be kept, can it be optimized out back to a closure when no arguments are bound? (aka the "$closure = $this->method(?)" case) > > This makes me wonder: can we create a partial programmatically? Wouldn't > > that be needed for some use cases? > > Partial::createFromCallable($callable, the-args)? > > > > Nicolas > > I cannot think of a use case where that would be needed. Since you can > partial-ize any callable, including a dynamic one, if you needed to do > something like partial-ize one of a series of function calls you can do > that already: > > $c = match($some_input) { > 'A' => 'func_a', > 'B' => 'func_b', > 'C' => 'func_c', > }; > > $p = $c(1, 2 ?, 4); > > Though at that point, just partialing them in the first place inside the > match would be better as then you never have a function name in a string to > begin with. > Here is a use case: high-order argument resolvers / function reducers. What I mean is a function that takes a callable as arguments, resolves as many args of the callable as it can using whatever logic fits, and returns a callable with fewer arguments (only the non-resolved ones - aka a Partial). Nicolas --0000000000006fa1c905c21ef322--