Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:104679 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 95627 invoked from network); 12 Mar 2019 18:53:09 -0000 Received: from unknown (HELO mail-lj1-f195.google.com) (209.85.208.195) by pb1.pair.com with SMTP; 12 Mar 2019 18:53:09 -0000 Received: by mail-lj1-f195.google.com with SMTP id q128so2707465ljb.11 for ; Tue, 12 Mar 2019 08:43:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=eU0wasessEdWkWDTXoiJC2bid0ISGN/hhySAC8ByvLg=; b=D7gCtxK9iRB1BL8U7dsBzfi4pgm1XsWdolKo1FpgjfsmYBwfIDMYTc91oBcGPfbYv5 rm4HZDRkFaIomlUvk16SKrEfEXzQ9J3t7vifH7P6WtNJtGo9FKn3fcZCXHHxEJ6jSkcb dY03YZyRbV7ah6jm4zQfU8Kfx4u8tXNObw1Z8aYedjZJZJ2g5UJstpZQPLzNZI6hvRgb +L5SRbpR2OLEpgr+d8DxspQ3bKFWpWL90g7XVQsN6qCC7cbZzxELxJxvxlL6tYqR6EOO +96FQup+HFi1dDm/jK99OIUaFtEzecdfj48hPqEYKhNht/vmYfpqnw8X4L3JdQ9wZfC0 8VPQ== X-Gm-Message-State: APjAAAV/Npj244KIzXl5V+EaDburQE1xtiSEOg0VQIv8Bx3zCgVnjw2C y163DepHLHIBMz0lYTu5zistuZfE+JcVQi88PVxqzQsD X-Google-Smtp-Source: APXvYqx2jqmZKTMzv60fbp3hCODHaYdjJ+fPdJvmhvdPA+UudMAW6f35BGy76jwwz7TO7wNX5luu8WbI7Nt6UEgbcng= X-Received: by 2002:a2e:9a0a:: with SMTP id o10mr12271100lji.112.1552405383461; Tue, 12 Mar 2019 08:43:03 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 12 Mar 2019 09:42:46 -0600 Message-ID: To: Larry Garfield Cc: internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] RFC Draft: Comprehensions From: levim@php.net (Levi Morrison) On Sun, Mar 10, 2019 at 3:45 PM Larry Garfield wro= te: > > Hello, peoples. I know it's been discussed once or twice before on the l= ist, many years ago, but not recently. I therefore feel OK putting forth t= he following draft proposal for Comprehensions, aka "compact generators", i= n PHP: > > https://wiki.php.net/rfc/comprehensions > > 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 loo= king for collaborators with more knowledge of internals than I (which is al= most everyone) to help finish it up. > > The syntax proposed is also subject to revision if a terser but still lex= er-friendly alternative can be proposed. > > At the moment I'm not calling this Proposed yet, as I don't feel comforta= ble 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 supportive= ly) 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 g= oofing something please be gentle about it.) > > -- > Larry Garfield > larry@garfieldtech.com > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php I want to like the idea of list comprehensions. However, after thinking about it for quite a bit over the past year as well as since discussion started, the issue is really that closures are terrible in PHP. Look at this example from the RFC: $result =3D (function() use ($table) { foreach ($table as $num =3D> $row) { if ($num % 2 =3D=3D 0) { foreach ($row as $col =3D> $val) { if ($col >=3D 3) { yield $num =3D> $val; } } } } })(); We could use this syntax today: $result =3D (function () { foreach ($table as $num =3D> $row) if ($num % 2 =3D=3D 0) foreach ($row as $col =3D> $val) if ($col >=3D 3) yield $num =3D> $val; })(); Compare that to this code using comprehensions, using the same whitespace s= tyle: $result =3D (for $table as $num =3D> $row if $num %2 =3D=3D0 for $row as $col =3D> $value if $col >=3D 3 yield $num =3D> $val ); In my opinion there are no meaningful differences here, and nothing prevents you from using the all-in-one-line style if you care to. Let's look at a shorter piece, and let's also assume that the following code is eagerly evaluated and stored into an array (which is not what the RFC is proposing, but it helps for this example): $result =3D [ for ($hex_numbers as $num) yield intval($num, 16); ]; With closures as they are today: $result =3D array_map(function($num) { return intval($num, 16); }, $hex_numbers); Oof, that sucks and I didn't even have to bind any variables. But if we had shorter closures (this is JavaScript style) then it's fine again: $result =3D array_map(($num) =3D> intval($num, 16), $hex_numbers); My point is, closures are the major culprit. We really need shorter closure= s. Again, I want to like comprehensions. I think we should revisit list comprehensions after we've had shorter closures for a little while (hopefully we can get something passed for 7.4 or 8.0).