Hi internals!
Python and several other languages include support for list
comprehensions and generator expressions and I'd like to see something
why PHP should going to like other language?
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 asarray_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 <
- 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?
NO!!!
PHP is not python please do not make PHP worse
if you like python just go to use python
Nikita
Hi internals!
Python and several other languages include support for list
comprehensions and generator expressions and I'd like to see something
why PHP should going to like other language?
Pretty much everything in PHP was "borrowed" from another language. This
is nothing new.
So, what do you think? Do we want something like this in PHP?
NO!!!PHP is not python please do not make PHP worse
if you like python just go to use python
Try again, this time with less knee-jerk and more brain:
Why is this a bad feature?
How will it make PHP worse?
What is so bad about it that it outweigh the benefits?
Is it the syntax you don't like, or the concept?
Cheers,
David