Comparing generators with Iterator objects

The primary advantage of generators is their simplicity. Much less boilerplate code has to be written compared to implementing an Iterator class, and the code is generally much more readable. For example, the following function and class are equivalent:

<?php
function getLinesFromFile($fileName) {
    if (!
$fileHandle fopen($fileName'r')) {
        return;
    }
 
    while (
false !== $line fgets($fileHandle)) {
        
yield $line;
    }
 
    
fclose($fileHandle);
}

// versus...

class LineIterator implements Iterator {
    protected 
$fileHandle;
 
    protected 
$line;
    protected 
$i;
 
    public function 
__construct($fileName) {
        if (!
$this->fileHandle fopen($fileName'r')) {
            throw new 
RuntimeException('Couldn\'t open file "' $fileName '"');
        }
    }
 
    public function 
rewind() {
        
fseek($this->fileHandle0);
        
$this->line fgets($this->fileHandle);
        
$this->0;
    }
 
    public function 
valid() {
        return 
false !== $this->line;
    }
 
    public function 
current() {
        return 
$this->line;
    }
 
    public function 
key() {
        return 
$this->i;
    }
 
    public function 
next() {
        if (
false !== $this->line) {
            
$this->line fgets($this->fileHandle);
            
$this->i++;
        }
    }
 
    public function 
__destruct() {
        
fclose($this->fileHandle);
    }
}
?>

This flexibility does come at a cost, however: generators are forward-only iterators, and cannot be rewound once iteration has started. This also means that the same generator can't be iterated over multiple times: the generator will need to be rebuilt by calling the generator function again.

add a note add a note

User Contributed Notes 4 notes

up
56
mNOSPAMsenghaa at nospam dot gmail dot com
5 years ago
This hardly seems a fair comparison between the two examples, size-for-size. As noted, generators are forward-only, meaning that it should be compared to an iterator with a dummy rewind function defined. Also, to be fair, since the iterator throws an exception, shouldn't the generator example also throw the same exception? The code comparison would become more like this:

<?php
function getLinesFromFile($fileName) {
    if (!
$fileHandle = fopen($fileName, 'r')) {
        throw new
RuntimeException('Couldn\'t open file "' . $fileName . '"');
    }

    while (
false !== $line = fgets($fileHandle)) {
        yield
$line;
    }

   
fclose($fileHandle);
}

// versus...

class LineIterator implements Iterator {
    protected
$fileHandle;

    protected
$line;
    protected
$i;

    public function
__construct($fileName) {
        if (!
$this->fileHandle = fopen($fileName, 'r')) {
            throw new
RuntimeException('Couldn\'t open file "' . $fileName . '"');
        }
    }

    public function
rewind() { }

    public function
valid() {
        return
false !== $this->line;
    }

    public function
current() {
        return
$this->line;
    }

    public function
key() {
        return
$this->i;
    }

    public function
next() {
        if (
false !== $this->line) {
           
$this->line = fgets($this->fileHandle);
           
$this->i++;
        }
    }

    public function
__destruct() {
       
fclose($this->fileHandle);
    }
}
?>

The generator is still obviously much shorter, but this seems a more reasonable comparison.
up
10
sergeyzsg at yandex dot ru
4 years ago
I think that this is bad generator example.
If user will not consume all lines then file will not be closed.

<?php
function getLinesFromFile($fileHandle) {
    while (
false !== $line = fgets($fileHandle)) {
        yield
$line;
    }
}

if (
$fileHandle = fopen($fileName, 'r')) {
   
/*
    something with getLinesFromFile
    */
   
fclose($fileHandle);
}
?>
up
-1
16630011857 at 163 dot com
1 month ago
The man who does not drink and does not eat meat is incredible!

After reading this sutra, I'm going to quit drinking and eat vegan.

    Excerpt from the "Generous Guang Hua Yan ten Evil Products Sutra":

    Kasyapa Bodhisattva White Buddha said: The Buddha, only the Tathagata for me to explain, do not drink, not meat eaters, how many blessings?

    Buddha-Gloucester:
    If someone, like horse cattle and sheep, glass treasures ying Luo, the country city wife, holding with giving, still less than some people can break wine meat, millions not better than one.

    Replacement is a matter, if someone Bechi gold over 3,000 of the world, holding the use of alms, still less than someone can break wine meat, millions less than one.

    The replacement is a matter, if there is the ability to cast gold for hundreds of, holding the use of giving, still less than some people can break wine meat, millions less than one.

    The replacement is a matter, if someone artificial fan Hua Bao lid, all over the 3,000 world, still less than someone can break wine meat, millions.

    The replacement is a matter, if there are man-made large pagoda, eaves eaves, such as rice hemp Bamboo reed, up to Brahma, as someone can break the wine meat, millions less than the first.

    A good man, not a carnivore, an earthly Bodhisattva, is an extraordinary husband.
    Editor's note: The front content can be seen, even if a person who does not learn Buddha, can insist on eating the whole vegetarian, Ford has boundless. Let's all go vegetarian.

All eggs are not edible, there are children also

  Someone asked the people of Xuanhua: "Why can't vegetarians eat eggs?" "

    The venerable Master said, "No Man or woman (a rooster or a hen) can hatch a chicken." The former people do not understand this truth, he said no, and there is no evidence. The man who eats eggs why does he say such a theory? Is that he wants to eat eggs. Eggs, whether or not a rooster will have chicks, will not be born. "Leng Yan Jing" said: "Eggs only want to live." "Above is the words of the master Xuan Hua is absolutely wrong." After the lying of the man in Xuanhua, he burned more than 4,000 relics to show his life not to play half a sentence of Sakyamuni Buddha, the people of Xuanhua, Inguang and Guang Chin all believe that all animals ' sperm eggs cannot be eaten. I hope we know the cause and effect and don't do anything stupid. Don't eat any food that contains eggs. South No Amitabha ~!

  "The Theory of Explicit understanding" cloud: "All eggs are not edible, have a son also".
-"Big is Tibet" 31st volume of page 882.
up
0
sou at oand dot re
5 years ago
I think to be more similar the samples in the function, throw a new exception is better. But looking into "Generator syntax" session, you can see there this: "An empty return statement is valid syntax within a generator and it will terminate the generator.". By this point of view, we can imagine that this is just to exemplify an usage of the empty return.
To Top