Newsgroups: php.internals
Path: news.php.net
Xref: news.php.net php.internals:61023
Return-Path: <krebs.seb@gmail.com>
Mailing-List: contact internals-help@lists.php.net; run by ezmlm
Delivered-To: mailing list internals@lists.php.net
Received: (qmail 45293 invoked from network); 28 Jun 2012 13:00:32 -0000
Received: from unknown (HELO lists.php.net) (127.0.0.1)
  by localhost with SMTP; 28 Jun 2012 13:00:32 -0000
Received: from [127.0.0.1] ([127.0.0.1:29283])
	by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ECSTREAM
	id 85/8C-08168-0755CEF4 for <internals@lists.php.net>; Thu, 28 Jun 2012 09:00:32 -0400
Authentication-Results: pb1.pair.com header.from=sebastian.krebs.berlin@gmail.com; sender-id=pass
Authentication-Results: pb1.pair.com smtp.mail=krebs.seb@gmail.com; spf=pass; sender-id=pass
Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.170 as permitted sender)
X-PHP-List-Original-Sender: krebs.seb@gmail.com
X-Host-Fingerprint: 209.85.215.170 mail-ey0-f170.google.com  
Received: from [209.85.215.170] ([209.85.215.170:52693] helo=mail-ey0-f170.google.com)
	by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP
	id D8/EB-08168-B515CEF4 for <internals@lists.php.net>; Thu, 28 Jun 2012 08:43:07 -0400
Received: by eaal12 with SMTP id l12so810137eaa.29
        for <internals@lists.php.net>; Thu, 28 Jun 2012 05:43:04 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=mime-version:sender:x-google-sender-delegation:in-reply-to
         :references:date:x-google-sender-auth:message-id:subject:from:to
         :content-type;
        bh=J5VF8rpQtKoEiWNWLyGWqWcBYRBHEAkomfD+0VCwRzU=;
        b=doH9+onzVV9wV23SaF6tpdLTBgGIUdtO2qXHZ3HWTeA3ekpKoZVvosQm7pPhIOdxfh
         FJFauvbQt35+P0daYA3TJo6jZ0Yqx4pHb60WUs9I1GGS98mOxclnd1dcpqMPunXN1bB8
         D+zc6eQk9laRC8EoXMJcPWub03Awlp9JOVitGEhbC2JOAS1Sp6xng0dEJAoBdOzb1lVt
         BnLDtnEiUevyBIm8PXBUgahohlwzgFsLaNB1c3P0doEwSP7VODgwvAUu0pkusYdtOScF
         gY+UdHt4en7YVgbnm8UCWMtr3dogxySXhY5N9W3fp2kWp6hpi0SOrcTRENRSfH4Z7JJK
         dgPw==
MIME-Version: 1.0
Received: by 10.14.97.67 with SMTP id s43mr453998eef.45.1340887384296; Thu, 28
 Jun 2012 05:43:04 -0700 (PDT)
Sender: sebastian.krebs.berlin@gmail.com
X-Google-Sender-Delegation: sebastian.krebs.berlin@gmail.com
Received: by 10.14.44.17 with HTTP; Thu, 28 Jun 2012 05:43:04 -0700 (PDT)
In-Reply-To: <CAF+90c8M9nxhGTXXh5dkKYaM75iYaPuKiSxfr7pC3KDkLyEwrA@mail.gmail.com>
References: <CAF+90c8M9nxhGTXXh5dkKYaM75iYaPuKiSxfr7pC3KDkLyEwrA@mail.gmail.com>
Date: Thu, 28 Jun 2012 14:43:04 +0200
X-Google-Sender-Auth: YTttLhZV9BYk-lioPd9OGiFWeqQ
Message-ID: <CALtuQzBzzR9JryNee4MPk9hvkdJm0-OqrmgeWTEJwwsB3tgHog@mail.gmail.com>
To: PHP internals <internals@lists.php.net>
Content-Type: multipart/alternative; boundary=bcaec52c6a9d398cfc04c387ac62
Subject: Re: [PHP-DEV] List comprehensions and generator expressions for PHP
From: krebs.seb@gmail.com (Sebastian Krebs)

--bcaec52c6a9d398cfc04c387ac62
Content-Type: text/plain; charset=ISO-8859-1

Hi,

Whats the difference to the (already existing) function array_map() (except
the syntax and one more new keyword)?

> $firstNames = array_map(function($user){return $user->firstname;},
$users);

Don't want to rewrite every example you gave, but you probably see my point.

Regards,
Sebastian

2012/6/28 Nikita Popov <nikita.ppv@gmail.com>

> Hi internals!
>
> Python and several other languages include support for list
> comprehensions and generator expressions and I'd like to see something
> similar in PHP too.
>
> I created a hacky proof of concept implementation here:
> https://github.com/nikic/php-src/tree/addListComprehensions. It's
> really dirty, but it implements both features in about ~150 lines of
> code.
>
> Currently I'm using the following syntax:
>
>    $firstNames = [foreach ($users as $user) yield $user->firstName];
>
> This code is roughly equivalent to writing:
>
>    $firstNames = [];
>    foreach ($users as $user) {
>        $firstNames[] = $user->firstName;
>    }
>
> You may notice that this particular list comprehension provides the
> same functionality as array_column(), just in a little more
> generalized way. E.g. you could use all of the following without
> having special functions for them all:
>
>    $firstNames = [foreach ($users as $user) yield $user->firstName];
>
>    $firstNames = [foreach ($users as $user) yield $user->getFirstName()];
>
>    $firstNames = [foreach ($users as $user) yield $user['firstName']];
>
> It's also possible to explicitly specify a key:
>
>    $firstNames = [foreach ($users as $user) yield $user->id =>
> $user->firstName];
>
> It is also possible to filter elements using list comprehensions:
>
>    $underageUsers = [foreach ($users as $user) if ($user->age < 18)
> yield $user];
>    // or just the names
>    $underageUserNames = [foreach ($users as $user) if ($user->age <
> 18) yield $user->firstName];
>
> It is also possible to nest multiple foreach loops:
>
>    $aList = ['A', 'B'];
>    $bList = [1, 2];
>    $combinations = [foreach ($aList as $a) foreach ($bList as $b)
> yield [$a, $b]];
>    // gives: [ ['A', 1], ['A', 2], ['B', 1], ['B', 2] ]
>
> All the above are list comprehensions (or in PHP rather array
> comprehensions), i.e. they create an array as the result.
>
> If this is not needed it is also possible to compute the values lazily
> using generator expressions, which use () instead of [].
>
>    $firstNames = (foreach ($users as $user) yield $user->firstName);
>
> In this case $firstNames will no longer be an array of first names,
> but instead will be a generator producing first names.
>
> This is handy if you only need to iterate the resulting "list" only
> once as it saves you holding the whole list in memory.
>
> Also it allows you to work with infinite lists easily:
>
>    function *naturalNumbers() {
>        for ($i = 0; ; ++$i) {
>            yield $i;
>        }
>    }
>
>    // all natural numbers
>    $numbers = naturalNumbers();
>    // only the odd ones
>    $oddNumbers = (foreach ($numbers as $n) if ($n % 2) yield $n);
>    // ...
>
> (At this point I wonder whether one should include support for
> for-loops in list comprehensions. So the naturalNumbers() function
> could be replaced with (for ($i = 0;; ++$i) yield $i), etc)
>
> So, what do you think? Do we want something like this in PHP?
>
> Nikita
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--bcaec52c6a9d398cfc04c387ac62--