在D中返回函数的函数的纯函数 [英] pure function of functions that returns functions in D
问题描述
纯Func multiplyFunctions()函数返回一个纯函数,返回两个其他纯函数的乘法: Func,Real)(范围为const Func f1,范围为const Func f2)
{
return(Real a){return f1(a)* f2(a); };
}
不幸的是,我遇到了问题,头号问题,我想声明f1和f2是纯函数/委托/类与opCall定义...这是必需的,因为我从一个纯函数调用它们。
但是第二,似乎是最有问题的是,我想f1和f2是一个功能,真正的变量,返回一个真正的价值......但我不知道如何模板了。 。
任何人有任何想法? 首先,删除作用域
;这是错误的,因为代表的范围是逃脱。
其次,尝试类似:
真实委托(真实)乘数(真实委托(真实)纯粹f1,
真实委托(真实)纯粹f2)纯粹
{
真实乘数(真实) val){return f1(val)* f2(val); }
return& multiply;
}
您也可以尝试使用模板来做这件事,虽然没有太多理由到:
纯自动乘数(别名F1,别名F2)(ParameterTypeTuple!F1参数)
{
返回F1(args)* F2(args);
}
real square(real a)pure {return a * a; }
别名乘数!(平方,平方)squareMultiplier;
//现在squareMultiplier是square()
的倍数请注意,在编译器中有错误,它们不允许纯度100%正确,所以你现在只需要忍受它们。
I'm trying to create a pure function that returns the multiplication of two other pure functions:
pure Func multiplyFunctions(Func,Real)(scope const Func f1, scope const Func f2)
{
return (Real a) { return f1(a) * f2(a); };
}
Unfortunately, I'm running into problems, number one, I want to declare f1 and f2 to be pure functions/delegates/classes with opCall defined... which is required because I'm calling them from a pure function.
But number two, and what seems to be the most problematic, is that I want f1 and f2 to be functions of one, "Real" variable that return one "Real" value... but I can't figure out how to template this out...
Anyone have any ideas?
First of all, remove the scope
; it's wrong, because the scopes of the delegates are escaped.
Second, try something like:
real delegate(real) multiplier(const real delegate(real) pure f1,
const real delegate(real) pure f2) pure
{
real multiply(real val) { return f1(val) * f2(val); }
return &multiply;
}
You can also try doing this with templates, although there's not much of a reason to:
pure auto multiplier(alias F1, alias F2)(ParameterTypeTuple!F1 args)
{
return F1(args) * F2(args);
}
real square(real a) pure { return a * a; }
alias multiplier!(square, square) squareMultiplier;
//Now squareMultiplier is a multiplier of square()
Note that there are bugs in the compiler that don't allow purity to be 100% correct, so you'll just have to put up with them for now.
这篇关于在D中返回函数的函数的纯函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!