Can we get rid of opApply?
Denis Koroskin
2korden at gmail.com
Tue Jan 20 01:23:58 PST 2009
One nice thing that opApply capable of is it can avoid heap activity by stack-allocating data during iteration.
For example, given an array of ints, iterate over string representations of them:
struct IntegersAsString
{
void opAplly(int delegate(string s) dg)
{
char[16] temp;
foreach (i; array) {
int len = sprintf(temp, "%d", i);
int result = dg(temp[0..len]);
if (result != 0) {
return result;
}
}
return 0;
}
private int[] array;
}
int array = [1, 1, 2, 3, 5, 8, 13];
// no heap allocation take place
foreach (string s; IntegersAsString(array)) {
writeln(s);
}
How would you do that with ranges?
More information about the Digitalmars-d
mailing list