Compile-time fold expression vs recursive template

Nick Treleaven nick at geany.org
Sun Jun 14 11:53:44 UTC 2020


On Saturday, 13 June 2020 at 10:52:46 UTC, Nick Treleaven wrote:
> the FoldExpression evaluates to a sequence of its last 
> parameter values

Actually it probably would be OK to use the parameter names as 
properties of the FoldExpression.

> template Merge(alias Less, uint half, S...)
> {
>     alias Result = __Fold(uint i = 0; uint j = half; Acc...)
>         if (i != half && j != S.length) {
>             static if (Less!(S[i], S[j]))
>                 __Fold!(i + 1, j, Acc, S[i]);
>             else
>                 __Fold!(i, j + 1, Acc, S[j]);
>         };
>     // fold handles min(half, S.length - half) elements of S
>     // then append any remaining elements
>     alias Merge = AliasSeq!(Result[2..$],
>         S[Result[0]..half], S[Result[1]..$]);
> }

alias Merge = AliasSeq!(Result.Acc,
     S[Result.i .. half], S[Result.j .. $]);

Easier to understand.



More information about the Digitalmars-d mailing list