Iterators and generators - MDC Doc Center

While custom iterators are a useful tool, their creation requires careful programming due to the need to explicitly maintain their internal state. Generators provide a powerful alternative: they allow you to define an iterative algorithm by writing a single function which can maintain its own state.

A generator is a special type of function that works as a factory for iterators. A function becomes a generator if it contains one or more yield expressions.

Javascript 的作法:
我們提供 Iterator 物件,也讓你用 prototype 來定義 custom iterator

如果你不想自己寫,我們也提供 iterator 的 factory,generator,
此時的作法就要用關鍵字 yield

What is the difference between an Iterator and a Generator? - Stack Overflow

An iterator traverses a collection one at a time.

A generator generates a sequence, one item at a time.

You might for example, iterate over the result of a generator...

Generators are iterators, but not all iterators are generators.

An iterator is typically something that has a next method to get the next element from a stream. A generator is an iterator that is tied to a function.

A generator is an implementation of an iterator.

 王小旺 
@ 
@ 其實我沒仔細研究過,我的理解是:「iterator」是用途、「generator」是作法。 :/

PHP Tutorials Examples Introduction to SPL

<?php
    
/*** class definition to extend Directory Iterator class ***/
    
class DirectoryReader extends DirectoryIterator
    
{
        
// constructor.. duh!
        
function __construct($path)
        {
            
/*** pass the $path off to the parent class constructor ***/
            
parent::__construct($path
);
        }

        

/*** return the current filename ***/
        
function current()
        {
            return 
parent::getFileName
();
        }

        

/*** members are only valid if they are a directory ***/
        
function valid()
        {
            if(
parent::valid())
            {
                if (!
parent::isDir())
                {
                    
parent::next();
                    return 
$this->valid();
                }
            return 
TRUE;
            }
            return 
FALSE
;
        }

    } 

// end class

    

try
    {
        
/*** a new iterator object ***/
        
$it = new DirectoryReader('./');
        
/*** loop over the object if valid ***/
        
while($it->valid())
        {
            
/*** echo the current object member ***/
            
echo $it->current().'<br />';
            
/*** advance the internal pointer ***/
            
$it->next();
        }
    }
    catch(
Exception $e)
    {
        echo 
'No files Found!<br />'
;
    }

?>

Overload Standard PHP Library 的 Iterator

Understanding and Using Iterators - Perl.com

sub gen_fib {
    my ($low, $high) = (1, 0);

    return sub {
        ($low, $high) = ($high, $low + $high);
        return $high;
    };
}

my $fib = gen_fib();
print $fib->(), "\n" for 1 .. 20;

Besides the funny initialization of $low being greater than $high, it also misses 0, which should be the first item returned. Here is one way to handle it:

sub gen_fib {

    my ($low, $high) = (1, 0);

    my $seen_edge;

    return sub {
        return 0 if ! $seen_edge++;
        ($low, $high) = ($high, $low + $high);
        return $high;
    };
}

State Variables Persist As Long As the Iterator

在古早的 Perl 中,iterator (customized iterator / generator) 可以用 closure 機制來實作。

需要 generator 的情況:用少一點記憶體、無窮元素、循環、太大。文中對每個情況都給一個例子。