Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114785 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 40539 invoked from network); 8 Jun 2021 13:54:42 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Jun 2021 13:54:42 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5FF7218053C for ; Tue, 8 Jun 2021 07:09:29 -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 out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) (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 ; Tue, 8 Jun 2021 07:09:28 -0700 (PDT) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 4A6AA5C0135 for ; Tue, 8 Jun 2021 10:09:28 -0400 (EDT) Received: from imap43 ([10.202.2.93]) by compute1.internal (MEProxy); Tue, 08 Jun 2021 10:09:28 -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=fm3; bh=9NuVyl 64dhB+v5mMXYyHc1Xm6IK68436VJcGO+DGp98=; b=p5LyNgl2t+L2sAli+byPk3 4IiSsrBx7sD8vIS0NSqas2wRWorjbv1GHQLBFE+v51vP9oIRx4AtNCjSqvTjh2UP Rnp58vkVe+60aJdUUS+QbWyJBgc+Ykl88KpDHwVWuZ7VtnGlMXJ3yi3umu4zQeoN oAOXGaGUZi/+C/BgYE7+lxSeGvLgtuW1RWQZE2MLLZTOpXzReS6d0tQ7mSUnFaL2 +jf3VQKx34a7ETuLNmTrH6sNgHZt4KXzxMH4Mmh120d9NMUjg4L12sa1Ic2plHqc XVtHTWRUiYiOlDHTI+9gvYMyRkjPVoksuxGh6+KAabcsC+FqTBP1yreRa3ILWenQ == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrfedtledgheekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesthdtredtreertdenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucggtffrrghtthgvrhhnpeejuddtleelkeevudeuheelvdffleejkeffueeigfej ffekieffgfejhedthfekveenucffohhmrghinhephhgrshhkvghllhdrohhrghdpphhhph drnhgvthdpghhithhhuhgsrdgtohhmnecuvehluhhsthgvrhfuihiivgeptdenucfrrghr rghmpehmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id F210CAC0064; Tue, 8 Jun 2021 10:09:27 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.5.0-alpha0-519-g27a961944e-fm-20210531.001-g27a96194 Mime-Version: 1.0 Message-ID: <8a890865-a7e0-49c0-aecd-73576c7d2ab1@www.fastmail.com> In-Reply-To: References: <13daea98-c32e-491b-a37c-3e151dd1f6c2@www.fastmail.com> Date: Tue, 08 Jun 2021 09:09:06 -0500 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] [RFC] Pipe Operator, take 2 From: larry@garfieldtech.com ("Larry Garfield") On Tue, Jun 8, 2021, at 5:41 AM, Guilliam Xavier wrote: > > Hm. You're right. It used to, but it's been a very long time since > > explode() allowed an empty split, apparently. I updated the example to use > > str_split, which is what I'd intended to do in this case. Thanks. > > > > Are you thinking to implode()? Anyway, you forgot to update one > `explode(?)` to `str_split(?)`, and also, the first `fn($v) => > 'strtoupper'` should be just `'strtoupper'`. I deliberately made that example extra verbose to show how ugly it can get, but I can shorten it. > About Haskell, rather than (or in addition to) the function composition > [not "concatenation"] (.), I would mention the reverse application operator > (&): > https://hackage.haskell.org/package/base-4.15.0.0/docs/Data-Function.html#v:-38- > > One thing I note is that even with PFA, some examples still need an arrow > function, e.g. the PSR-7 one: > > ``` > ServerRequest::fromGlobals() > |> authenticate(?) > |> $router->resolveAction(?) > |> fn($request) => $request->getAttribute('action')($request) > /* ... */; > ``` > > while in Hack you would write it as: > > ``` > ServerRequest::fromGlobals() > |> authenticate($$) > |> $router->resolveAction($$) > |> $$->getAttribute('action')($$) > /* ... */; > ``` > > Also, quoting from > https://wiki.php.net/rfc/first_class_callable_syntax#partial_function_application > : > > """ > Both approaches to the pipe operator have their advantages. The $$ based > variant allows using more than plain function calls in each pipeline step > (e.g. you could have $$->getName() as a step, something not possible with > PFA), and is also trivially free. A PFA-based optimization would entail > significant overhead relative to simple function calls, unless special > optimization for the pipe operator usage is introduced (which may not be > possible, depending on precise semantics). > """ > > Could you (or Nikita) expand a bit on this (esp. the advantages of the PFA > approach / disadvantages of Hack's approach)? > > Regards, It's true PFA doesn't cover every possible RHS of pipes. In practice, I think using the piped value as an object on which to invoke a method is the only major gap. Normally in functional code you would use a lens in that case, which (if I am understanding those correctly; that's roughly at the edge of my functional understanding) is essentially a function call that wraps accessing a property or calling a method so that it feels more functional, and thus pipes cleanly. However, piping with callables has a number of advantages. 1) The implementation is vastly simpler. It's simple enough that even I can manage it, whereas Hack-style would be more considerably implementation work. 2) I would argue it's more flexible. Once you start thinking of callables/functions in a first class way, producing functions on the fly that do what you want becomes natural, and fits better with a pipe-to-callable model. For instance, the comprehension-esque example (which I suspect will be one of the most common use cases of pipes) is far cleaner with a callable, as it can obviate any question about parameter order. Another example I threw together last night is this proof of concept last night, which works when pipes, enums, and partials are combined. I don't think Hack-style would be capable of this, at least not as elegantly. https://gist.github.com/Crell/e484bb27372e7bc93516331a15069f97 (That's essentially a "naked either monad".) 3) I disagree that the overhead of arbitrary callables is "significant." It's there, but at that point you're talking about optimizing function call counts, mostly on partials; unless you're using pipes for absolutely everything, go remove an SQL query or two and you'll get a bigger performance boost. 4) Far more languages have callable pipes. Hack is, as far as I am aware, entirely alone in having pipes be combined with a custom expression syntax rather than just using functions/callables. That isn't conclusive proof of anything, but it's certainly suggestive. I'm going to be moving forward with this approach one way or another (if for point 1 if nothing else). I do believe it is the more flexible, more robust approach, and fits with the general strategy I recommend of small, targeted changes that combine with other small, targeted changes to offer more functionality than either of them alone. That's exactly what we're doing here. --Larry Garfield