www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - AdvancedDelegate - "Partial application" for delegates and function

reply Markus Dangl <danglm in.tum.de> writes:
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

I often miss some features of functional programming languages when 
coding in imperative / oo languages. But i really love D, so i had to 
write something that allows me to use partial application in D.

What i always wanted to do was: Give another object a delegate which has 
a part of it's parameters already given, like this:

void Update(Child c, int a, int b, int c) { ... }

child.OnUpdate = &this.Update(child);

Where the Child wouldn't be able to tell the difference between this 
(partially applied) delegate and a delegate of type "void 
delegate(int,int,int).

The AdvancedDelegate templates solve this in a type-safe manner. The 
only problem remaining was: i had to write different templates for a 
different number of parameters. Since this would have been very 
repetative, i hacked together a PHP script that does this for me.
Now you can do something like:

child.OnUpdate = new 
AdvancedDelegateP4G1!(void,Child,int,int,int)(&this.Update, child);

Where child.OnUpdate would be of the type 
"AdvancedDelegate3!(void,int,int,int);"

For more examples, see the "demo.d" file.

The attached ZIP file contains a script (written in PHP because i just 
hacked it together) that generates the "advanceddelegate" library for 
any given number of parameters. Since not everyone uses PHP, i generated 
the library for up to 10 parameters.

You can use delegates and functions with this library, they are fully 
interchangeable.

If this is useful in any way, please provide me some feedback - without 
feedback i wont be in the mood to develop it any further than it is now ;)
Jun 13 2006
next sibling parent reply Walter Bright <newshound digitalmars.com> writes:
I think it's pretty cool. My only suggestion is to ditch the php script 
and write a D script to do the same thing!
Jun 13 2006
next sibling parent Markus Dangl <danglm in.tum.de> writes:
Walter Bright schrieb:
 I think it's pretty cool. My only suggestion is to ditch the php script 
 and write a D script to do the same thing!

I only wrote it today, i used PHP because i can hack it so damn fast. The script is a mess, especially the variable names :) So writing a nice d script will be the next step!
Jun 13 2006
prev sibling next sibling parent Markus Dangl <danglm in.tum.de> writes:
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Walter Bright schrieb:
 I think it's pretty cool. My only suggestion is to ditch the php script 
 and write a D script to do the same thing!

Ok, here it is! Changes: - added opCall() as a shortcut for Eval() in AdvancedDelegate0 - the library is now generated by a D script instead of a PHP script - removed most of the aliases in "all.d" as they aren't needed when you use the helpers
Jun 14 2006
prev sibling parent "Craig Black" <cblack ara.com> writes:
"Walter Bright" <newshound digitalmars.com> wrote in message 
news:e6nhnp$1cd5$2 digitaldaemon.com...
I think it's pretty cool. My only suggestion is to ditch the php script and 
write a D script to do the same thing!

Or if you added variable template parameters we wouldn't need a script at all :) -Craig
Jun 15 2006
prev sibling parent Markus Dangl <danglm in.tum.de> writes:
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

IMPORTANT UPDATE:
I suddenly realised how helpful the auto keyword is, and i added a few 
helper functions to make things even more easier. You can now write:

auto add = AdvancedFunction(
	function int(int a, int b) { return a+b; }
);
writeln("add(2,2): ", add(2)(2).Eval);

The same goes for delegates, except i had to call the helper template 
"AdvancedDelegate". Seems i cant overload two templates like this:

AdvancedDelegate0!(R) AdvancedDelegate(R) (R delegate() dg)
{
     return new AdvancedDelegateP0G0!(R)(dg);
}

AdvancedDelegate0!(R) AdvancedDelegate(R) (R function() dg)
{
     return new AdvancedFunctionP0G0!(R)(dg);
}

But i don't think it is a big problem.
Jun 13 2006