Function Composition

atzensepp webwicht at web.de
Thu Jan 25 08:25:02 UTC 2024


On Wednesday, 24 January 2024 at 21:34:26 UTC, user1234 wrote:
> On Wednesday, 24 January 2024 at 21:30:23 UTC, user1234 wrote:
>> On Wednesday, 24 January 2024 at 21:12:20 UTC, atzensepp wrote:
>>> [...]
>>> what a bummer!
>>
>> Have you tried 
>> https://dlang.org/phobos/std_functional.html#compose ?
>
> Well this violates the second requirement:
>
>> the composition itself requires additional lambda expressions
>>   I would like to write compose(f,g)
>
> I just realize, as this requires template specialization with 
> `!`. But this is how D works with these kind of things.

Hello,

thank you for pointing me to compose! I think i can live with 
intermediate lambda expression as it is hidden in the template. 
Obviously functions and delegates are different kinds. And 
compose from std.functional is excellent as it has varargs and is 
also very generic. The only rest issue is that I do not know how 
to get a pointer to composed function.

```d
int main()
{
   writeln(compose!(map!(to!(int)), split)("1 2 3").equal([1, 2, 
3]));
   writeln( compose!(f,g,g,f,g,g,f,g,g,f)(8));
   int function(int) t = compose!(f,g,g,f,g,g,f,g,g,f);

   writeln(t(3));

  // auto cf = curry!f;
//  auto cf1 = cf(1);
//  auto cf2 = cf(2);
   return(0);
}
```

This leads to:
```
gdc lambda4.d
lambda4.d:28:25: error: template compose(E)(E a) has no value
    int function(int) t = compose!(f,g,g,f,g,g,f,g,g,f);

```


More information about the Digitalmars-d-learn mailing list