Simple and quite easy to use iterator library for D
Witold Baryluk
baryluk at mpi.int.pl
Sun Jan 28 10:24:37 PST 2007
Hello,
http://smp.if.uj.edu.pl/~baryluk/d/iterators-1.0.tar.gz
documentation and samples included.
Feel free to comment, providing bug report, or nice examples. :)
Performance note: about 5% slowdown in comparison to hand writen loops.
Examples:
class Naturals(int n) {
int iter() {
for (int i = 1; i <= n; i++) yield(i);
return 0;
}
mixin mainiter!(int, iter); // mixin iterator's stuff
}
...
foreach (int x; new Naturals!(10)()) {
...
}
Multiple iterators:
class MNaturals(int n) {
int iter2(Iterator!(double) x) {
for (int i = 1; i <= n; i++) x.yield(i*i);
return 0;
}
mixin inneriter!(double, iter2) squers;
int iter3(Iterator!(double) x) {
for (int i = 1; i <= n; i++) x.yield(i*i*i);
return 0;
}
mixin inneriter!(double, iter3) qubes;
}
...
auto s = new MNaturals!(10)();
foreach (int x; s.squers.ic()) {
...
}
foreach (int x; s.qubes.ic()) {
...
}
--
Witold Baryluk
More information about the Digitalmars-d-announce
mailing list