Function Composition
atzensepp
webwicht at web.de
Wed Jan 24 08:19:06 UTC 2024
How is it possible to compose functions?
I came up with following solution that is not satisfactory
for two reasons:
1. the compose function should be argument agnostic
here it is explicitly coded for (int) -> (int)
2. the composition itself requires additional lambda
expressions
I would like to write compose(f,g)
```dimport std.stdio;
// Function composition:
int f(int x) { return x*2;} ;
int g(int x) { return x+2;} ;
int delegate (int) compose( int delegate(int)second, int
delegate(int)first)
{
return ((int i) => second(first(i)));
}
void main()
{
writeln( compose((x) => f(x),(x) => g(x))(2));
writeln( compose((x) => g(x),(x) => f(x))(2));
}
~
~
~
```
More information about the Digitalmars-d-learn
mailing list