Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114451 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 62322 invoked from network); 12 May 2021 16:11:48 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 12 May 2021 16:11:48 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AB0A718050A for ; Wed, 12 May 2021 09:19:51 -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.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 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 (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 12 May 2021 09:19:51 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 2FD3410D9 for ; Wed, 12 May 2021 12:19:49 -0400 (EDT) Received: from imap8 ([10.202.2.58]) by compute4.internal (MEProxy); Wed, 12 May 2021 12:19:49 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=yZUqt9 2JinyPve8CyvyjGw633FqRi9DhJmRx2QvFiLw=; b=RfZYBneW8OXZfQD4EaRQRQ i+CUEDrbSP/ixXXCqUR6couRbxCNUVLVuM4e9nQhuwnZ9gSmoGZ3kfU4vAdWpdut lGcOKAA/RX/amMz4a8XfokirxYPkfcrbrtqsuVH/wD23NX+Sx0LkeZFosbYryTVT oSgw7lhjcbxDszm3WejcKeS6MGvsvMUNvDTqzbnxGyM5GamuEKm3KSpyI1ROuGP3 OPSeD93che2TotbNXdFHEeJTR+ddZODg1VomehWaGyJ1Ug9gmXnFAksaNQRbql0B kTXCCEve87vpEsZNMOrbn75k0qTZ6IuLedz1gQBkq/Gg2yCfTS8qQlOPEZIuq0Gw == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvdehvddgleeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesthdtredtreertdenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucggtffrrghtthgvrhhnpeeglefgkeduiedvvdetffeujefftdfhjeeiveehgfff keduveektddvledvvdfffeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmh grihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomh X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 418F13A040C; Wed, 12 May 2021 12:19:48 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.5.0-alpha0-448-gae190416c7-fm-20210505.004-gae190416 Mime-Version: 1.0 Message-ID: In-Reply-To: References: <5650fab5-6006-4af2-b337-9e4aba67bf04@www.fastmail.com> <13142d57-f901-462c-a38f-b146ab36497a@www.fastmail.com> Date: Wed, 12 May 2021 11:18:51 -0500 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] [RFC] Partial function application From: larry@garfieldtech.com ("Larry Garfield") On Wed, May 12, 2021, at 4:51 AM, Nicolas Grekas wrote: > > > 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). You're thinking something like an auto-wiring routine for callables? function volume(int $x, int $y, int $z) { ... } class Resolver { private array $context = ['x' => 5]; public resolve(callable $c) { foreach (get_argument_names_from_param($c) as $k) { if (isset($this->context[$k]) { $args[$k] = $this->context[$k]; } } return $c(...$args, ?); } } $r = new Resolver(); $c2 = $r->resolve(volume(?)); print $c2(y: 3, z: 9); I haven't tried running that (I don't feel like messing with the necessary reflection), but... I think it would work already? Whether that's useful in practice or not I don't know. :-) --Larry Garfield