Generator (computer programming): Difference between revisions

Content deleted Content added
Python: PEP 380 yield from
Line 303:
}
</source>
 
Moreover, [[C++11]] allows foreach loops to be applied to any class that provides the <code>begin</code> and <code>end</code> functions. It's then possible to write generator-like classes by overloading having the <code>begin</code> function returning the class instance itself and having the class implement the iterator syntax. For example, it is possible to write the following program:
 
<source lang="cpp">
#include <iostream>
int main()
{
for (int i: range(10))
{
std::cout << i << std::endl;
}
return 0;
}
</source>
 
A basic range implementation would look like that:
 
<source lang="cpp">
class range
{
range(int end):
last(end),
iter(0)
{}
 
// Iterable functions
range& begin() const { return *this; }
range& end() const { return *this; }
 
// Iterator functions
bool operator!=(const range&) const { return i < end; }
void operator++() { ++iter; }
int operator*() const { return iter; }
};
 
=== Perl ===