Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:104663 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 79927 invoked from network); 12 Mar 2019 03:17:22 -0000 Received: from unknown (HELO out2-smtp.messagingengine.com) (66.111.4.26) by pb1.pair.com with SMTP; 12 Mar 2019 03:17:22 -0000 Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id EC94622001 for ; Mon, 11 Mar 2019 20:07:07 -0400 (EDT) Received: from imap26 ([10.202.2.76]) by compute7.internal (MEProxy); Mon, 11 Mar 2019 20:07:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:references:subject:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=62mzipeQd31aH38Zb a6Df1O/3mt8yj3LNAHATYg35Yo=; b=fajTTpK0O8TxYpq5McepeLCRKq8Gpy5sM JE3k2ZSPftZX3BN4b2D2Q1v8ZgA/exxtIbYIm4/Lnwaq1bno9A+elRY3JGiwjMCu Fo6XK/ANdrTwYBXX1ntQxcC89vozaqCgHkeaCDo/CUdOLrQlBwPcYyqTYGA257pb Y1yMkD+kyOBfjxuF1mhxgEI6CvrkKU5er2cv44BM7oa3ELbwbZ/f7yg0gvr+fyUa 5GYk0wNuI5om+Mow57zJjzcQ/0wIboYromIpMGmFPzluuyYJmn1URhq0f5OKLG1O 5mCXqO2d3uVHWQ13RN4zuAsNQkO7j9onPwGrf5yxgoqLpKWMuMJSA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedutddrgeejgddugecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepofgfkfgjfhffhffvufgtsehttdertd erredtnecuhfhrohhmpedfnfgrrhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehg rghrfhhivghlughtvggthhdrtghomheqnecuffhomhgrihhnpegvgihtvghrnhgrlhhsrd hiohdpphhhphdrnhgvthenucfrrghrrghmpehmrghilhhfrhhomheplhgrrhhrhiesghgr rhhfihgvlhguthgvtghhrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id A2613B43C0; Mon, 11 Mar 2019 20:07:07 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.1.5-925-g644bf8c-fmstable-20190228v5 X-Me-Personality: 10727885 Message-ID: In-Reply-To: References: Date: Mon, 11 Mar 2019 20:07:06 -0400 To: internals@lists.php.net Content-Type: text/plain Subject: Re: [PHP-DEV] RFC Draft: Comprehensions From: larry@garfieldtech.com ("Larry Garfield") On Mon, Mar 11, 2019, at 8:16 AM, Nikita Popov wrote: > On Sun, Mar 10, 2019 at 10:45 PM Larry Garfield > wrote: > > > Hello, peoples. I know it's been discussed once or twice before on the > > list, many years ago, but not recently. I therefore feel OK putting forth > > the following draft proposal for Comprehensions, aka "compact generators", > > in PHP: > > > > https://wiki.php.net/rfc/cohttps://externals.io/message/61021mprehensions > > > > > > Sara Golemon has written a preliminary patch that is partially complete > > (see the RFC link for details, it's impressively simple), but unfortunately > > doesn't have the bandwidth to complete it at this time. I am therefore > > looking for collaborators with more knowledge of internals than I (which is > > almost everyone) to help finish it up. > > > > The syntax proposed is also subject to revision if a terser but still > > lexer-friendly alternative can be proposed. > > > > At the moment I'm not calling this Proposed yet, as I don't feel > > comfortable doing so until someone else is on board to finish coding it. > > That said, if someone wants to weigh in on the concept for now (hopefully > > supportively) that's also fine. > > > > Anyone excited enough to help finish the job? > > > > (This is my first RFC collaboration, so if you're going to smack me for > > goofing something please be gentle about it.) > > > > I've brought up a similar proposal a few years ago, probably around the > time generators were first introduced: https://externals.io/message/61021 Ah, I knew I'd seen it on the list once before. Thanks. > I think my main point of feedback would be to stick closer to existing PHP > syntax, even if it costs us some brevity. I would prefer > > $gen = [foreach ($list as $x) if ($x % 2) yield $x * 2]; > > over > > $gen = [for $list as $x if $x % 2 yield $x * 2]; > > The latter is nice in languages that generally use "for" for this purpose > and generally allow omitting parentheses, but I don't think it's good to > introduce this kind of syntax inconsistency in one place. > > Similarly, I found the ability to omit the "yield" expression somewhat > confusing. Especially once you get to nesting > > $gen = [for $a as $b for $b as $c]; In that case you'd get a list of $c returned (implicit yield $c), although I can see where that's non-obvious. OTOH, requiring the yield statement every time in the majority case seems like excessive verbosity and ceremony. > the behavior becomes non-obvious. Also wondering how this part interacts > with keys, do you decide whether or not to yield the keys based on whether > they are part of the for expression? > > $gen = [for $list as $v]; // drops keys > $gen = [for $list as $k => $v]; // keeps keys Yes, that's the intent. No yield means implicit "key and value"; if key is omitted then it's implicitly just 0-based numbers. > Finally, Python makes a distinction between list comprehensions using [] > and generator expressions using (). This proposal effectively corresponds > to generator expressions, but uses the [] syntax. I'm wondering if that > will cause confusion. > > Regards, > Nikita I don't think so. For one, although we started from Python syntax the result is decidedly not Python, so I wouldn't expect people to assume the same symbols mean the same things. For another, Python uses [] for a list-producing comprehension, and {} for a dictionary-producting comprehension. Whether those return generators or not is a version question, not a syntax question. Since PHP doesn't differentiate between lists and dictionaries to begin with there's no need for different syntax. Our "dictionaries" already use []. --Larry Garfield