Why does compose from std.functional return a templated function

Daniel Kozak kozzi11 at gmail.com
Wed Sep 16 10:50:06 UTC 2020

On Wed, Sep 16, 2020 at 12:00 PM Jan Hönig via Digitalmars-d-learn <
digitalmars-d-learn at puremagic.com> wrote:

> ...
> My main question is why? Is there something, which I am missing,
> that explains, why it is beneficial to return a templated
> function?
> (maybe, because I might want to compose together templated
> non-initialized functions?)

It has to be templated because than you can alias it and use it many times
something like

import std.stdio;
import std.functional : compose;
import std.algorithm.comparison : equal;
import std.algorithm.iteration : map;
import std.array : split, array;
import std.conv : to;

alias StrArrToIntArr = compose!(array,map!(to!int), split);
void main()
    auto str1 = "2 4 8 9";
    int[] intArr = StrArrToIntArr(str1);

If compose would not be template it would need to store functions addresses
so it would need to have some array of functions, this would be ineffective
and need to use GC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20200916/a4ba0f31/attachment.htm>

More information about the Digitalmars-d-learn mailing list