Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:100490 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 46408 invoked from network); 9 Sep 2017 14:38:23 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Sep 2017 14:38:23 -0000 Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.45 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.214.45 mail-it0-f45.google.com Received: from [209.85.214.45] ([209.85.214.45:37659] helo=mail-it0-f45.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DE/68-10715-DDCF3B95 for ; Sat, 09 Sep 2017 10:38:22 -0400 Received: by mail-it0-f45.google.com with SMTP id o200so8776891itg.0 for ; Sat, 09 Sep 2017 07:38:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=aJh0d0+hHlVf8R2/DzeB/r0ByRQBKgieAqSPjeBqcQ0=; b=r022l5YpcNHD+lGkcsvG/w9U+ciI/yBAQQ8Y1LU2OH0A+f/X76dG4SqhVt1Ow2T3PA UDrUgmeH8R9PPNOuLcSzc7B+OzAh35ctQL6b0+kkwZrdAYv2NFBbVTLb1uLKQjqFvR2/ rjpu39WwLV1u1UmUgYq4VT/PNck2MkgCbd6LTQtTPW9EKtqJL3GfPojFtQqjE42YxOjy gwtVwbL090zHgDGDUNeCt4rQYmrIEruCPpu5Ifqdg/MlCyxT5Gp7SsEHTKxB8i6fcaXx VwxaR0ezXjXZqDIjWmXK7T9bzsRv+RUPLQb3Kz5u5iwXWqTn590XTurA66dyb+FCpdRm Azow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=aJh0d0+hHlVf8R2/DzeB/r0ByRQBKgieAqSPjeBqcQ0=; b=VP7RV7WCnc8KtOkmre1dhBnEeRiYmdJdr5hL60VETSEOLuZ+deni62rfpxUVwIicpQ SwS3hWJKCB63++J5bOARXBjBszG56MSWX3TPmQnrxxNJmak/JXMWZ1ZkAPAs1gc2cMi6 ForKQDu+HDqP5IzesXrMtIsAxLELbDtXQifKUyRQzQHJFa8FlD2ejDC2kTjTU8CupwU+ RTF0zQiFkbVCp5nqCduAiJ8tIIOC4Qmmjw14vrTiiY+R7fTBUAUDkmVnetl+vk4t6ODb WvyNL+DEF+t/cLh1kPsRUc9EVwriK2HqwZ3JaEX0zGdLI4n5Il0fYuoerMVLrgXkOobF cSYQ== X-Gm-Message-State: AHPjjUjWjTTurbw0RvJZ2OroVUcuJCbnAlUZ5QRL+5sB7hgdtVy0XTM8 VXJPwWb3hQMbjqoI9jutyUi2yWrdoC5+rJ7E3JU= X-Google-Smtp-Source: AOwi7QBusC+ZignK7g73SiILqr0kFQFDFG5LmOdZ6MQDzPMlHaA4RqLQZ7Bd4qU+kk3u9Kc+Sby5VD78GNS0uPUzkFo= X-Received: by 10.36.31.8 with SMTP id d8mr1233321itd.15.1504967899129; Sat, 09 Sep 2017 07:38:19 -0700 (PDT) MIME-Version: 1.0 Received: by 10.107.13.3 with HTTP; Sat, 9 Sep 2017 07:38:18 -0700 (PDT) In-Reply-To: <82.32.10715.A8E03B95@pb1.pair.com> References: <82.32.10715.A8E03B95@pb1.pair.com> Date: Sat, 9 Sep 2017 16:38:18 +0200 Message-ID: To: Andrea Faulds Cc: PHP internals Content-Type: multipart/alternative; boundary="001a1140566006ac4a0558c2a558" Subject: Re: [PHP-DEV] [RFC] [Discussion] Operator functions From: nikita.ppv@gmail.com (Nikita Popov) --001a1140566006ac4a0558c2a558 Content-Type: text/plain; charset="UTF-8" On Fri, Sep 8, 2017 at 11:41 PM, Andrea Faulds wrote: > Hi everyone! > > Here's an RFC for a small, simple, self-contained feature with no > backwards-compatibility breaks and which in fact doesn't even touch the > language's syntax (it's 50%+1 eligible!) but which could make PHP a bit > more expressive and consistent, especially with potential later features. > It even has a test designed to impose minimal maintenance burden while > testing a fairly large possibility space! > > Anyway, the RFC in question is this: https://wiki.php.net/rfc/opera > tor_functions > > Please tell me what you think and suggest any potential improvements or > anything you think might have been an omission. > > Thanks! I like the general idea here, but have some comments. My main observation is that this proposal is only really useful in combination with a form of partial application. Passing operators to array_reduce() is cute, but it's not a major application, especially as we already have built-in functions for the two common operations (array_sum and array_product). Where operators-as-functions really shine is in cases where only one of the operands is bound. You acknowledge this in the RFC, and provide a few examples using a (not yet existing) partialApply() function: // Select only the positive numbers $positiveSubset = array_filters($numbers, partialApply('>', 0)); However, this code is subtly broken. Partial application (at least without specifying a more specific behavior) operates from left to right, so this code would be equivalent to: // Select only the positive numbers $positiveSubset = array_filters($numbers, function($n) { return 0 > $n; }); As such, it would return all negative numbers, not all positive numbers. This is a general issue of partial application in combination with operators: For the operations that do not commute, you nearly always want to bind the right operand, not the left. For my own purposes, I define an operator() function as follows: https://github.com/nikic/iter/blob/master/src/iter.fn.php#L60 This function either accepts a single argument such as operator('+'), in which case it is essentially equivalent to this proposal. Or it accepts two arguments, in which case the right operand will be bound, such as operator('>', 0). I wonder if providing such a function might not be a better solution to this problem. It also has the additional advantage that it can be easily polyfilled in older PHP versions. Nikita --001a1140566006ac4a0558c2a558--