“深”函数使用模板元编程在C ++中进行curry [英] "Deep" function currying in C++ using template metaprogramming
问题描述
我刚刚想出了一个(另一个!)实现的函数currying在C + +使用模板元编程。 (我几乎肯定其他实现比我更好/更完整,但我这样做是为了学习目的,我认为重新发明的车轮是合理的。)
我的函数currying实现,测试用例包括,是以下:
#include< iostream>
#include< functional>
template< typename>咖喱类
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:
显式curry(_Fun fun)
:_fun(fun){}
_Ret运算符b {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;
}
这个函数currying实现是shallow,在以下意义上: std :: function
的签名是...
arg1,arg2,arg3 ...) res
然后curry函数的签名是...
arg1 - > arg2 - > arg3 - > ... - 但是,如果任何参数或返回类型本身可以curry,他们不会得到咖喱。例如,如果原始 std :: function
的签名是... (((arg1,arg2)→tmp),arg3) curry函数的签名将是...
$ b
< $ b ((arg1,arg2) - > tmp) - > arg3 - > res
而不是...
(arg1→> arg2 - > tmp) - > arg3 - > res
这是我想要的。所以我想有一个深层currying实现。有谁知道我该如何写?
@vhallac: b
$ b
这是应该传递给 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)>
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;
解决方案 $ c> decurry
类演示如何实现专业化。该版本是欺骗,因为它被宣布为 curry< T>
的朋友,并访问内部 _fun
将curry版本的函数转换为原始版本。
崩溃
implementation is:
模板< 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操作符(_Args ... rest){
return _fn._fun ...);
}
private:
_Curried _fn;
};
它需要行:
friend class decurry< curry< _Res(_Arg,_Args ...)> > ;;
里面 class curry< _Res(_Arg,_Args ...)>
,以授予我们的类访问 curry< T> ._ fun
。
现在,专业化可以写成:
模板< 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:
显式curry(_Fun fun)
:_fun(fun){}
_Ret运算符b {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)>
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 。
I just came up with an (yet another!) implementation of function currying in C++ using template metaprogramming. (I am almost sure other implementations are better / more complete than mine, but I am doing this for learning purposes, a case in which I think reinventing the wheel is justified.)
My funcion currying implementation, test case included, is the following one:
#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;
}
This function currying implementation is "shallow", in the following sense: If the original std::function
's signature is...
(arg1, arg2, arg3...) -> res
Then the curried function's signature is...
arg1 -> arg2 -> arg3 -> ... -> res
However, if any of the arguments or the return type themselves can be curried, they do not get curried. For example, if the original std::function
's signature is...
(((arg1, arg2) -> tmp), arg3) -> res
Then the curried function's signature will be...
((arg1, arg2) -> tmp) -> arg3 -> res
Instead of...
(arg1 -> arg2 -> tmp) -> arg3 -> res
Which is what I want. So I would like to have a "deep" currying implementation. Does anyone know how I could write it?
@vhallac:
This is the kind of function that should be passed to the constructor of curry<int(int(int,int),int)>
:
int test(std::function<int(int,int)> f, int x)
{ return f(3, 4) * x; }
Then one should be able to do the following:
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;
解决方案 I have implemented a cheating version of a decurry
class to demonstrate how you would go about implementing the specialization. The version is cheating, because it gets declared as a friend of curry<T>
, and accesses the internal _fun
to convert a curried version of a function back to original. It should be possible to write a generic one, but I didn't want to spend more time on it.
The decurry
implementation is:
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;
};
And it requires the line:
friend class decurry< curry<_Res(_Arg, _Args...)> >;
inside class curry< _Res(_Arg, _Args...) >
to give our class access to curry<T>._fun
.
Now, the specialization can be written as:
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))); }
};
The test code is a specified in the question:
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;
}
The code output is on Ideone.com again.
这篇关于“深”函数使用模板元编程在C ++中进行curry的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!