shouting versus dotting
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Tue Oct 7 09:01:48 PDT 2008
Benji Smith wrote:
> Hmmmmmm... I still hate opCall, though :)
One context in which I found opCall very valuable is parameterized
functions: functions that take, say, 1 argument but are parameterized by
another. An example is a Gaussian kernel of parameterized bandwidth:
double gaussianKernel(double d)
{
return exp(-d * d / alpha * alpha);
}
We'd like to be able to configure alpha properly (not via a global,
because there could be several Gaussian kernels around). A solution
would be to use delegates, but that's inefficient and a kernel is
evaluated so often it's not even funny. So a good solution is to make
the function a struct with state:
struct GaussianKernel
{
private double alpha2InvNeg = -1;
double opCall(double d) const
{
return exp(d * d * alpha2InvNeg);
}
void alpha(double a)
{
enforce(a > 0);
alpha2InvNeg = -1 / (a * a);
}
double alpha() const
{
return sqrt(-1 / alpha2InvNeg);
}
}
Andrei
More information about the Digitalmars-d
mailing list