Higher-order functions: Difference between revisions

Updated both D versions
(Updated both D versions)
Line 523:
 
=={{header|D}}==
int<lang someprocedured>int hof(int a, int b, int delegate (int, int) f) {
D's function/delegate will be investigated by passing each type of function/delegate to _test_ as argument.
return f(a, b);
<lang d>module functest ;
import std.stdio ;
 
void main() {
// test the function argument
import std.stdio ;
writeln("addAdd: ", someprocedurehof(2, 3, (int a, int b) { return=> a + b; } ));
writeln("multiplyMultiply: ", someprocedurehof(2, 3, (int a, int b) { return=> a * b; } ));
}</lang>
{{out}}
<pre>Add: 5
Multiply: 6</pre>
D'sThis function/delegatelonger willand bemore investigatedsystematic example shows D functions/delegates by passing each type of function/delegate to _test_ as argument.
<lang d>import std.stdio, std.functional;
 
// testTest the function argument.
string test(U)(string scopes, U func) {
string typeStr = typeid(typeof(func)).toString ();
 
string isFunc = (typeStr[$ - 1] == '*') ? "function" : "delegate" ;
writefln("Hi, %-13s : scope: %-8s (%s) : %s", func(), scopes, isFunc, typeStr ) ;
return scopes ;
func(), scopes, isFunc, typeStr );
return scopes ;
}
 
// normalNormal module level function .
string aFunction() { return "Function" ; }
 
// Implicit-Function-Template-Instantiation (IFTI) Function.
T tmpFunc(T)() { return "IFTI.function" ; }
 
// Member in a template.
template tmpGroup(T) {
T t0(){ return "Tmp.member.0" ; }
T t1(){ return "Tmp.member.1" ; }
T t2(){ return "Tmp.member.2" ; }
}
 
// usedUsed for implementing member function at class & struct.
template Impl() {
static string aStatic() { return "Static Method" ; }
string aMethod() { return "Method" ; }
}
class C { mixin Impl!() ; }
struct S { mixin Impl!() ; }
 
class C { mixin Impl!() ; }
void main() {
struct S { mixin Impl!() ; }
// nested function
string aNested(){
return "Nested" ;
}
 
void main () {
// bind to a variable
// nestedNested function.
auto variableF = function string() { return "variable.F"; } ;
string aNested() {
auto variableD = delegate string() { return "variable.D"; } ;
return "Nested" ;
} }
 
C// cBind =to newa C ;variable.
auto variableF = function string() { return "variable.F"; } ;
S s ;
auto variableD = delegate string() { return "variable.D"; } ;
 
C c = new C;
"Global".test(&aFunction) ;
S s ;
"Nested".test(&aNested) ;
 
"Class".test(&C.aStatic)
"Global".test(&c.aMethodaFunction) ;
"StructNested".test(&S.aStaticaNested) ;
"Class".test(&sC.aMethodaStatic) ;
"Template" .test(&tmpFunc!(string)c.aMethod);
"Struct".test(&tmpGroup!(string)S.t2aStatic) ;
"Binding" .test(variableF&s.aMethod);
"Template".test(variableD&tmpFunc!(string)) ;
.test(&tmpGroup!(string).t2);
// leteral function/delegate
"ClassBinding".test(&C.aStaticvariableF)
"Literal".test(function string() { return "literal.F"; })
.test(delegate string(variableD) { return "literal.D"; }) ;
// leteralLiteral function/delegate.
"Literal".test(function string() { return "literal.F"; })
.test(delegate string() { return "literal.D"; });
}</lang>
{{out}}}
 
<pre>Hi, Function : scope: Global (function) : immutable(char)[]()*
 
Hi, Nested : scope: Nested (delegate) : immutable(char)[] delegate()
There is a simpler version:
Hi, Static Method : scope: Class (function) : immutable(char)[]()*
 
Hi, Method : scope: Class (delegate) : immutable(char)[] delegate()
<lang d>
Hi, Static Method : scope: Struct (function) : immutable(char)[]()*
import std.stdio, std.functional;
Hi, Method : scope: Struct (delegate) : immutable(char)[] delegate()
 
Hi, IFTI.function : scope: Template (function) : immutable(char)[]()*
int someprocedure (int a, int b, int delegate (int, int) f)
Hi, Tmp.member.2 : scope: Template (function) : immutable(char)[]()*
{
Hi, variable.F : scope: Binding (function) : immutable(char)[]()*
return f(a, b);
Hi, variable.D : scope: Binding (delegate) : immutable(char)[] delegate()
Hi, literal.F : scope: Literal (function) : immutable(char)[]()*
 
Hi, literal.D : scope: Literal (delegate) : immutable(char)[] delegate()</pre>
void main ()
{
writeln("add: ", someprocedure(2, 3, (int a, int b) { return a + b; } ));
writeln("multiply: ", someprocedure(2, 3, (int a, int b) { return a * b; } ));
}
 
</lang>
 
"delegate" keyword is the same as say "function". In "someprocedure" we pass two integers (a and b) and one function f that has two integer inputs and one integer output. We call this procedure with anonymous functions (that return the sum and the times of two integers)
 
=={{header|Delphi}}==