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. If you already have the data, of course you
can't un-evaluate it in order to lazily-evaluate it again. But why would
you, that would be just a waste of time! If you don't yet have the data,
then you usually can easily rewrite the code that produces it as a
generator if you wanted, but in most cases you don't need it as it would
be more complex and won't give you any advantage (exceptions of course
exist like processing enormous files or processing infinite streams
continuously). So I am not seeing the point of mentioning "eager to
lazy" case as it is usually does not happen in practice.

> $hugeCSVFileIterator = new  CSVFileLazyLoader($filename);
> $filteredCSVFileIterator = [ foreach $hugeCSVFileIterator as $line if
> $line['type'] == 'foo' yield $line['value'] ];

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.

> Similarly, an array-only syntax is useless for infinite iterators, whereas
> an iterator version lets you write a filtered version that's still infinite.

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.

> 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.
-- 
Stas Malyshev
smalys...@gmail.com

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to