[英]“Deep” function currying in C++ using template metaprogramming
我只是想出了使用模板元編程在C ++中實現函數泛濫的(還有另一個!)實現。 (我幾乎可以肯定,其他實現比我的實現更好/更完善,但是出於學習目的,我這樣做是出於這種考慮,我認為重新發明輪子是合理的。)
我的funcion currying實現(包括測試用例)如下:
#include <iostream>
#include <functional>
template <typename> class curry;
template <typename _Res>
class curry< _Res() >
{
public:
typedef std::function< _Res() > _Fun;
typedef _Res _Ret;
private:
_Fun _fun;
public:
explicit curry (_Fun fun)
: _fun(fun) { }
operator _Ret ()
{ return _fun(); }
};
template <typename _Res, typename _Arg, typename... _Args>
class curry< _Res(_Arg, _Args...) >
{
public:
typedef std::function< _Res(_Arg, _Args...) > _Fun;
typedef curry< _Res(_Args...) > _Ret;
private:
class apply
{
private:
_Fun _fun;
_Arg _arg;
public:
apply (_Fun fun, _Arg arg)
: _fun(fun), _arg(arg) { }
_Res operator() (_Args... args)
{ return _fun(_arg, args...); }
};
private:
_Fun _fun;
public:
explicit curry (_Fun fun)
: _fun(fun) { }
_Ret operator() (_Arg arg)
{ return _Ret(apply(_fun, arg)); }
};
int main ()
{
auto plus_xy = curry<int(int,int)>(std::plus<int>());
auto plus_2x = plus_xy(2);
auto plus_24 = plus_2x(4);
std::cout << plus_24 << std::endl;
return 0;
}
在以下意義上,此函數周期性實現為“淺”:如果原始std::function
的簽名為...
(arg1, arg2, arg3...) -> res
那么curried函數的簽名是...
arg1 -> arg2 -> arg3 -> ... -> res
但是,如果任何參數或返回類型本身可以被管理,則它們不會被管理。 例如,如果原始std::function
的簽名是...
(((arg1, arg2) -> tmp), arg3) -> res
然后咖喱函數的簽名將是...
((arg1, arg2) -> tmp) -> arg3 -> res
代替...
(arg1 -> arg2 -> tmp) -> arg3 -> res
這就是我想要的。 因此,我希望有一個“深層”的可實現的實現。 有人知道我怎么寫嗎?
@vhallac:
這是應該傳遞給curry<int(int(int,int),int)>
的構造函數的函數:
int test(std::function<int(int,int)> f, int x)
{ return f(3, 4) * x; }
然后,一個人應該能夠執行以下操作:
auto func_xy = curry<int(int(int,int),int)>(test);
auto plus_xy = curry<int(int,int)>(std::plus<int>());
auto func_px = func_xy(plus_xy);
auto func_p5 = func_px(5);
std::cout << func_p5 << std::endl;
我已經實現了一個decurry
類的作弊版本,以演示如何實現專業化。 該版本是作弊的,因為它被聲明為curry<T>
的朋友,並訪問內部_fun
將函數的咖喱版本轉換回原始版本。 應該可以編寫一個通用的,但是我不想花更多的時間在上面。
decurry
實現為:
template <typename _Res, typename... _Args>
class decurry< curry<_Res(_Args...)> > {
public:
typedef curry<_Res(_Args...)> _Curried;
typedef typename curry<_Res(_Args...)>::_Fun _Raw;
decurry(_Curried fn): _fn(fn) {}
_Res operator() (_Args... rest) {
return _fn._fun(rest...);
}
private:
_Curried _fn;
};
它需要以下行:
friend class decurry< curry<_Res(_Arg, _Args...)> >;
在class curry< _Res(_Arg, _Args...) >
以使我們的類可以訪問curry<T>._fun
。
現在,專業化可以寫成:
template <typename _Res, typename _Res2, typename... _Args2, typename... _Args>
class curry< _Res(_Res2(_Args2...), _Args...) >
{
public:
typedef curry< _Res2(_Args2...) > _Arg;
typedef std::function< _Res2(_Args2...) > _RawFun;
typedef std::function< _Res(_RawFun, _Args...) > _Fun;
typedef curry< _Res(_Args...) > _Ret;
private:
class apply
{
private:
_Fun _fun;
_RawFun _arg;
public:
apply (_Fun fun, _RawFun arg)
: _fun(fun), _arg(arg) { }
_Res operator() (_Args... args)
{ return _fun(_arg, args...); }
};
private:
_Fun _fun;
public:
explicit curry (_Fun fun)
: _fun(fun) { }
_Ret operator() (_Arg arg)
{ return _Ret(apply(_fun, decurry<_Arg>(arg))); }
};
測試代碼是在問題中指定的:
int test(std::function<int(int,int)> f, int x)
{ return f(3, 4) * x; }
int main ()
{
auto func_xy = curry<int(int(int,int),int)>(test);
auto plus_xy = curry<int(int,int)>(std::plus<int>());
auto func_px = func_xy(plus_xy);
auto func_p5 = func_px(5);
std::cout << func_p5 << std::endl;
return 0;
}
代碼輸出再次在Ideone.com上 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.