Generators are now in PHP 5.5

Generators (and therefore coroutines) are now part of PHP (Wikipedia) 5.5, as of 20-Jun-2013. Here is an example:

function xrange($start,$end) {
    for ($i = $start; $i<=$end; ++$i)
        yield($i);
}

The Icon programming language (Wikipeda) was one of the first computer languages where generators are completely general and may occur in any computation. Icon is goal-directed in the sense that the evaluation mechanism attempts to produce at least one result for all expressions. yield is analogous to Icon’s suspend.

Icon can limit generators, PHP apparently cannot. Icon uses

expr \ i

for this limitation.

The Python programming language also provides generators. A simple example is

def xrange(a,b):
    for i in range(a,b):
        yield i
Advertisements