www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - currying

reply Thorsten <thorstenkiefer gmx.de> writes:
Hi, party people, because of my Haskell experience, I like the currying example
of the online manual. I altered it a bit. How do you like this :

R delegate(X) Curry1(R,X,U...)(R delegate(U) dg,X arg)
{
	struct Foo
	{
		typeof(dg) dg_m;
		X arg_m;
		
		R bar(U[1..$] args)
		{
			return dg_m(arg_m,args);
		}
	}
	
	Foo* f = new Foo;
	f.dg_m = dg;
	f.arg_m = arg;
	
	return &f.bar;
}


void main(char[][] args){
	int plus(int a,int b){
		return a + b;
	}
	
	auto b = Curry1(&plus,1);
	writefln("%d",b(2));
}

Best Regards
Thorsten
Mar 03 2007
next sibling parent Thorsten <thorstenkiefer gmx.de> writes:
Sorry, I have to correct it :

R delegate(U) Curry1(R,X,U...)(R delegate(X,U) dg,X arg)
{
	struct Foo
	{
		typeof(dg) dg_m;
		X arg_m;
		
		R bar(U args)
		{
			return dg_m(arg_m,args);
		}
	}
	
	Foo* f = new Foo;
	f.dg_m = dg;
	f.arg_m = arg;
	
	return &f.bar;
}
Mar 03 2007
prev sibling parent Thorsten <thorstenkiefer gmx.de> writes:
Sorry, I have to correct it :

R delegate(U) Curry1(R,X,U...)(R delegate(X,U) dg,X arg)
{
	struct Foo
	{
		typeof(dg) dg_m;
		X arg_m;
		
		R bar(U args)
		{
			return dg_m(arg_m,args);
		}
	}
	
	Foo* f = new Foo;
	f.dg_m = dg;
	f.arg_m = arg;
	
	return &f.bar;
}
Mar 03 2007