Ferenc Kovacs wrote:
So now you understand how the proposed Generator implementation works?
If you think that Iterators require a bunch of boilerplate code, I can
understand that, and that was one of the motivations behind the Generators rfc I
guess.
If you think that using Iterators (hence reversing the process control between
the consumer and producer) is always wrong, I cannot agree with that.
There are cases when you don't care about how the data get created/filtered, you
just want to iterate over it, without the need to build a huge array and only
start working on it, when the whole thing is ready.
Having been working with databases for 20+ years one gets used to ensuring only the required data is accessed in the first place ;) Invariably I'm working with a cursor on a data source of some sort - so no big array.

Your style of processing data is tightly coupling the generation of the data and
the processing of the data, which works for you, but in some scenarios and for
some people it is a bad architectural design.

I think this is a good example of where 'you don't need to use it' is just the wrong attitude. There are good and bad examples of everything because there are many ways of doing things. PHP IS just too flexible. This is why I keep banging on about 'examples of good coding practice' - heck *I* need them so that I can tidy up 10 years worth of code! From my point of view the generators rfc is simply not presenting the case in a good light and even reading it today, the 'comparisons' against what is already bad code grate :( I'd not be doing that in the first case comes to mind ... so switch off.

Working out the flow through
$lines = getLinesFromFile($fileName);
foreach ($lines as $line) {
    // do something with $line
}
Where yield is used in 'getLinesFromFile' still does not look logical to me, and if we break out of the foreach loop just how does '$lines' get closed out later?

I am assuming that the 'getLinesFromFile' is being actioned once for each cycle of the foreach loop so has to keep it's state, it's not being able to 'see' the magic that is the hidden ->next() which is the problem. I think that THIS is why I'd prefer to see a 'generator function' header so that straight away I know there is some magic under the hood to do with THIS particular function. Scanning a function and then finding a yield later then needs you to back wind and understand that is IS an interruptible function. Generators just seem to be hiding the boilerplate code that an interator needs to display?

I accept your point about not caring about how the data was created, but on the other side, if the data creation is handling a lot more data than the consumer needs there is an amount of processing time that is wasted. The quick way of doing something does not equate to the best way of doing it.

--
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk



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

Reply via email to