How to resolve the algorithm Higher-order functions step by step in the D programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Higher-order functions step by step in the D programming language
Table of Contents
Problem Statement
Pass a function as an argument to another function.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Higher-order functions step by step in the D programming language
Source code in the d programming language
int hof(int a, int b, int delegate(int, int) f) {
return f(a, b);
}
void main() {
import std.stdio;
writeln("Add: ", hof(2, 3, (a, b) => a + b));
writeln("Multiply: ", hof(2, 3, (a, b) => a * b));
}
import std.stdio;
// Test 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;
}
// Normal 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"; }
}
// Used 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!(); }
void main() {
// Nested function.
string aNested() {
return "Nested";
}
// Bind to a variable.
auto variableF = function string() { return "variable.F"; };
auto variableD = delegate string() { return "variable.D"; };
C c = new C;
S s;
"Global".test(&aFunction);
"Nested".test(&aNested);
"Class".test(&C.aStatic)
.test(&c.aMethod);
"Struct".test(&S.aStatic)
.test(&s.aMethod);
"Template".test(&tmpFunc!(string))
.test(&tmpGroup!(string).t2);
"Binding".test(variableF)
.test(variableD);
// Literal function/delegate.
"Literal".test(function string() { return "literal.F"; })
.test(delegate string() { return "literal.D"; });
}
You may also check:How to resolve the algorithm Hello world/Text step by step in the Coq programming language
You may also check:How to resolve the algorithm String length step by step in the Delphi programming language
You may also check:How to resolve the algorithm Repeat a string step by step in the GAP programming language
You may also check:How to resolve the algorithm Emirp primes step by step in the Rust programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Factor programming language