Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:104835 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 85964 invoked from network); 21 Mar 2019 01:52:22 -0000 Received: from unknown (HELO mail-wr1-f47.google.com) (209.85.221.47) by pb1.pair.com with SMTP; 21 Mar 2019 01:52:22 -0000 Received: by mail-wr1-f47.google.com with SMTP id y13so4582996wrd.3 for ; Wed, 20 Mar 2019 15:44:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=VOlUMARlzFn3LZyLUvWpKwGc0S9lpUDzlQnDMRAFRkg=; b=KB8Zh0qFdR9hvxdO0Uaoq7pfSO9sz7t2/5lHoOIIJ2mhJ7J2UFzD76FRaDuXHs2DL0 +O3ZXWb/1U0mEZuoueWReo0+ZIwwIzQDCt3yOUhJGg2ED8aVE6ahwJIOSu+hb6l7RrJv mCsS5bA4x9A9gpBNwjXQx6y4auav+DyUpRFaR0KNKOYbYpIgRMF51u9hXqn9KCl934MR 3NpT3HD43joInQ8noDN0nM009FWll097yhi70dxh9dcusJvwWLJyxYXXn0pyHrkgNbB1 AGYy4oIm2ZG2S8ZD1GqjI/mAT+PG9PH2huz0c8EOoPkXw0Km6aMTbgnycz9nAKL3K7Zi QMNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=VOlUMARlzFn3LZyLUvWpKwGc0S9lpUDzlQnDMRAFRkg=; b=kxRGu1wXgIYV3cnQ7NWiCn8Er9Wd/l29Zwh1/SlrqcZH4kgQg7R8d1dmYsWeYr7YLl a+C8uET73WZ8FAtWE8aYKNwxYC1EtUcs3kBEoF+/42t2bNbUCdXkOuGJtNB1MPglNP87 wOH7P/3uePSTIDrWNiFblz0tghx2pAPNyWbVRrT/uKOYALWToqeAH6GnzT+0N/V6NZQ8 7HuZAmTdAtDnOyl8SERjr9yH+7eIL8gcaBBSxxT2mYqiezhrgHI6YllyDwu+/E1lt89S hQ6MsMUGqyK5s+wbJjrIRRlODqzhedlRs2l3vBAyW0Bfi8RXRIofKtn6sWDdRlZXNbT0 Vh8Q== X-Gm-Message-State: APjAAAUB7rmSd8MzUpWqtxTCloGPTR9AIlHuLf4ONdOqDJ8setDrCorz zDAzHkJ6JxwjLqrOxJarKxB2gaP5 X-Google-Smtp-Source: APXvYqyMGyDKqccbTpqqbIehi3bwk4wVC8q0hS5491ic8GjVhyLe8PZasRdHTddV9UZtPWbzznpefw== X-Received: by 2002:a5d:620f:: with SMTP id y15mr323963wru.101.1553121861423; Wed, 20 Mar 2019 15:44:21 -0700 (PDT) Received: from [192.168.0.16] (cpc84253-brig22-2-0-cust114.3-3.cable.virginm.net. [81.108.141.115]) by smtp.googlemail.com with ESMTPSA id k15sm139943wmj.8.2019.03.20.15.44.20 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 20 Mar 2019 15:44:20 -0700 (PDT) To: PHP internals References: <85417a73-a391-c9ad-49aa-5b88c65b0900@gmail.com> <5c8ea43d.1c69fb81.e50f1.7d18SMTPIN_ADDED_MISSING@mx.google.com> <0d34b3b1-866e-4bab-9a8f-3c70f1fc9721@www.fastmail.com> <752c83ab-0e6f-d8f5-ff15-e1f75b5d04b5@gmail.com> Message-ID: <301552a9-6f01-3177-0e57-3d9aac54cc0c@gmail.com> Date: Wed, 20 Mar 2019 22:44:16 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.5.3 MIME-Version: 1.0 In-Reply-To: <752c83ab-0e6f-d8f5-ff15-e1f75b5d04b5@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-GB Subject: Re: [PHP-DEV] RFC Draft: Comprehensions From: rowan.collins@gmail.com (Rowan Collins) On 20/03/2019 20:39, Stanislav Malyshev wrote: > Hi! > >> It's not that you can't make an array into a generator, but you can't make >> an eagerly-evaluated expression into a lazily-evaluated one. > Not sure what you mean here. I mean that, given a syntax that lazily-evaluates something, you can "fast-forward" the result to make it eagerly-evaluated; given a syntax that eagerly-evaluates something, you cannot do the opposite. Therefore, a lazy-evaluating syntax is more powerful, in that it can do everything an eager-evaluating one can do *and more*. > In this particular case, when you're working with an iterator over a > large file, you probably want a generator. Which is very easy to write > using a functional syntax, and the only thing comprehension syntax does > is switching the parts around a bit and saving you writing a "function" > keyword. I don't think it's a very common case though. Isn't that true of *every* use of the syntax, though, that "all it does is switch the parts around a bit"? There's nothing that an array comprehension can do that can't be done any other way, it's just short-hand; making it return a generator just extends that short-hand to more use cases. > Of course, when you need to filter iterators, then you need a different > approach. I do not doubt that. I doubt that filtering iterators is the > most common case in PHP to make the comprehension syntax only support it > - I think the case of array transformation is much more common. I am not saying that it is "the most common case", and I am not saying that it should be the only thing supported. I am saying that if we don't have dedicated syntax for each, one way to support *both* variants is to make the iterator version the base case, and then have a universal syntax that switches it to an array. If we do have syntax for both variants, then clearly both cases are covered anyway - although I think a short-hand for iterator_to_array would still have merit elsewhere. >> But if we only have one, it should be the iterator version, with a >> short-hand for iterator_to_array as a separate language improvement. > I think this would significantly reduce the usability of such construct > - to the point that it's easier to use the existing syntax, thus making > new syntax sugar useless. The whole point of syntax sugar is making > common case easy, and doing what you are suggesting is to make a corner > case (existing, but IMHO much less common) easy and make the user work > to achieve the common case. I think you're arguing against a straw man here. Yes, the cost of the extra syntax should be weighed against the benefit of the extra flexibility. And IF the extra syntax was so verbose that it would be easier to use the existing syntax, that cost would probably be too high. But that's not what I was suggesting at all; I was picturing a new operator, a few characters long, which could turn any iterator into an array. Are you really saying that something like this: $a = [ ... (foreach $x as $y if $y < 2 yield $y) ]; would be so verbose that you'd rather write this: $a = (function() use ($x) { $a=[]; foreach ( $x as $y ) if ( $y < 2 ) $a[] = $y; return $a; })(); or with the shortest closure syntax I can think of, this: $a = (() => { $a=[]; foreach ( $x as $y ) if ( $y < 2 ) $a[] = $y; return $a; })(); By all means let's try to weigh the cost and benefit, but let's not jump to the conclusion that the cost is nearly infinite and the benefit is nearly zero. Regards, -- Rowan Collins [IMSoP]